Last modified: Fri Feb 10 11:51:42 1995
Other versions:
Given a sequence of nonnegative integers (you may choose the representation of this sequence as an array, linked list, file of characters, etc.) find the largest. Produce two different programs, one based on the iteration
and the other based on the recursionbiggest := 0; start before the 1st list element; While there is more in the list do advance the list, assigning nextint the next integer value in the list; biggest := max(nextint, biggest) end while
Make the following 3 separate and independent modifications, in each case choosing the more appropriate of the two programs above as your starting point.maxlist(list) = if (list = nil) then 0 else max(first(list), maxlist(rest(list)))
using assignments, while-loops, and conditionals. You may assume that E1 and E2 are pure integer expressions with no side effects, and that SL does not change the value of i. But, you must insure that your code will execute successfully under all reasonable circumstances. In particular, if E1 > E2, the for-loop must be equivalent to doing nothing. And, you must never assign a value to i that is less than E1 or greater than E2, because that might cause an overflow if the loop is running right up to the limits of the range of i. This problem is taken from problem 3.8 on p. 112 of the text, but you need not use Modula-2 in the solution. No program execution is required, just paperwork.for i := E1 to E2 do SL
<alpha>; If A then go to l; <beta>; l: <gamma>
<alpha>; l: <beta>; If A then go to m; <gamma>; If B then go to l; m: <delta>
<alpha>; While A do <beta>; If B then go to l; <gamma> end while; <delta>; l: <epsilon>