Content of the lesson:
A 2D array uses another dimension compared to a standard array. You can see an illustration of an empty 2D array (5x5):
A perfect analogy is a list of squared paper with dimensions m x n squares.
Take a look at writing an empty 2D array to screen:
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
const m=2; n=4;
type pole2d = array[1..m,1..n]of integer;
var p:pole2d;
radek, sloupec:integer;
begin
for radek:=1 to m do
for sloupec:=1 to n do
write(p[radek, sloupec],' ');
readln;
end.
Numbers are written all together and you absolutely cannot see that this is a 2D array. To illustrate this fact you have to use another writeln command after writing every line to console.
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
const m=2; n=4;
type pole2d = array[1..m,1..n]of integer;
var p:pole2d;
radek, sloupec:integer;
begin
for radek:=1 to m do
begin
for sloupec:=1 to n do
write(p[radek, sloupec],' ');
writeln;
end;
readln;
end.
In the following examples we will try to fill a square 2D array (dimensions set to nxn) with numbers. To be able to write it to screen, choose the dimensions to 10x10 items.
program Project2;
{$APPTYPE CONSOLE}
uses
SysUtils;
const m=10; n=10;
type pole2d = array[1..m,1..n]of integer;
var p:pole2d;
radek, sloupec:integer;
begin
for radek:=1 to m do
begin
for sloupec:=1 to n do
begin
end;
end;
for radek:=1 to m do
begin
for sloupec:=1 to n do
write(p[radek, sloupec]:3,' ');
writeln;
end;
readln;
end.
1 | 1 | 1 |
1 | 1 | 1 |
1 | 1 | 1 |
p[row, column]:=1;
1 | 2 | 3 |
1 | 2 | 3 |
1 | 2 | 3 |
p[row, column]:=column;
1 | 1 | 1 |
2 | 2 | 2 |
3 | 3 | 3 |
p[row, column]:=row;
1 | 2 | 3 |
2 | 4 | 6 |
3 | 6 | 9 |
p[row, column]:=row*column;
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 1 |
if row=column then
p[row, column]:=1
else
p[row, column]:=0
1 | 1 | 1 |
0 | 1 | 1 |
0 | 0 | 1 |
if row<=column then
p[row, column]:=1
else
p[row, column]:=0
1 | 0 | 1 |
0 | 1 | 0 |
1 | 0 | 1 |
// if (row=column) or (row+column=11) then
// if (row=column) or (row+column=n+1) then
if (row=column) or (row=m-column+1) then
p[row, column]:=1
else
p[row, column]:=0
Write a program to compute the arithmetic mean of all inserted values inside a 2D array.
Write a program to find out the minimum and the maximum values from all inserted values in a 2D array.