For each code segment below determine the contents of the sp

For each code segment below, determine the contents of the specified variable following execution. If an error will occur, write ERROR and explain what caused the error. What is stored in Count by the following code? Count=0; for N=10: -0 .2:8.5 Count=Count+1; end What is displayed on the screen by the following code? for m=3:8 fprintf(\'%0.Of\ \'/m^2-m) end What is displayed on the screen by the following code? for m=3:8 M2=m^2-m; end fprintf (\' %0.Of\ \', M2) What is stored in P and C? S = 3; I = 2; P=1; C=0; for K=S:I:S^I P = P*K; C=C+1; end

Solution

Answers

(a)   Initially Count = 0.

In the beginning of for loop N will be 10 and Count will be 1.

In the second iteration N = 10 - 0.2 = 9.8 and Count = 2.

        In the third iteration N = 9.8 – 0.2 = 9.6 OR N = 10 – 0.2*(3-1) = 10 – 0.4 = 9.6 and Count = 3.

       Similarly in the 8 th iteration N = 10 – 0.2*(8-1) = 10 -0.2*7 = 10 – 1.4 = 8.6 and Count = 8.

        It will not execute the next iteration because N = 10 – 0.2*(9-1) = 8.4<N limit 8.5, Thus after the   

       execution of the code segment the value of count will be 8.

(b)   In this code segment the loop variable m will change from 3 to 8 with an increment of 1, that is during each iterations m will change like 3,4,5,6,7,8. During each iterations of the loop it will compute the value of “m^2 –m” and print it in the output. In the fprintf command also contains the format character %0.0f this will print only integer values. The \ will print the results in new line. Thus the output will look like

6

12

20

30

42

56

(c)   This example is same as the previous example, Here the printing command is outside the loop and hence it will execute only once. Inside the ‘for’ loop the variable M2 stores the value of m^2-m in each iteration. Whatever value stored in M2 will be erased during each iteration. In the first iteration M2 gets a value M2 = 3^2- 3 = 9-3 = 6. In the next iteration M2 = 4^2- 4 = 16 – 4 = 12. Similarly in the last iteration M2 = 8^2-8 = 64-8 = 56. Thus the fprintf function print the value of M2 in the screen, Hence OUTPUT will look like

56

(d)   Before the beginning of for loop the variable P = 1 and C = 0. The loop variable index K vary from S to S^I with an increment of I. That is K varies from 3 to 9 with an increment of 2. Hence in the first iteration K = 3 and P = 1*3 = 3 and C = 0+1 = 1. In the second iteration K will increment by 2 and become 5 and P = 3*5 = 15 and C = 1+1 = 2. In the third iteration K = 5+2 = 7 and P = 15*7 = 105 and C = 2+1 = 3. In the fourth iteration K = 7+2 = 9 and P = 105*9 = 945. There will not be any more iterations of the for loop because the iteration index already reach the limit. Hence after the completion of the program the variable P = 945 and C = 4.

 For each code segment below, determine the contents of the specified variable following execution. If an error will occur, write ERROR and explain what caused

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site