# Lesson #16. Matrices

## Theory: Two-dimensional arrays

Lecture in pdf format

• Definition
• 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).

• Matrix declaration
• var a2: array [,] of integer; a2 := new integer[3,4]; // or: var a := new integer[3,4];
• Loop over the elements and pronting out:
• 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

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.
• Filling with random numbers
• 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.

Lab 0. Two-dimensional array

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)


Expected output:

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:

1. 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 and 11).
2. Output the values of the temperature of all weather stations during the 2nd day (for loop is needed).
3. Output the values of the temperature of all weather stations during all days.
4. Calculate the average temperature at the 3rd weather station (for loop is needed).
5. Output the days and the weather stations’ numbers where the temperature was in the range of 2426 degrees Celsius.

Expected output:

1-st task: 20  and  11
-8  -14  -19  -18
25   28   26   20
11   18   20   25

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: Create a two-dimensional array 4-by-3 ([4,3]) with randomized integer values. Count positive elements within the array.

Expected output:

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: Create a two-dimensional array 4-by-5 ([4,5]) with randomized integer values. Create a function to find 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);

Expected output:

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]

Lab 1

To do: Create a two-dimensional array 3-by-4 ([3,4]) using the rule:
each element equals to i-th row * j-th column + 0.5
Expected output:

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: Create a two-dimensional array 4-by-5 ([4,5]) using the rule: all elements of the i-th row have the value 10 ∗ i. To comlete the task you should use MatrGen standard function.

Expected output:

    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]

1. Open taskBook of pascalAbc.net IDE:
2. Choose ZMatrix group in the dropdown menu and click html-page button:
3. Complete the tasks giving the files the proper names (ZMatrix1.pas etc.). Tasks #: 1, 2, 7, 8, 9, 10, 11, 12, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 18, 29, 30
4. Or you can open pdf-file with all the tasks to translate them.
Lab 2

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: Create a two-dimensional array 4-by-5 ([4,5]) with randomized integer values. Find maximum element in each row.

Expected output:

  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: Create a two-dimensional array 4-by-5 ([4,5]) with randomized integer values in the [0,10] range. Create a function to return the array of the products (multiplications) in each column.

Expected output:

   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]

Lab 3

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: Create a two-dimensional array 4-by-5 ([4,5]) with randomized integer values. Create a procedure to print out the array of the quantities of elements in the range [0,10] in each row.

Note: the signature of the procedure must be as follows:

procedure arrCount(a:array[,] of integer);

Expected output:

  59   6  34  70
11  28  10  31
8   0   9  80
1 1 3


[Program name: 15task-07.pas]

Lab 4

To do: The matrix is given. Does the matrix contain the element equaled to x? x is entered. You should output boolean value.

The result examples:

   3   8   5   7
1   7   6   1
1   1   5   9
>> 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: Create a two-dimensional array 3-by-4 ([3,4]) with randomized 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);

Expected output:

   1   2   2  10
3   4   7   5
3   5   5  10
(-1,-1)
---
3   6   7   1
7   1   8   5
10   2   4   6
(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 randomized numbers). Find the number of its first row, which contains 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)

Expected output:

N:
>>3
M:
>>4
-5  -5   5  -1
1  -1   0   0
3  -3  -4   1

result: 2


[Program name: 15task-09.pas]