In the following algorithm, how many times is the line "z := z + 1" executed if n = 4 and z is an integer?
procedure algor(z,n)
i = 1
while i <= 4 do
begin
z = z +1
n = n – 2
if n > 0 then
algor(z,n)
i = 1 + 1
end
return(z)
end algor
Thanks for the help.
Pwr
The loop is executed exactly four times and the line in question executed once per trip around the loop with n decremented by 2 each time.
The line "if n > 0 then" is arrived at with n=2, 0, -2, -4 and so "algor(z,n)" is executed once with n = 2, and in executing this with n=2 the line in question is executed twice, and the line "algor(z,n)" is never reached.
Hence the line in question is executed 6 times.
RonL