Computer Programming II (CS2500)
Final Review, Spring, 1997
These are some questions to help you review for the final. They are intended to help you study, but they will not replace studying the book and your notes.
function pow (x, n : integer) : integer;
var i, temp : integer;
begin
temp := 1;
for i := 1 to n do
temp := temp * x;
pow := temp
end
What is the approximate runtime of this function?
Program pow(output);
function mystery(x, y : integer):integer
var tmp : integer;
begin if (y = 0) then mystery := 1
else if (y = 1) then mystery := x
else begin tmp := mystery(x, y div 2);
if ((y mod 2) = 0) then mystery := tmp * tmp
else mystery := tmp * tmp * x
end end;
begin writeln('mystery(5,0) = ', mystery(5,0));
writeln('mystery(7,1) = ', mystery(7,1));
writeln('mystery(2,5) = ', mystery(2,5)) end.