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

Hand Tracing Table 1. Hand-trace the following code, showing the value of n and

ID: 3757978 • Letter: H

Question

Hand Tracing Table

1. 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:

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

int n = 1;

while (n <= 3)

{

                print (n + ", ");

                n++;

}

Hand Tracing Table Goes Here:

3. 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 <= n)

{

                r = r * a;

                i ++;

}

Hand Tracing Table Goes Here:

4. 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.

1. 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.

2. 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)

3. Provide trace tables for these loops:

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

                     while (i<j) {i++; j--; n++;}

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

                     while (i<10) {i++; n=n+i+j; j++;}

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

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

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

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

4. What do these loops print?

                A. for (int i = 1; i < 10; i++){cout << i << “ “;}

                B. for (int i = 1; i < 10; i += 2){cout << i << “ “;}

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

                D. for (int i = 1; i < 10; i ++){cout << i <<“ “;}

                E. for (int i = 1; i < 10; i = i*2){cout << i << “ “;}

                F. for (int i = 1; i < 10; i ++){ if ( i%2 = 0) {cout << i <<“ “;}}

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

6. 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 << sum << endl;

Part 1: 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 2: output to screen goes here:

Explanation / Answer

Hand Tracing Tables solutions

1.Code:

int n = 5;

while (n >= 0)

{

n--;

print n;

}

Tracing of code

Variable n

Condition

n--

Print n

5

5>=0 , True

4

4

4

4>=0, True

3

3

3

3>=0, True

2

2

2

2>=0, True

1

1

1

1>=0, True

0

0

0

0>=0, True

-1

-1

-1

-1>=0,false Exit from loop

----------------------------------------------------------------------------------------------------------------------------------

2.

Code:

int n = 1;

while (n <= 3)

{

print (n + ", ");

n++;

}

Tracing of code

Variable n

Condition

Print n

n++

1

1<=3 , True

1

2

2

2<=3, True

2

3

3

3<=3, True

3

4

4

4<=3, False Exit from loop

------------------- -------------------------------------------------------------------------------------------------------------------------------------

3.

Code

int a=2;

int n=4;

int r = 1;

int i = 1;

while (i <= n)

{

r = r * a;

i ++;

}

Tracing of code

i<=n

r=r*a

i++

1<=4, True

2

2

2<=4, True

4

3

3<=4, True

8

4

4<=4, True

16

5

5<=4, false exit from loop


------------------------------------------------------------------------------------------------------------------------------------------------------------------

4.

Code

int n = 1;

while (n != 50)

{

print (n);

n = n + 10;

}

Tracing of Code

Variable n

Condition, n!=50

Print n

n=n+10

1

1!=0 , True

1

11

11

11!=0, True

11

21

21

21!=0, True

21

31

31

31!=0, True

31

41

41

41!=0, True

41

51

51

51!=0, True , Infinite loop

51

61

The above loop forms infinite loop since the condition at n=51 does not meet the condition of while loop n!=50 . Therefore, the loop repeats infinitely.

------------------------------------------------------------------------------------------------------------------------------------------------------------

1. Writing code using while loop that prints the following

1. A

Code to print squares of numbers less than 100

     //set n value to 100

          int n = 100;

          //set square to 0

          int square=0;

          //set i=1

          int i=1;

          //check condition square is less than n-value

          while (square<n)

          {            

              //print square

              System.out.println(square);

              //multiply i value by i value

              square=i*i;

              //increment i value by 1

              i++;

          }

------------------------------------------------------------------------------------------------------------------------------------------------

B.

Code to print values divisible by 100

//set n value to 100

int n = 100;      

int i=1;

//check condition i is less than n-value

     while (i<n)

     {            

          //check if i is divisible by 10

          if(i%10==0)

          //print square

          System.out.println(i);           

          //increment i value by 1

          i++;

          }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

C

Code to print the power of 2 less than n=100

//set n value to 100

int n = 100;      

int i=1;

int power=1;

//check power is less than n value

while (power<n)

     {                      

     //print square

     System.out.println(power);

     //multiply power by 2

     power=power*2;

     //increment i value by 1

     i++;         

     }

----------------------------------------------------------------------------------------------------------------------------------------------------------

2. Write a loop that computes

A

Code to find the sum of all even numbers from 2 to 100 inclusive

//set n value to 100

int n = 100;

//set sum to zero

int sum=0;

//run for loop that start from 1 to 100

//and sum the values if start is divisible by 2

for (int start = 1; start < =n; start++)

{

     if(start%2==0)

     //add start to sum variable

     sum+=start;

}

print (sum);

------------------------------------------------------------------------------------------------------------

2.

Code to sum of squares from 1 to 100

//set n value to 100

int n = 100;

//set sum to zero

int sumOfSquares=0;

//run for loop that start from 1 to 100

//and sum of the values of the squares

for (int start = 1; start < =n; start++)

{            

     //Add the square of start to sumOfSquares

     sumOfSquares+=start*start;

}

print(sumOfSquares);

--------------------------------------------------------------------------------------------------------------------------------------------

C)

Code for sum of odd numbers from a to b inclusive

//set a value

int a=1;

//set b value

int b=100;

//set sumOfOdd to zero

int sumOfOdd=0;

//Run for loop that start from 1 to 100

//and sum of the values of odd numbers from a to b

for (int start = a; start < =b; start++)

{                      

     if(start%2==1)

     //add odd value to the sumOfOdd variable

     sumOfOdd+=start;

}

//print sum of odd values

Print (sumOfOdd);

------------------------------------------------------------------------------------------------------------------------------------------------------

D)

Code to find the sum of the odd numbers in a number

//set value value

int value=32677;

//set sumOfOdd to zero

int sumOfOdd=0;

//while that runs until value not zero

while (value!=0)

     {                                    

     int rem=value%10;

     //check if it is odd value

     if(rem%2==1)

     //add odd value to the sumOfOdd variable

          sumOfOdd+=rem;

          //divide value by 10

          value=value/10;

     }

//print sum of odd values

Print (sumOfOdd);

------------------------------------------------------------------------------------------------------------------------------------------------------------------

3)

A)

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

while (i<j)

{

i++;

j--;

n++;

}

Trace of code

i

j

n

i<j

i++

j--

n++

0

10

0

0<10, true

1

9

1

1

9

1

1<9

2

8

2

2

8

2

2<8

3

7

3

3

7

3

3<7

4

6

4

4

6

4

4<6

5

5

5

5

5

5

5<5 false exit from loop

--------------------------------------------------------------------------------------------------------------------------------------------

B)

int i = 0; int j = 0; int n = 0;

while (i<10)

{

i++;

n=n+i+j;

j++;

}

Trace of code

i

j

n

i<10

i++

n=n+i+j

j++

0

0

0

0<10, true

1

1

1

1

1

1

1<10

2

3

2

2

2

7

2<10

3

11

3

3

3

11

3<10

4

17

4

4

4

17

4<10

5

25

5

5

5

25

5<10

6

35

6

6

6

35

6<10

7

47

7

7

7

47

7<10

8

61

8

8

8

61

8<10

9

77

9

9

9

77

9<10

10

95

10

10

10

115

10<10 false exit from loop

----------------------------------------------------------------------------------------------------------------------------------------------------------

C)

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

while (i>0)

{

    i--;

    j++;

   n=n+i-j;

}

Trace of code

i

J

n

i>0

i--

j++

n=n+i-j

10

0

0

0<10, true

9

1

10

9

1

10

9>0

8

2

18

8

2

18

8>0

7

3

24

7

3

24

7>0

6

4

28

6

4

28

6>0

5

5

30

5

5

30

5>0

4

6

30

4

6

30

4>0

3

7

28

3

7

28

3>0

2

8

24

2

8

24

2>0

1

9

24

1

9

24

1>0

0

10

16

0

10

16

0>0,false exit from loop


----------------------------------------------------------------------------------------------------------------------------------------------------------------

D)

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

while (i!=j)

{

i=i+2;

j=j-2;

n++;

}

i

j

n

i!=j

i=i+2

j=j-2

n++

0

10

0

0!=10, true

2

8

1

2

8

1

2!=10

4

6

2

4

6

2

4!=10

6

4

3

6

4

3

6!=10

8

2

4

8

2

4

8!=10

10

0

5

10

0

5

10!=10 false exit from loop

------------------------------------------------------------------------------------------------------------------------------------

4. Output of loops

A)

1 2 3 4 5 6 7 8 9

B)

1 3 5 7 9

C)

10 9 8 7 6 5 4 3 2

D)

1 2 3 4 5 6 7 8 9

E)

1 2 4 8

F)

Note : If condition must == not =(assignment )

2 4 6 8

----------------------------------------------------------------------------------------------------------------------------------------------------------------

5)

Infinite loop : A loop that executes unconditionally without come out of loop.

To cancel the infinite, cntl+c(cancellation of execution ) or Cntl+z(to stop program)

-----------------------------------------------------------------------------------------------------------------------------------------------

6)

Code

int n = 1729;

int sum = 0;

while (n > 0)

{

                int digit = n % 10;

                sum = sum + digit;

                n = n / 10;

}

cout << sum << endl;

Trace of Program code

n

n>0

digit=n%10

sum=sum+digit

n=n/10

1729

1729>0, true

9

9

172

172

172>0

2

11

17

17

17>0

7

18

1

1

1>0

1

19

0

0

0>0 false exit from while loop,print sum=19

Variable n

Condition

n--

Print n

5

5>=0 , True

4

4

4

4>=0, True

3

3

3

3>=0, True

2

2

2

2>=0, True

1

1

1

1>=0, True

0

0

0

0>=0, True

-1

-1

-1

-1>=0,false Exit from loop

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote