1. Given the following algorithm in pseudocode: assign x the value 4 assign y th
ID: 3556822 • Letter: 1
Question
1. Given the following algorithm in pseudocode:
assign x the value 4
assign y the value 7
assign product the value 0
while (x is greater than 0) do
(assign product the value product + y
assign x the value x - 1)
write down the values of x, y and product until the algorithm terminates:
x y product
2. The procedure find_largest() takes a list of numbers and prints the largest. Work
through the call to find_largest(), writing down the values of largest and next
until the procedure ends:
procedure find_largest (List)
assign largest the value of the first number in List
while (there is another number in List) do
(assign next the value of next number in List
if (next > largest) then
assign largest the value of next)
print the value of largest
example_list is: 7 2 3 3 9 1
apply procedure find_largest to example_list
largest next
Explanation / Answer
1.write down the values of x, y and product until the algorithm terminates:
Ans :
assign x the value 4
assign y the value 7
assign product the value 0
while (x is greater than 0) do //when x=0 it will terminate
(assign product the value product + y // value of y doesn't change
assign x the value x - 1)
product=7+7+7+7
x = 0
y=7
product =28
2.
procedure find_largest (List)
assign largest the value of the first number in List
while (there is another number in List) do
(assign next the value of next number in List //all numbers are assigned to x one by one
if (next > largest) then
assign largest the value of next) //largest changes if next is greater than largest
print the value of largest
example_list is: 7 2 3 3 9 1
answer
next =1 and largest =9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.