Hand trace of following code include using namespace std int

Hand trace of following code:

#include <iostream>
using namespace std;


int x;


void summer(int&, int);
void fall(int, int&);


int main()
{
int intNum1 = 2;
int intNum2 = 5;
x = 6;


summer(intNum1, intNum2);
cout << intNum1 << \" \" << intNum2 << \" \" << x << endl;
fall(intNum1, intNum2);
cout << intNum1 << \" \" << intNum2 << \" \" << x << endl;
return 0;
}
void summer(int& a, int b)
{
int intNum1;
intNum1 = b + 12;
a = 2 * b + 5;
b = intNum1 + 4;
}
void fall(int u, int& v)
{
int intNum2;
intNum2= x;
v = intNum2 * 4;
x = u - v;
}

Solution

Please follow the code and commensts for description :

CODE :

#include <iostream>
using namespace std;


int x;


void summer(int&, int);
void fall(int, int&);


int main()
{
   int intNum1 = 2;
   int intNum2 = 5;
   x = 6;

   summer(intNum1, intNum2);
   cout << intNum1 << \" \" << intNum2 << \" \" << x << endl;
   fall(intNum1, intNum2);
   cout << intNum1 << \" \" << intNum2 << \" \" << x << endl;
   return 0;
}

void summer(int& a, int b)
{
   int intNum1;
   intNum1 = b + 12;
   a = 2 * b + 5;
   b = intNum1 + 4;
}

void fall(int u, int& v)
{
   int intNum2;
   intNum2= x;
   v = intNum2 * 4;
   x = u - v;
}


Hand Trace :


intNum1   |   intNum2   |   x
------------------------------
   2   |       5   |   6
   15   |       5   |   6
   15   |       24   |   -9
  
Description :

Initially the data for the variables is given by 2, 5, 6 for the intNum1, intNum2 and x respectively. The same data is sent to the function calls as the parameters with the first parameter as the address of the variable and the second parameter as the data of the variable for the function call of summer. So the values get updated as the above to 15 for the intNum1 after the call as the data has been passed and that the remaaining values do no get affected as the the address is passed instead of the data for the intNum2. This has been tabulated above.

Now for the same data the function call for the fall has been done with the first parameter as the value and the second as the address. So thus after the call the first parameter remains same and the respective second value get s updated with the affected x - value as 24, -9 as updated values respectively.

Hope this is helpful.

Hand trace of following code: #include <iostream> using namespace std; int x; void summer(int&, int); void fall(int, int&); int main() { int intNu
Hand trace of following code: #include <iostream> using namespace std; int x; void summer(int&, int); void fall(int, int&); int main() { int intNu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site