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

Recommended Answers

All 2 Replies

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.