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

Why does this code print \"bye\" first and then \"Hello 1\" 3 times. I do not un

ID: 672070 • Letter: W

Question

Why does this code print "bye" first and then "Hello 1" 3 times. I do not understand why it goes in reverse, please explain.

#include <iostream>

void what(int[ ], int); using namespace std;

int main()

{

int s, a[ ]={0, 1,2,3,4,5,6,7};

what(a,8);

return 0; }

void what(int b[ ], int size)

{

cout << "size is: " << size << endl;

if (size > 1 ) {
what(b, size - 2);
cout << "size: " << size << endl;
cout << "hello ";

cout << b[1] << " ";

}

else {
cout << "bye ";

}}

Explanation / Answer

It is printed in reverse because what function is a recursive function due to which it will keep calling itselt till the size is not greater than 1 once the size is less than or equal to 1 then the recursive calls will start returning to the previous calls and in turn will do their remaining work.

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