Theory: Two-dimensional arrays
Lection in pdf format
- Definition
- Matrix declaration
- Loop over the elements and pronting out:
A matrix or two-dimensional array is a rectangular table of numbers (or other elements of the same type). Each element of the matrix has two indices (row number and column number).
var a2: array [,] of integer; a2 := new integer[3,4]; // or: var a := new integer[3,4]; |
for var i:=0 to a2.RowCount-1 do for var j:=0 to a2.ColCount-1 do a2[i,j] := i + j; a2.Println; Println(a2); // [[0,1,2,3],[1,2,3,4],[2,3,4,5]] |
Matrix filling
- Filling with random numbers
begin var a := Matr(3,4,1,2,3,4,5,6,7,8,9,10,11,12); a.Println(3); var a1 := MatrGen(3,4,(i,j)->i+j+0.5); // using arrow operator a1.Println(5,1); end. |
begin var a := MatrRandomInteger(3,4); a.Println(4); var a1 := MatrRandomReal(3,4,1,9); a1.Println(6,2); end. |
Operations by rows and columns
begin var a := MatrRandomInteger(3,4); a.Println; a.Row(0).Sum.Println; a.Row(1).Average.Println; a.Row(2).Product.Println; a.Col(0).Min.Println; a.Col(1).Max.Println; end. |
Labs and tasks
To do: Initialize a two-dimensional array named arr2d
with given integer values, they are [[1, 2], [3, 4], [5, 6]]
.
1) First, output all the elements of the array.
2) After, output the following elements to the Output window (the elements to output are marked in red color):
(1, 2) (3, 4) (5, 6)
The result examples:
The array: 1 2 3 4 5 6 1-st element = 3, 2-nd element = 5
✍ Algorithm:
begin var a := new integer[3,2]; for var i:=0 to a.RowCount-1 do for var j:=0 to a.ColCount-1 do a[i,j]:=readinteger; println('The array:'); a.Println(); Println(a[1,0], a[2,0]); end. |
{0.6 points}
Task 1:
To do: The air temperature values for 4 days are given, they are taken from three weather stations located in different regions of the country:
Station number | 1-st day | 2-nd day | 3-d day | 4-th day |
---|---|---|---|---|
1 | -8 | -14 | -19 | -18 |
2 | 25 | 28 | 26 | 20 |
3 | 11 | 18 | 20 | 25 |
That is, in a two-dimensional array it would look like this:
t[0,0]=-8; | t[0,1]=-14; | t[0,2]=-19; | t[0,3]=-18; |
t[1,0]=25; | t[1,1]=28; | t[1,2]=26; | t[1,3]=20; |
t[2,0]=11; | t[2,1]=18; | t[2,2]=20; | t[2,3]=25; |
or it is better to use matr
function:
var t := matr(3,4,-8,-14,-19,-18,25,28,26,20,11,18,20,25); |
To do:
- Output value of the temperature at the 2nd weather station during the 4th day and at the 3rd weather station during the 1st day (your result must be
20
and11
). - Output the values of the temperature of all weather stations during the 2nd day (
for
loop is needed). - Output the values of the temperature of all weather stations during all days.
- Calculate the average temperature at the 3rd weather station (
for
loop is needed). - Output the days and the weather stations’ numbers where the temperature was in the range of 24—26 degrees Celsius.
The resulting example:
1-st task: 20 and 11 2-nd task: -14 28 18 3-d task: -8 -14 -19 -18 25 28 26 20 11 18 20 25 4-th task: 18.5 5-th task: station number 1 day 0 station number 1 day 2 station number 2 day
[Program name: 15task-01.pas
]
{0.2 points}
Task 2:
To do: Fill a two-dimensional array 4-by-3 ([4,3]) with random integer values. Count positive elements within the array.
The resulting example:
The array: 2 5 -1 6 7 8 1 8 6 -3 1 -6 number of positive = 9
[Program name: 15task-02.pas
]
{0.5 points}
Task 3:
To do: Fill a two-dimensional array 4-by-5 ([4,5]) with random integer values. Create a function to find a minimum and maximum elements of the array (FindMinMaxArr
).
Note: the signature (header) of the function should be as follows:
function FindMinMaxArr(a: array [,] of integer): (integer, integer); |
The resulting example:
The array: 10 2 9 3 4 -3 -10 -14 -4 2 2 -9 11 3 -10 -1 -13 -5 -2 3 min and max: -14 11
[Program name: 15task-03.pas
]
To do: Fill a two-dimensional array 3-by-4 ([3,4]) using the rule:
each element equals to i-th row * j-th column + 0.5
The result examples:
0.5 1.5 2.5 3.5 1.5 2.5 3.5 4.5 2.5 3.5 4.5 5.5
✍ Algorithm:
begin var a1 := MatrGen(3,4,(i,j)->i+j+0.5); a1.Println(5,1); end. |
{0.2 points}
Task 4:
To do: Fill a two-dimensional array 4-by-5 ([4,5]) using the rule: all elements of the i
-th row have the value 10 ∗ i
.
The resulting example:
0 0 0 0 0 10 10 10 10 10 20 20 20 20 20 30 30 30 30 30
[Program name: 15task-04.pas
]
To do: Calculate sum in each row.
The result examples:
15 21 9 78 54 61 2 19 19 28 50 61 123 136 158
✍ Algorithm:
begin var a := MatrRandomInteger(3,4); a.Println; var Sums := ArrGen(a.RowCount,r -> a.Row(r).Sum); Sums.Println; end. |
{0.2 points}
Task 5:
To do: Fill a two-dimensional array 4-by-5 ([4,5]) with random integer values. Calculate maximum element in each row.
The resulting example:
91 9 82 71 99 59 30 36 35 26 56 66 76 7 23 25 46 92 39 77 99 59 76 92
[Program name: 15task-05.pas
]
{0.3 points}
Task 6:
To do: Fill a two-dimensional array 4-by-5 ([4,5]) with random integer values in the range [0,10]. Create a function to return the array of the products (multiplications) in each column.
The resulting example:
6 4 6 8 0 6 4 5 3 7 0 3 5 2 8 6 4 6 3 8 result: [0,192,900,144,0]
[Program name: 15task-06.pas
]
To do: Calculate a number of evens in each row.
The result examples:
49 98 78 27 44 65 50 74 68 1 75 59 2 1 2 1
✍ Algorithm:
begin var a := MatrRandomInteger(3,4); a.Println; var EvensCount := ArrGen(a.ColCount,c -> a.Col(c).Count(x->x.IsEven)); EvensCount.Println; end. |
{0.3 points}
Task 7:
To do: Fill a two-dimensional array 4-by-5 ([4,5]) with random integer values. Create a procedure to print out the array of the quantities of the matrix elements in the range [0,10] by rows.
Note: the signature of the procedure must be as follows:
procedure arrCount(a:array[,] of integer); |
The resulting example:
59 6 34 70 11 28 10 31 8 0 9 80 1 1 3
[Program name: 15task-07.pas
]
To do: The matrix is given. Does the matrix contain the element equaled to x
? x
is entered.
The result examples:
3 8 5 7 1 7 6 1 1 1 5 9 enter x, please: >> 6 True
✍ Algorithm:
function Contains<T>(a: array [,] of T; x: T): boolean; begin Result := False; for var i:=0 to a.RowCount-1 do for var j:=0 to a.ColCount-1 do if a[i,j]=x then begin Result := True; exit; end; end; begin var a := MatrRandomInteger(3,4,1,10); a.Println; var x:=readinteger('enter x, please:'); var found := Contains(a,x); Println(found); end. |
{0.3 points}
Task 8:
To do: Fill a two-dimensional array 3-by-4 ([3,4]) with random integer values. Create a function to print out the indices (row and column) of the array element equaled to entered x
value. If x is not found, the function must return -1,-1
.
Note: the signature of the function must be as follows:
function indicesArr<T>(a: array [,] of T; x: T): (integer,integer); |
The resulting example:
1 2 2 10 3 4 7 5 3 5 5 10 enter x, please: 6 (-1,-1) --- 3 6 7 1 7 1 8 5 10 2 4 6 enter x, please: 7 (0,2)
[Program name: 15task-08.pas
]
{0.8 points}
Task 9:
To do: M
-by-N
integer matrix is given (it is filled with generated random numbers). Find the number of the first of its rows containing an equal quantity of positive and negative elements (zero matrix elements are not taken into account). If there are no such rows, then output 0
.
Note: you can do the task using one of the following ways: 1) iterating over the elements of the array; 2) using mass operations by rows and columns (.Row
)
The resulting example:
N:
>>3
M:
>>4
-5 -5 5 -1
1 -1 0 0
3 -3 -4 1
result: 2
[Program name: 15task-09.pas
]