Hamza Bounezra - 7 months ago 124

Pascal Question

I need to create a program that shows a square matrix (array) with 20 lines and 20 columns and then gives you the maximum and the minimum of this matrix.

I started with this code:

`program ex2dimarray;`

var

a: array [0..20, 0..20] of integer;

i, j: integer;

begin

for i := 0 to 3 do

for j := 0 to 3 do

a[i,j] := i * j;

for i := 0 to 3 do

begin

for j := 0 to 3 do

write(a[i,j]:2, ' ');

writeln;

end;

end.

Answer

You declare an array of 20x20 but in your code you are using only a 3x3 block. Besides, the logic for finding the max and the min values is missing.

You can try something like this:

```
program ex2dimarray;
const
Dim = 20;
var
a: array [0..Dim, 0..Dim] of integer;
i, j, min, max: integer;
begin
// initialize the array
for i := 0 to Dim do
begin
for j := 0 to Dim do
a[i,j] := i * j;
end;
// search for max and min. You can initialize both to a[0,0]
min:= maxint;
max:= -maxint;
for i := 0 to Dim do
begin
for j := 0 to Dim do
begin
if (a[i,j] > max) then
max := a[i,j];
if (a[i,j] < min) then
min := a[i,j];
end;
end;
// print out the values
for i := 0 to Dim do
begin
for j := 0 to Dim do
write(a[i,j]:3, ' ');
writeln;
end;
writeln;
write('Max: ', max, ' Min: ', min);
writeln;
readln;
end.
```