0

hey, im really struggling displaying the maximum value in this array, i have managed to get the average and etc but getting it to display the maximum and minimum value in the array is proving to be hard can anyone help?
heres what i have so far


Program arrayz;
uses crt;

Const
MAX = 5;

VAR
marks : array [1..MAX] of integer;
loop : integer;
answer : integer;
average : integer;

begin
answer := 0;
average:= 0;
for loop := 1 to MAX do
begin
write('enter an integer :');
readln (marks [loop]);
end;

for loop := MAX downto 1 do
begin
Writeln('the numbers entered are ', marks [loop]);
end;
delay (2000);

for loop := 1 to max do
begin
answer := answer + marks[loop];
end;

begin
writeln ('the result of adding them together is: = ',answer, ' now press enter');
readln;
end;

begin
average := answer DIV MAX;
writeln ('the average is: ',average);
readln;
end;
end.


thanks in advance for any help

3
Contributors
2
Replies
4
Views
9 Years
Discussion Span
Last Post by philhodgkins
0

chkmark:=0;
for loop := 1 to MAX do
begin
if marks[loop] > chkmark then chkmark := marks[loop];
end;

writeln('best mark is:', chkmark);

something like that

0
chkmark:=marks[1]; //Initialise using the first item in the array
for loop := 2 to MAX do  //Start loop with 2nd item in array
begin
if marks[loop] > chkmark then chkmark := marks[loop];
end;

writeln('best mark is:', chkmark);

This ALWAYS works, the original answer would not work with an array containing all negative values. It is also slightly more efficient, Doing the same when searching for the minimum will also always work.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.