Pointer Study Guide Question

I'm having issues writing this program.

Pointer Rewrite
The following function uses reference variables as parameters. Rewrite the function so that it uses pointers instead of reference variables. Write a main procedure to test your function.
1
2
3
4
5
6
7
int myFunction(int &number, int &anotherNumber) 
{ 
      int temp = number; 
      number = anotherNumber * 10; 
      anotherNumber = temp * 10; 
      return number + anotherNumber 
}


This is what I have so far:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int myFunction(int *number, int *anotherNumber);

int main(){
	int number = 5;
	int anotherNumber = 10;
	myFunction(&number, &anotherNumber);
	
	return 0;

}

int myFunction(int *number, int *anotherNumber) {
	cout << number << endl;
	cout << anotherNumber << endl;

	cout << *number << endl;
	cout << *anotherNumber << endl;

	return 0;
}



Last edited on
Your answer seems unrelated to the question :+)
Sorry, I posted the code to my other problem!
From your cout statements, you've clearly understood how to access the value that a pointer points to. So what are you having trouble with?
Your prompt says you need to "Rewrite the fucnction...". In order to rewrite the function, you need to understand what it does in the first place, and then reproduce those results the new way. Consider your main program (with a little twist):

1
2
3
4
5
6
7
int main(){
	int number = 5;
	int anotherNumber = 10;
	int result = myFunction(number, anotherNumber);
	
	return 0;
}


If you call the original myFunction, At line 5, what are the values of number, anotherNumber and result?

Now you need to take the function you wrote at line 15 and create the same results. The function you wrote only prints values out the the console. How do you obtain the results from the original function?
Here is my edit after your suggestion but I still can't compile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;


int myFunction(int *number, int *anotherNumber)
{

int temp=*number;
*number= *anotherNumber * 10;
*anotherNumber = temp * 10;
return *number + *anotherNumber;
}

int main()
{
    int number, anotherNumber, value;
    cout << "Please enter a number" << endl;
    cin >> number;
    cout << "Please enter another number" << endl;
    cin >> anotherNumber;


    myFunction(&number, &anotherNumber);
    cout << myFunction(&number, &anotherNumber) <<  endl;
    return 0;
}
Last edited on
I still can't compile

Is that what your compiler told you? Nothing more than "I can't compile"?

If so, I suggest you get rid of it, and start using a compiler that gives helpful error messages that indicate which line the error is on, and what the error is.
While I think @MikeyBoy may have been just a little bit too sarcastic, I have to agree wholeheartedly with his point of view. Saying
I still can't compile.
is a little like me saying "I heard this great song on the radio this morning. Can you tell me who sang it?"

In order to answer questions we need information.

I don't see anything that jumps out as causing compiler problems. I think you will get results that you are possibly not expecting, but without error messages, I don't immediately see why it doesn't compile.

By the way, I'll leave the unexpected results as a little Easter egg for you. When you get the program running, see if you can figure out why it prints out what it does.
Topic archived. No new replies allowed.