Complete the trace table of the following program and output of program? int n = 1729;int sum = 0;while (n > 0) { int digit = n % 10; sum = sum + digit; n = n / 10; } cout Part (a): Trace table...


Complete the trace table of the following program and output of program? int n = 1729;int sum = 0;while (n > 0)


{



int digit = n % 10;



sum = sum + digit;



n = n / 10;


}


cout <>


Part (a): Trace table goes here:


(NOTE: be sure to strike out to demonstrate there’s a change in the memory, please use MS Word to insert the table and disable all Autocorrect features by clicking on the lightning bolt when MS Word tries to Auto correct, select disable. OR you are welcome to write out the table in pencil, scan it and upload it as PDF.)


Part (b): output to screen goes here:


2. Hand-trace the following code, showing the value of n and the output.


int n = 5;


while (n >= 0)


{



n--;



print n;


}


Hand Tracing Table Goes Here:


3. Hand-trace the following code, showing the value of n and the output. What potential error do you notice?


int n = 1;


while (n


{



print (n + ", ");



n++;


}


Hand Tracing Table Goes Here:


4. Hand-trace the following code, assuming that a is 2 and n is 4. Then explain what the code does for arbitrary values of a and n.


int r = 1;


int i = 1;


while (i


{



r = r * a;



i ++;


}


Hand Tracing Table Goes Here:


5. Trace the following code. What error do you observe?


int n = 1;


while (n != 50)


{



print (n);



n = n + 10;


}


Hand Tracing Table Goes Here:


Part III (2 points X 5 hand trace tables = 10 points): Trace Table – While and For Loops


On this Word Document, complete these exercises.


6. Write a while loop that prints



A. All squares less than n. For example, if n is 100, print 0 1 4 9 16 25 36 49 64 81.


B. All positive numbers that are divisible by 10 and less than n. For example, if n is 100, print 10 20 30 40 50 60 70 80 90



C. All powers of two less than n. For example, if n is 100, print 1 2 4 8 16 32 64.


7. Write a loop that computes



A. The sum of all even numbers between 2 and 100(inclusive).



B. The sum of all squares between 1 and 100 (inclusive).



C. The sum of all odd numbers between a and b(inclusive).



D. The sum of all odd digits of n. (For example, if n is 32677, the sum would be 3 + 7 + 7 = 17)


8. Provide trace tables for these loops:



A. int i = 0; int j = 10; int n = 0;



while (i



B. int i = 0; int j = 0; int n = 0;



while (i


C. int i = 10; int j = 0; int n = 0;



while (i>0) {i--; j++; +i-j;}



D. int i = 0; int j = 10; int n = 0;



while (i!) {+2; -2; n++;}


9. What do these loops print?



A. for (int i = 1; i <><><>



B. for (int i = 1; i <><><>



C. for (int i = 10; i > 1; i--){cout <><>



D. for (int i = 1; i <><>



E. for (int i = 1; i <><><>



F. for (int i = 1; i <><>


10. What is an infinite loop? on your computer, how can you terminate a program that executes an infinite loop?

May 18, 2022
SOLUTION.PDF

Get Answer To This Question

Submit New Assignment

Copy and Paste Your Assignment Here