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

Consider the statement k = k – 1; inside the while loop of the program of Figure

ID: 3593229 • Letter: C

Question

Consider the statement k = k – 1; inside the while loop of the program of Figure 10.5. Remove this statement from the program.
(a) Will this change raise a syntax or type error for this modified program? Explain.

hand-simulate the program in Figure 10.5 without removing k = k -1. By hand-simulate, I mean that you should show what the program is doing step by step. You should show what the values of the variables are each time it loops. Then, once you're finished with that, answer Part A (without hand-simulation) by removing k = k-1. For full points, be thorough with your response as opposed to sharing just a yes or no. This will get you started for the hand-simulation part. On the first iteration, inside fibonacci(), but outside the loop while (k>0), we have:

n=8

fib0=0

fib1=1

k=8

Then, inside the while loop, we have:

while (8>0) {

   temp=0

   fib0=1

   fib1=1

   k=7

}

The first iteration has completed but we don't return anything because k is still greater than 0. Now, k is 7, so continue with:

int fibonacci (ntn) t int fib0 fibl, temp. k while (k > 0) tempfibo fibl fibo temp: is return fibo: int main int answer answer fibonacci(8);

Explanation / Answer

First we are analyzing the program without removing the statement k = k-1.

Now for k = 8, we have temp = 0, fib0 = 1 which gives, fib1 = 1 + 0 = 1 and k = 7.

Now for k = 7, we have temp = 1, fib0 = 1 which gives, fib1 = 1 + 1 = 2 and k = 6.

Now for k = 6, we have temp = 1, fib0 = 2 which gives, fib1 = 2 + 1 = 3 and k = 5.

Now for k = 5, we have temp = 2, fib0 = 3 which gives, fib1 = 3 + 2 = 5 and k = 4.

Now for k = 4, we have temp = 3, fib0 = 5 which gives, fib1 = 5 + 3 = 8 and k = 3.

Now for k = 3, we have temp = 5, fib0 = 8 which gives, fib1 = 5 +8 = 13 and k = 2

Now for k = 2, we have temp = 8, fib0 = 13 which gives, fib1 = 13 + 8 = 21 and k = 1.

Now for k = 1, we have temp = 13, fib0 = 21 which gives, fib1 = 21 + 13 = 34 and k = 0.

Since k is not greater than 0. Therefore, loop will be closed and function will return 21.

Now, if we remove statement k = k -1. We will never be able to reach the terminating condition of the loop. So loop will never terminate and in turn program will give an error.

Hope it helps, do give your valuable response.