# Working with two-dimensional arrays on Pascal Programming

A two-dimensional array in a Pascal (matrix) is a table that contains several one-dimensional arrays. The position of the elements in the two-dimensional array is indicated by two indexes.

Example of a 3*3 matrix (contains 3 rows and 3 columns):

Each of the elements has two indexes: the first is the row number where the element is located, the second is the column number. Accordingly, the index of an item is determined by where the column and row intersect. For example, a13 is an element that is located in the third column and the first row of the array.

## Description of a two-dimensional array in Pascal

There are several methods for declaring a two-dimensional array. Here is an example where the element type and variables are specified:

Type

Vector = array [1..9] of<type of elements>;

Matrix= array [1..4] ofvector;

Varmas: matrix;

In this case, the mas matrix includes 3 rows, each of which contains 9 columns. In this case, through mas[ i ], and j can refer to any i -th line, as well as to the j -m element within the i line – m [ i , j ]. In the latter two ways, the matrix can be specified in one row.

Type

Matrix= array [1..4] ofarray [1..9] of< element type >;

or even simpler:

type

matrix = array [1..4, 1..9] of<type of elements>;

As in the previous example, a two-dimensional array consists of 4 rows and 9 columns, a reference to any element of the array is as follows: mas[ i , j ]. It means that the element is in the j -th column and the i -th row.

Attention! Do not confuse rows with columns, otherwise there will be an error in the response.

## Common actions with two-dimensional Pascal arrays

The main actions on the matrices are carried out element-by-element, while their data types should be the same. In other words, if a two-dimensional array consists of numbers, then actions can only be performed on numbers. But to implement the mass assignment operation, you must be of the same size. For example, the following array is given:

type

matrix= array [1..4, 1..9] ofinteger;

var

a , b : matrix ;

when executing such a program, the matrix a can be assigned the values of the two-dimensional array b ( a := b ).

## Entering a two-dimensional Pascal array

To alternately output elements to a two-dimensional array, you need to iterate through the elements from the first column of the first row to the last column and the last row. You can do this by using two for loop operators (one nested within the other).

Example of matrix input from the keyboard:

type

matrix= array [1..4, 1..9] ofinteger;

var

a, :matrix;

i, j: integer; { array indexes }

begin

fori :=1 to 4 do {loop to iterate through all rows}

forj :=1 to 9 do {loop iterant cycle of row elements by columns}

readln( a [ i , j ]); {keyboard input of an element in the i -th row and j -m column}

The choice of how to populate a two-dimensional array depends on the specific task. For example, the random (N) function allows you to fill a two-dimensional array with random variables a[i,j]:=random(25)-10. There are tasks that require the content of expressions in the matrix. Be that as it may, each element must be defined in columns and rows.

## Displays a two-dimensional Pascal array on the screen.

Items in the output should print in order of indexes. In other words, in rows, elements should stand one after another, whereas in columns one below the other. To do this, you need to write out the following code elements:

fori :=1 to 4 do {loop to iterate through all rows}

begin

forj :=1 to 9 do {loop iteration of row elements by columns}

write( a [ i , j ] :3); {print elements standing in the i -th row of the matrix in one screen row, while 3 positions are allocated for the output of each element}

writeln ;

end ;

**Importantly!** If you use the readln statements ( a [ i , j ]), writeln in another form, the compiler will not count or print the element. In this case, entering statements in this form readln (a), writeln (a) into the program is unacceptable, because it is a variable of type array.

## Representation of a two-dimensional Pascal array in memory

The elements of the matrix are located in the matrix of the computer sequentially and weigh several bytes. For example, the elements of an array of type integer will take 2 bytes each. In this case, the entire array will take up S^2 bytes (S denotes the number of elements in the matrix).

In a two-dimensional array, each element of type integer requires 2 bytes of memory. Now here's an example:

Matrix = array [1..4, 1..3] ofinteger ;

In this case, 24 bytes of memory will be required.

## Example model for placing an array M type matrix in memory

For each element, two memory locations are offered: the location runs from the first row to the bottom, in the order in which the index changes.

A correspondence is established between the address of the cell and the variable, but when declaring a two-dimensional array, the program knows only the address of the beginning of the array, to other elements the address is calculated according to the following formula:

Addres + SizeElemt * sum*( I -1)+ SizeElem *( J -1),

Where Addres is the location of the first element allocated to the matrix; I , J – indexes of the element in the array; SizeElemt – the size of the matrix element (for example, 2 bytes for elements of the integer type); sum – the number of elements in the row.

SizeElemt * sum*( I -1) + SizeElemt *( J -1) is the offset relative to the beginning of the array.

## How much memory is allocated to the array?

For the normal operation of the program, the computer allocates memory, dividing into segments of 64 KB. One of the segments goes for the data processed by the program. Each variable has its own segment. For example, if a variable consists of an array, it will not take up more than 65536 bytes.

Of course, there may be other variables in a segment in addition to the array, so the following formula is used to calculate the amount of memory:

65536- S , where S is the size of the memory allocated to other variables.

Here's an example:

TypemyArray= array [1..50000] ofinteger;

In terms of syntax, this entry is correct, but the compiler will throw an error because the array is too long.

You can easily calculate the number of elements allowed by the formula: 65536/2 –1 = 32767. There should be no other variables. Matrices have even smaller index limits.

## Consider a problem with a two-dimensional Pascal array

Objective: To compute the product of the non-zero elements of a two-dimensional array.

Solution: First of all, you need to set the variables: a matrix of integer elements: P is the product of elements, not equal to 0; N , M is the number of rows and columns in the array, I , J are the array indexes.

- The input data N, M are entered from the keyboard, and the matrix is set with random ().
- The output parameter is P.
- Display the matrix on the screen to check the operability of the program.

Let's look at the procedures.

**Attention!** The type of the array must be determined in advance. For example:

Type

Matrix=array [1..10, 1..10] ofinteger;

..............................

procedureprimer (a: matrix);

..............................

In order for the input matrix to be transferred to the program as a result, it is worth resorting to the vvod procedure. In this case, the matrix will be passed by reference. The procedure will look like this:

Procedurevvod( var m : matrix );

Print – procedures for displaying a matrix on the screen, which is transmitted by value.

Procedureprint( m : matrix );

To implement nested loops inside the procedure, you must specify the k and h counters. Use the above description to display the matrix on the screen.

The status of the programme will be as follows:

- Enter N and M values;
- Refer to the procedures vvod ( a ) and print ( a ) for input and output of the matrix, where a is the matrix;
- The variable responsible for the product P is assigned a value of 1;
- Alternately iterate through the elements of the two-dimensional array with an index of 11 to an element with an index Each element must satisfy the condition: if a ij ? 0, then the product P is multiplied by the element a ij ( P = P * a ij );
- The result of the product of the zero elements of the matrix - P is displayed on the screen.

Programproizvedenie;

Type

Matrix=array [1..10, 1..10] ofinteger;

Var

a: matrix;

n, m, i, j: byte;

P: integer;

Procedurevvod (var m: matrix);

Vark , h : byte ;

Begin

Fori :=1 to n do

Forj :=1 to m do

M[i,j]:= random(10);

End;

Procedureprint (m: matrix);

Var k, h: byte;

Begin

Fori:=1 to n do

begin

Forj:=1 to m do

Write (M[i, j]: 4);

Writeln;

end ;

End ;

Begin {start of the main program}

Writeln ('Enter the dimension of the matrix:');

Readln(N, M);

Vvod(a);

Print(a);

P:=1;

Fori:=1 to N do

Forj:=1 to M do

Ifa[i, j]<>0 then p:=p*a[i, j];

Writeln( p );

End .

Have you mastered two-dimensional Pascal arrays? Leave a comment to share your experience.