Czech version
logolink

< Back to the list of lessons

Two-dimensional Array (2D Array)

AlgortimyContent of the lesson:

  • Principle of 2D Array
  • Writing 2D Array to Screen
  • Filling in 2D Array

Principle of 2D Array

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.

Writing 2D Array to Screen

Take a look at writing an empty 2D array to screen:

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.

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
  begin
    for sloupec:=1 to n do
      write(p[radek, sloupec],' ');
    writeln;
  end;
  readln;
end.

Filling in 2D Array

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.

The basic algorithm to fill an array with numbers
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.

Ones Everywhere

111
111
111

Ones everywhere
p[row, column]:=1;

Numbers 1..n in Rows

123
123
123

Numbers 1..n in rows
p[row, column]:=column;

Numbers 1..n in Columns

111
222
333

Numbers 1..n in columns
p[row, column]:=row;

Small Multiplying Table

123
246
369

Small Multiplying Table
p[row, column]:=row*column;

Ones on the Diagonal Line

100
010
001

Ones on the Diagonal Line
if row=column then
   p[row, column]:=1
else
   p[row, column]:=0

Ones on the Diagonal Line and Above It

111
011
001

Ones on the Diagonal Line and Above It
if row<=column then
   p[row, column]:=1
else
   p[row, column]:=0

Ones on the Both Diagonal Lines

101
010
101

Ones on the Both Diagonal Lines
// 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

Individual Task I

Write a program to compute the arithmetic mean of all inserted values inside a 2D array.

Individual Task II

Write a program to find out the minimum and the maximum values from all inserted values in a 2D array.

Questions

  1. Name several suitable situations when 2D arrays can be used.
webdesign, xhtml, css, php - Mgr. Michal Mikláš