Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What is the output of the following code? #include <iostream> using namespace st

ID: 3691107 • Letter: W

Question

What is the output of the following code?

#include <iostream>

using namespace std;

void TestIt(int Y);

int main ()

{

int A=5;
TestIt(A);
cout << " A = " << A << " " ;
return 0; }

void TestIt (int Y)
{ int Z(7);
Z= Y*3 -
Z; Y = Y * 2;
cout << " Y = " << Y << " Z = " << Z ;
return; }

a) Y=10 Z=8 A=5 b) Y=10 Z=8 A=10 c) Y=8 Z=10 A=10 d) Y=8 Z=10 A=5

I know the answer is A, but how did I know that I had to use the int A as the value of Y?? Please explain

Explanation / Answer

#include <iostream>
using namespace std;
void TestIt(int Y);
int main ()
{
int A=5;   //A is assigned a value of 5.
TestIt(A);   //A function is called with A as a call by value.
cout << " A = " << A << " " ;   //The value in A i.e., 5 will be printed.
return 0; }

void TestIt (int Y)   //The value 5 which is passed by the calling function will be stored in Y.
{ int Z(7);           //The value 7 will be stored in Z.
Z= Y*3 -Z;            //5 * 3 = 15, then 15 - 7 = 8 will be stored in Z.
Y = Y * 2;           //5 * 2 = 10 will be stored in Y.
cout << " Y = " << Y << " Z = " << Z ;   //The value of Y i.e., 10, and the value of Z i.e., 8 will be printed.
return; }

//So, by the end of this program execution the output is:
//Y = 10 Z = 8 A = 5

So, a) Y=10 Z=8 A=5 is the final answer.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote