What will the following code display?

# inlcude <iostream>
using namespace std;
void test(int &, int =1, int =1);
void test( int, int *, int&);
const int SIZE = 4;
int main()
{
int first=1. second = 2, third = 3;
int Numbers[SIZE]={2,4};
test(third, first);
cout << first << "," << second << "," << third << ","<< endl;
for (int i = 0; i< SIZE; i++)
cout << Numbers[i] << "," ;
test(third, &third, second);
cout << endl << first << "," << second << "," << third << "," << endl;
return 0;
}

void test(int & first, int second, int third)
{
first += 1;
second += 2;
third += 3;
cout << first << "," << second << "," << third << "," << endl;
}

void test( int first, int * second, int & third)
{
first += 3;
*second += 2;
third += 1;
}

Now its suppose to display:

4,3,4
1,2,4
2,4,0,0
1,3,6

Now i got the first three displays but I am having trouble on how the program got the output 1,3,6? I keep looking through the program but I don't see how it can. If anyone can explain this to me I would appreciate it a lot.

Thanks Guys
Hi,

As I mentioned in my other post, please use code tags.

Put some std::cout staements in your code to see what is happening. That way you can learn.

Cheers
In the fourth display, the second and third arguments in the call to test will update the variables back in main.

Before this call,
first is 1
second is 2
third is 4

test(third, &third, second);

1
2
3
4
5
6
void test( int first, int * second, int & third)
{
first += 3; // this does not change value of third in main - still 4
*second += 2; // this adds 2 to value of third in main, now 6
third += 1; // this adds 1 to second variable in main, now 3
}


Now,
first is 1
second is 3
third is 6
> Put some std::cout staements in your code to see what is happening. That way you can learn.
Or better yet, use a debugger.


Use meaningful names for functions and variables, that'll help to follow the code.
Or better yet, use a debugger.


Yes, as I said in my other post to the same guy.

I also mentioned pen & paper. :+)

Sometimes I think it is worthwhile to create some output & look at that, to aid the OP's understanding.

Of course, if the program crashes then the debugger is indespensible.
Topic archived. No new replies allowed.