SE 504   Spring 2009
HW #8: More Repetition, Again
Due: Thursday, April 23

1. Modify the program developed in class for the Maximum Segment Sum problem (see the relevant web page) so that, in addition to the variable z, there are two other "output" variables, left and right, which (when the program completes execution) satisfy the condition that A[left..right) is a "leftmost" segment having maximum sum (meaning that no segment beginning "to the left" of location left has as large a sum).

It is important not only to modify the program correctly but also to modify the loop invariant properly (i.e., so that item (ii) on the loop checklist is provable).

For extra credit, also show a version of the program that terminates with left and right satisfying the condition that A[left..right) is a shortest segment with the maximum sum and leftmost among all such segments. (In other words, any segment shorter than A[left..right] must have a smaller sum and any other equal-length segment with maximum sum must begin "to the right" of location left.


2. Derive (and provide an accompanying narrative) a solution to the following programming problem:
|[ con A : array of int;  { #A > 0 }
   var r : int;

   S

   {Q: r = (MAX p,q | 0≤p≤q<#A : A.p - A.q) }
]|

The methods used in deriving the program for the Maximum Segment Sum problem will be useful here.