How to find the maximum value in a multidimensional array with Pascal

1 Answer

0 votes
program FindMaxInMultiDimArray;

var
  arr: array[1..4, 1..3] of Real; // Define a 2D array (4 rows, 3 columns) of real numbers
  maxValue: Real;
  i, j: Integer;

begin
  // Initialize the 2D array with values
  arr[1, 1] := 1;     arr[1, 2] := 2;     arr[1, 3] :=  3.14;
  arr[2, 1] := 1;     arr[2, 2] := 1;     arr[2, 3] := 16.80;
  arr[3, 1] := 3;     arr[3, 2] := 5;     arr[3, 3] := 17.50;
  arr[4, 1] := 2;     arr[4, 2] := 4;     arr[4, 3] := 11.03;

  // Initialize maxValue to a very small number
  maxValue := -MaxInt; 

  // Traverse the array to find the maximum value
  for i := 1 to 4 do
    for j := 1 to 3 do
    begin
      if arr[i, j] > maxValue then
        maxValue := arr[i, j]; // Update maxValue
    end;

  WriteLn('The maximum value in the array is: ', maxValue:0:2);
end.




(*
run:

The maximum value in the array is: 17.50

*)

 



answered Apr 5, 2025 by avibootz
...