The function should pass the square of the first input parameter and the cube of the second input parameter back to the calling routine. I thought I could use a recursion but its not going to work. Help please!
If you have a pointer to a variable, you "dereference" the pointer by sticking a * before it. Dereferencing a a pointer means you are accessing whatever it is the pointer points to.
Example:
1 2 3 4 5 6 7
int foo = 5;
int* ptr = &foo;
// ptr points to foo
*ptr = 10; // this modifies whatever ptr points to. Since ptr points to foo, this modifies foo.
cout << foo; // prints 10
You have pointers in your square_cube function. Those pointers point to other variables. All you have to do to square/cube those variables is dereference and multiply.
EDIT:
Recursion is completely unrelated to this problem.
I understand how to use pointers but the a and b parts are throwing me off in the main. Would I point x and y to a and b? I tried that and it was saying I needed to declare the a and b. I knew it was not correct because a and b are declared in the main. Since I was given the main I can't change it.
This line passes &a as the 'x' parameter
and &b as the 'y' parameter.
Therefore:
x = &a
y = &b
So x and y already point to what you need them to point to.
1 2 3 4 5 6
void square_cube(float *x,float *y)
{
float s=0;
s = (*x)*(*x);
s = (*y)*(*y)*(*y);
}
You are very close.
You are properly squaring and cubing the values... the problem is you are storing the result in this new 's' variable you made rather than storing them in the variables you're supposed to store the results in.
x and y are both input and output here. You want to square whatever x points to... and write that value back to whatever x points to.