% Exchange the maximum and minimum in a list.
const small := -1000000
const large :=  1000000
const     N := 10
var i: int  := 0
var maximum: real := small
var minimum: real := large
var imin, imax: int := 1	% indexes of min and max
var foo: real			% a dummy variable
var numbers : array 1 .. N of real

put "Enter a list of numbers and I'll find the largest number"
loop
	get skip
	exit when eof or i >= N
	i := i + 1
	get numbers(i)
	if numbers(i) > maximum then
		maximum := numbers(i)
		imax    := i
	end if
	if numbers(i) < minimum then
		minimum := numbers(i)
		imin    := i
	end if
end loop

if i = 0 then
	put "Hey!  You didn't enter anything."
else
	if i >= N then
		put "We ran out of room to store all the numbers."
		put "The result is only based on the first ", N, " of them."
	end if
	put "The maximum is ", maximum
	put "The minimum is ", minimum
	foo := numbers(imin)
	numbers(imin) := numbers(imax)
	numbers(imax) := foo
	for j: 1..i
		put numbers(j)
	end for
end if

