7. Consider the following pseudocode fragment to answer a-c: sum :=0 For x = 1 t
ID: 3146083 • Letter: 7
Question
7. Consider the following pseudocode fragment to answer a-c:
sum :=0
For x = 1 to 4
sum :=sum+x
End-for
a. What is the value of the variable sum after the third iteration?
b. How many iterations will the for-loop execute?
c. What is the final value for the variable sum after executing the for-loop?
8. Consider the following pseudocode fragment to answer a-c:
product := 2
count := 7
While (count >1)
product :=(product)(count)
count :=count-3
End-while
a. What is the value of product after the first iteration?
b. How many iterations will the while-loop execute?
c. What is the final value for product?
Explanation / Answer
7. sum :=0
For x = 1 to 4
sum :=sum+x
End-for
a. The value of sum after first iteration is 0 + 1 = 1.
The value of sum after second iteration is 1 + 2 = 3.
The value of sum after third iteration is 3 + 3 = 6.
b. Since x goes from 1 to 4, the number of iterations = 4.
c. The value of sum after fourth iteration = 6 + 4 = 10.
8.product := 2
count := 7
While (count >1)
product :=(product)(count)
count :=count-3
End-while
a. Value of product after first iteration = 2*7 = 14.
b. The variable count begins with 7 and gets decremented to 4 and then to 1 when the while loop does not execute.
The while loop executes 2 times.
c. Value of product after second iteration = 14 * 4 = 56.
Final value for product is 56.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.