program avg;
{$modeswitch result}
{$modeswitch out}
uses
SysUtils;
type
TMark = 0..10; //Allowed range for a mark
TIndex = 1..10; //Allowed range for the Scores array
Var
Scores: Array[TIndex] of integer;
Index: TIndex; //Index for the array
CurrentMark: TMark; //Current Mark
NrOfMarks: integer; //Amount of marks in the array, can be zero, so not of type TIndex
MinMark: TMark; //Minimum Mark
MaxMark: TMark; //Maximum Mark
Sum: Integer; //Sum of all Marks
Av: Double; //Average Mark
function GetMarkForIndex(const Index: TIndex; out AMark: TMark): Boolean;
var
Ans: String;
Value: LongInt;
begin
Result := False;
repeat
write(Format('Please write the mark no. %d (Q to stop): ',[Index]));
readln(Ans);
Ans := UpperCase(Ans);
if (Ans = 'Q') then Exit;
Value := StrToIntDef(Ans, -1);
Result := Value in [Low(TMark)..High(TMark)];
if not Result then writeln(Format('"%s" is not a valid mark, please try again.',[Ans]));
until Result;
AMark := Value;
end;
Begin
NrOfMarks := 0;
for Index := Low(TIndex) to High(TIndex) do
begin
if GetMarkForIndex(Index, CurrentMark) then
begin
Scores[Index] := CurrentMark;
Inc(NrOfMarks);
end
else
Break;
end;
if (NrOfMarks > 0) then
begin
MinMark := High(TMark);
MaxMark := Low(TMark);
Sum := 0;
for Index := 1 to NrOfMarks do
begin
Sum := Sum + Scores[Index];
if (Scores[Index] > MaxMark) then
MaxMark := Scores[Index];
if (Scores[Index] < MinMark) then
MinMark := Scores[Index];
end;
Av := Sum / NrOfMarks;
writeln(Format('The average mark is: %.1f',[Av]));
writeln(Format('The highest mark is: %d',[MaxMark]));
writeln(Format('The lowest mark is : %d ',[MinMark]));
end
else
writeln('Array is empty ...');
end.