Thursday, July 9, 2009

C++ practice exam question. I need help understanding what this program is doing and what it outputs.?

For the partial code below:





1. int a(0);


2. int b(2);


3. int f(int d, int c) {


4. a = c;


5. if (d %26lt; c)


6. d = a + b;


7. return d; }


8. int g(int%26amp; c) {


9. int n=0;


10. int a = c;


11. if (n %26lt; f(n,c))


12. c = b - a;


13. return n; }


14. int main() {


15. int i = 1;


16. int b = g(i);


17. return 0; }





a) trace the code line by line. For those lines where a function is called show the values of the variables; before entering the function, after entering the function, before leaving the function, and after leaving the function.


For example for the code below:


1. int f1(int s) {


2. return 0 }


3. int main() {


4. a = 3;


5. f1(a);


6. return 0; }





you would type





line 3 call to main()


line 4


line 5 call to f1(a=3)


line 1 f1(s=3)


line 2 return f1(s=3)


line 5 return from f1(a=3)


line 6 return main()





b) What are the values of a and b at the end of the program before the main() function returns?

C++ practice exam question. I need help understanding what this program is doing and what it outputs.?
So they almost give away the answer.





line 14 enter main


line 15


line 16 call to g (c=1)


etc

night blooming cereus

No comments:

Post a Comment