Why is Y suppose to be 6 instead of 14 in this program? Thank you.

0 Bob huginstien · May 1, 2015
#include <iostream>
#include <string>

using namespace std;


void Try (int&, int );
int x;
int y;
int z;


int main()
{

    x=1;
    y=2;
    z=3;
    Try(y,x);
    
    cout << x << '' <<  y << ' ' << z <<endl;
    return 0;
}
void Try (int&a , int b)
{
    x= a+2;
    a=a*3;
    b= x+a;
}

the solution is 1 ' ' 6 ' ' 3  

Post a Reply

Replies

Oldest  Newest  Rating
0 Bob huginstien · May 1, 2015
 why is that ?  does it skip x =a+2, and b= x+a?  is it because b is not a reference parameter and int a is?  I more curious as to why it skips x= a+2. Thank you. 
+1 Maciej Gozdek · May 1, 2015
Umm, shouldn't the solution be 4 6 3?

And no, it doesn't skip anything.

At the begining a takes the value of y which is 2, so x =2+2=4.
Then a is still 2 and that times 3 is 6.
At the end b equals 4+6 so that's 10 but b is never used in the code to do anything so I don't really know what the point of b=x+a is.
0 Bob huginstien · May 1, 2015
this was a quiz question. My compiler gives me  4 6 3,  isnt it  1 6 3  because b is associated with x? That is a pass by value correct? 
+1 Maciej Gozdek · May 1, 2015
B takes the value of x = 1 at the start of the function and keeps that value until it is changed with b=x+a (put cout << b << endl; before b=x+a and you'll see that it's still 1. But after try() is done x equals 4 and since you're printing x after that function, the result is 4 6 3. If the quiz is from a book there might have simply been an error in print.
0 Bob huginstien · May 2, 2015
That is weird, I thought you would need to have a reference parameter to allow it change like that or be manipulated, really what would be the difference between the two? The value parameter like int b should be only incoming, where as the reference is incoming and outgoing and outgoing with &, I tested the program and it is 4 6 3, but from what I have learned in class it doesn't make any sense  . No its from one of my previous quizzes that my professor gave us and I am using it as a review for the final. 
+1 c student · May 2, 2015
The Try function has no return value, meaning anything passed into the function will not be returned, also anything copied into the function will be destroyed at the end of its use.  The special case of variable y is that it is passed by reference, meaning that the changes which occur within the Try function directly affects its original, where a=a*3 = y=y*3 = 6.  At the end of the function, all of the variables copied into the function are destroyed and nothing is returned, however, the value of y still remains the same, therefore, x = 1, y = 6, z = 3.

Another example:
#include <iostream>
#include <cassert>

void swap (int &x, int y) {
    int temp;
    temp = x;
    x = y;
    y = temp;
}

int main() {
    int x = 3;
    int y = 2;
    
    swap (x, y);
    assert (x == 2 && y == 2);
   
    return 0;
}



http://ideone.com/1LVnJJ

Here is some reading material:
http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references
0 Bob huginstien · May 2, 2015
C student, thank you as well Krootushas Gesu and Maciej Gozdek  . To Maciej Gozdek  now I understand in this case why  there is a 4 6 3 instead of a 1 6 3 thank you for this. My professor declared those as global variables meaning they are accessed through out this program, now I tested if x were to be in the main function it and if there is a x inside the void ,which its now a local variable, which will print out 1 6 3, like this:

#include <iostream>
#include <string>

using namespace std;


void Try (int&, int );

int y;
int z;


int main()
{
int x =1;

y=2;
z=3;
Try(y,x);

cout << x << " "<< y << " " << z <<endl;
return 0;
}
void Try (int&a , int b)
{int x ;
x= a+2;
a=a*3;
b= x+a;
}


Thank you all so much for your help!
0 zhang yida · May 2, 2015
 you can run your IDE in your debug model and see what's going on with each variable.
  • 1

C++

107,207 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator