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

C++ What is the ouput of this code and why?? 1) If the code of main( ) shown bel

ID: 3811210 • Letter: C

Question

C++ What is the ouput of this code and why??

1) If the code of main( ) shown below is executed, the output will be:

int enigma(int val1, int val2)

{

     return (val1 + val2);

}

int main()

{

     int num = 5;

     cout << enigma(enigma(num, num), num) << endl;

     return 0;

}

2) If the code of main( ) shown below is executed, the output will be:

int enigma(int val1, int val2)

{

     if(val1 > val2)

          return val1++;

     else

          return ++val2;

}

int main()

{

     int num1 = 5, num2 = 3;

     cout << enigma(num1, num2) << '&' << enigma(num2, num1) << endl;

     return 0;

}

Explanation / Answer

Question 1

Answer: 15

enigma(enigma(num, num), num)

enigma function adding the parameter values and sending back to main. Here num value is 5. Inside function enigma(num, num) will return value 10.

So enigma(enigma(num, num), num) function will return value 15.

Question 2

Answer: 5&6

When we pass val1 as 5 ans val2 as 3 then if block will execute. So Inside if block we are using post increment operator so value will not change soo will return value 5.

When we pass val1 as 2 ans val2 as 5 then else block will execute. So Inside else block we are using pre increment operator so value will change so will return value 6.

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