Passing Pointer to a Function

I have this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include "stdafx.h"
#include <iostream>
using namespace std;


void square_cube(float *x,float *y)
{


}

int main()
{
	float a=5.0, b=7.0;
	cout<<"Before function call: a="<<a<<"b="<<b<<endl;
	square_cube(&a,&b);
	cout<<"After function call: a="<<a<<" b="<<b<<endl;

	system("pause");
	return 0;
}


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!
Why are you trying to use recursion? I think you might be overthinking the problem. How do you calculate the square or the cube of a number.
I could use recursion if I wanted right? or is that much harder than what I could do. I don't know.....
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.
Last edited on
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 is what I came up with. Its compiling right but it must be something with how I'm coding it because its not square and cubing:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void square_cube(float *x,float *y)
{
	float s=0;
	s = (*x)*(*x);
	s = (*y)*(*y)*(*y);
}

int main()
{
	float a=5.0, b=7.0;
	cout<<"Before function call: a=" << a<<"b="<<b<<endl;
	square_cube(&a, &b);
	cout<<"After function call: a="<<a<<" b="<<b<<endl;

	system("pause");
	return 0;
}


EDIT:
Its compiling with no errors
Last edited on
Hi brownjas,

In the square_cube function just remove the variable s and use *x instead for double and *y for cube.
Last edited on
Would I point x and y to a and b?


You already are:

square_cube(&a, &b);

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.
Thanks @Disch
The pointers were tricky but I figured it out. Appreciate the guidance!!
Topic archived. No new replies allowed.