I'm very new to programming and I have been trying to do the following, but I don't know if I am doing it right.
I have the following declarations
int a, b, c;
int *p1, *p2, *p3;
char d, str[10], *cp;
float big, r;
and with those declarations I have to find out how to declare the following q variables.
For example, if *p3 is a integer pointer and r is a float. then what would q1 would have to be. I need to find out how to declare it.
But since each one is of a different type, I don't know how to do it. Some hints would be kindly appreciated
q1 = r + *p3;
q2 = &p1 + 5;
q3 = *str + c;
q4 = &str[4];
q5 = *p2;
int a, b, c;
int *p1, *p2, *p3;
char d, str[10], *cp;
float big, r;
So I think the question is to use the declarations above, and the expressions below and come up with a valid set of types for the expressions.
q1 = r + *p3;
q2 = &p1 + 5;
q3 = *str + c;
q4 = &str[4];
q5 = *p2;
I think these types are valid for the expressions above:
float q1 = r + *p3; // float = float + int
int **q2 = &p1 + 5; // int ** = int** + int
int q3 = *str + c; // int = char + int
char *q4 = &str[4]; // char * = char *
int q5 = *p2; // int = *int*
q1, q3 and q5 could be any numeric type. I picked the one most obvious to me.
Related
float a[1000];
float b[1000];
float c[1000];
int main()
{
float *w1;
w1 = &a;
float *w2[1000];
w2 = &b;
...
}
so error: cannot convert float (*)[1000] to float* in assignments - to w1
and compiler throws error: incompatible types in assignments of float* to float * [1000] to w2
Probably you aim to do something like this?
float a[1000];
float b[1000];
float c[1000];
int main()
{
float *w1;
w1 = a; // no need of &
float *w2; // no need of [1000]
w2 = b; // no need of &
return 0;
}
float *w2[1000];
is erroneous, and should be float *w2;
float a[1000];
float b[1000];
float c[1000];
int main()
{
float *w1;
w1 = a;
float *w2;
w2 = b;
}
compiles with no error.
The '&' is intended for single variables, not arrays of single variables. When you instantiate an array with [] your variable actually transforms into a pointer.
I created a little program that is supposed to calculate pi using the first 26 iterations of the Leibniz formula in c++, just to see id it would work. When I ran the code, it outputted 4 instead of a floating point number. What is going on and how can I fix it? Here is the code:
#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
float a = 1/1;
float b = 1/3;
float c = 1/5;
float d = 1/7;
float e = 1/9;
float f = 1/11;
float g = 1/13;
float h = 1/15;
float i = 1/17;
float j = 1/19;
float k = 1/21;
float l = 1/23;
float m = 1/25;
float n = 1/27;
float o = 1/29;
float p = 1/31;
float q = 1/33;
float r = 1/35;
float s = 1/37;
float t = 1/39;
float u = 1/41;
float v = 1/43;
float w = 1/45;
float x = 1/47;
float y = 1/49;
float z = 1/51;
float a1 = a-b+c-d+e-f+g-h+i-j+k-l+m-n+o-p+q-r+s-t+u-v+w-x+y-z;
float b1 = a1*4;
cout << b1;
}
Yes, I know there are much more simple ways to do this, but this is just a proof of concept.
When you use:
float b = 1/3;
the RHS of the assignment operator is evaluated using integer division, which results in 0. All other variables have the 0 value except a which has the value of 1.
In order to avoid that, use
float b = 1.0f/3;
or
float b = 1.0/3;
Make similar changes to all other statements.
Another way, use casting
float a = (float)1/1; // C-style cast
or
float a = float(1)/1;
We need to understand the next code for college. Everything goes fine till the this instruction :
(*((*q1)+2))++;
Can someone tell me what's the effect of that instruction? q1 or p1 doesn't change. Here is the whole exercise.
double a = 2;
double b[] = {1,3,5};
double c[] = {4,6,8,10};
double & d = a;
double* p1 = b;
double* p2 = &c[1];
double **q1 = &p2;
double **q2 = &p1;
(*q1)++;
a = **q1-*(p1+1);
q1 = q2;
(*((*q1)+2))++;
p2-=2;
**q1 = *p2;
*q2 = &c[1];
b[2] -= *(p2+3)-**q1;
d = c[1];
cout << a << endl << b[0] << endl << b[1] << endl << b[2] << endl;
Considering that q1 is a double** :
*q1 Dereference q1, obtaining a double*&
( )+2 Add 2, ending up pointing two double's further
*( ) Dereference where we are, obtaining a double&
( )++; Increment that last double.
Work from the inside out.
(*((*q1)+2))++;
(*q1) is p2 so replace.
(*(p2 + 2))++;
p2 + 2 is the address of c[3] so replace
(*(&c[3])++;
*(&c[3]) is 10 so replace
10++ is 11
(*((*q1) + 2))++; is equivalent to (*(p1 + 2))++;
I have the filter coefficients for a Butterworth lowpass filter, coming from the Matlab function [b, a] = butter(3, 0.4, 'low') and I am implemented the same computation Matlab is using according to their documentation for filter(b, a, X). The results for filtering a constant signal of 5.0, for example, are the same, but only for the first 10 values!?
I suppose that my circular buffer is wrong, but I cannot find any problems. The values are written correctly in x using the filter method, the arrays are initialized with zeros, the circular buffer pointer n has the right values... Do you have any ideas?
// Interface
class LowpassFilter {
private:
double x[10]; // input vector
double y[10]; // output vector
int n; // pointer to the current array index
public:
LowpassFilter();
double filter(double sample);
};
// Implementation
// filter coefficients a and b
const double a[] = {1.0, -0.577240524806303, 0.421787048689562, -0.056297236491843};
const double b[] = {0.098531160923927, 0.295593482771781, 0.295593482771781, 0.098531160923927};
static int c = 0;
LowpassFilter::LowpassFilter() : x{0.0}, y{0.0}, n(0) { } // Constructor
double LowpassFilter::filter(double sample)
{
x[n] = sample;
y[n] = b[0] * x[n] + b[1] * x[(n-1)%10] + b[2] * x[(n-2)%10] + b[3] * x[(n-3)%10]
- a[1] * y[(n-1)%10] - a[2] * y[(n-2)%10] - a[3] * y[(n-3)%10];
std::cout << c++ << ": " << y[n] << std::endl; // for checking the result with the Matlab results
double result = y[n];
n = (n + 1) % 10; // new pointer index
return result;
}
Thanks to Mike Seymour and emsr the problem were the negative indexes in the computation of y[n]. To solve the problem only one line has to be adopted:
y[n] = b[0] * x[n] + b[1] * x[(n-1+m)%m] + b[2] * x[(n-2+m)%m] + b[3] * x[(n-3+m)%m]
- a[1] * y[(n-1+m)%m] - a[2] * y[(n-2+m)%m] - a[3] * y[(n-3+m)%m];
to make sure that the index is positive. Now it works fine. Thanks alot!
I am a C++ beginner. I would like to get the normal vector of a surface, which is determined by three point a,b and c. I have the following code, but I do not know what is wrong with it. Thanks for the help.
#include <iostream>
using namespace std;
class point
{
public:
double x,y,z;
};
class crproduct:point
{
public:
double x1,x2,x3,y1,y2,y3,z1,z2,z3,Ax,Ay,Az,Bx,By,Bz;
point crproduc(point *a,point *b,point *c)
{
//point a
x1 = (*a).x;
y1 = (*a).y;
z1 = (*a).z;
//point b
x2 = (*b).x;
y2 = (*b).y;
z2 = (*b).z;
//point c
x3 = (*c).x;
y3 = (*c).y;
z3 = (*c).z;
//Vector A
Ax = x1-x2;
Ay = y1-y2;
Az = z1-z2;
//vector B
Bx = x2-x3;
By = y2-y3;
Bz = z2-z3;
//cross product
point vector;
vector.x = (Ay*Bz)-(By*Az);
vector.y = -(Ax*Bz)+(Bx*Az);
vector.z = (Ax*By)-(Ay*Bx);
return vector;
}
};
int main ()
{
point *pp, *p1, *p2;
point cd;
crproduct cr1,cr2,cr3,cr4;
(*pp).x = 12;
(*pp).y = 13;
(*pp).z = 15
(*p1).x = 10;
(*p1).y = 10;
(*p1).z = 10;
(*p2).x = 8;
(*p2).y = 5;
(*p2).z = 2;
cd = cr1.crproduc(pp,p1,p2);
cout << cd.x << " " << cd.y << " " << cd.z << endl;
system("pause");
return 0;
}
This is incorrect:
point *pp,*p1,*p2;
point cd;
crproduct cr1,cr2,cr3,cr4;
(*pp).x=12;
(*pp).y=13;
(*pp).z=15
(*p1).x=10;
(*p1).y=10;
(*p1).z=10;
(*p2).x=8;
(*p2).y=5;
(*p2).z=2;
Apart from the missing semicolon, the point *pp,*p1,*p2 line establishes three typed pointers. It doesn't create any objects or instantiate the pointers. So at that point, using the pointers will have undefined results.
You then go on to access the pointers.
If you want them on the stack, just declare the objects directly as:
point pp, p1, p2;
... and then access appropriately. If you need them on the heap then you should use new to create objects that the pointers can point to, e.g.
pp = new point;
And don't forget to delete later.