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

Dani AI

Generated

already has the basic input and sum logic. 's compare-each-item idea is fine, and is right to warn against initializing max/min to a fixed value like 0 (that breaks for all-negative data). A reliable pattern: fill the array, set both min and max from the first element, then scan the remaining elements once—update min, max and the running total in the same loop. That single pass is simple and correct for negatives.

program FindMinMax;
uses crt;

const
  MAX = 5;

var
  marks: array[1..MAX] of Integer;
  i: Integer;
  total: LongInt;
  minVal, maxVal: Integer;
  avg: Real;

begin
  for i := 1 to MAX do
  begin
    write('Enter integer: ');
    readln(marks[i]);
  end;

  minVal := marks[1];
  maxVal := marks[1];
  total := marks[1];

  for i := 2 to MAX do
  begin
    total := total + marks[i];
    if marks[i] < minVal then minVal := marks[i];
    if marks[i] > maxVal then maxVal := marks[i];
  end;

  avg := total / MAX;  { use / not DIV for a real-average }
  writeln('Minimum: ', minVal);
  writeln('Maximum: ', maxVal);
  writeln('Average: ', avg:0:2);
  readln;
end.

Notes: use avg: Real and / to avoid integer truncation; verify MAX > 0 before referencing marks[1]; prefer LongInt/Int64 for total if many or very large inputs; remove extra stray begin..end blocks so the main program block nests correctly.

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.