# Algorithm Trace

• May 22nd 2007, 05:03 PM
pwr_hngry
Algorithm Trace
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
• May 22nd 2007, 05:08 PM
Plato
Quote:

Originally Posted by pwr_hngry
In the following algorithm, how many times is the line "z := z + 1" executed if n = 4 and z is an integer?
http://kucourses.com/ec/courses/1928.../l5q10img1.gif
Thanks for the help.
Pwr

As shown, the answer is zero.
There is no n in the problem.
• May 22nd 2007, 05:33 PM
pwr_hngry
Sorry about that. For some reason the image showed up in the Preview but never made it to the final post. Sorry.

Pwr
• May 22nd 2007, 07:38 PM
CaptainBlack
Quote:

Originally Posted by pwr_hngry
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