program sortstudents(input,output); {Input student records, sort on ID and output the records.} const Maxarray = 50; type IDType = 1..2500; String30 = String[30]; NameType = String30; StuRecType = record ID : IDType; Name : NameType end; StuRange = 1..Maxarray; StuArray = array [StuRange] of StuRecType; SortRange = StuRange; TestRange = SortRange; SortElt = StuRecType; SortArrayType = array[SortRange] of SortElt; TestArrayType = SortArrayType; var SSub, NumStu : StuRange; SArray : StuRecType; Sort : SortArrayType; I, Size : SortRange; {counter, and the array size} Sentinel : IDType; procedure ReadStu (var FStu {output} : StuRecType); { Reads one student record into FStu. Pre : None Post: Data are read into record FStu. } begin {ReadStu} with FStu do begin Write ('ID> '); ReadLn (ID); Write ('Name> '); ReadLn (Name); end {with} end; {ReadStu} procedure WriteStu( FStu {input} : StuRecType ); begin with FStu do begin Write ('ID = ', ID :4); WriteLn (' Name: ', Name); WriteLn end; {with FStu} end; {WriteStu} function SmallPos(Lo,Hi : TestRange; FArray : TestArrayType) : TestRange; {returns position of smallest element between positions lo and hi} var TestPos, Small : TestRange; begin Small := Lo; for TestPos := Lo to Hi do if FArray[TestPos].ID < FArray[Small].ID then Small := TestPos; SmallPos := Small end;{SmallPos} procedure SelSort(var FSort : SortArrayType; LoPos, HiPos : SortRange); {ascending order selection sort of array FSort from LoPos to HiPos} var Pos, SP : SortRange; Temp : SortElt; begin for Pos := LoPos to Pred(HiPos) do begin Temp := FSort[Pos]; SP := SmallPos(Pos,HiPos,FSort); FSort[Pos] := FSort[SP]; FSort[SP] := Temp; end;{for J} end;{SelSort} begin{main} WriteLn('Input a sentinel value (ID) to terminate input'); ReadLn(Sentinel); WriteLn('Input Students, ending with ',Sentinel :4,' as ID - at most ',Maxarray); WriteLn; WriteLn; I := 1; repeat ReadStu(Sort[I]); I := I + 1; until Sort[I-1].ID = Sentinel; Size := I - 2; SelSort(Sort,1,Size); for I := 1 to Size do WriteStu(Sort[I]); WriteLn; end.{main}