Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lecture 1.docx
Скачиваний:
1
Добавлен:
01.07.2025
Размер:
704 Кб
Скачать
☆

3.3. Arrays

Data in a program are often grouped in sets which are treated as logical units, although they are composed of a number of elements. Such a set should have a single name (because it designates a logical unit). Its components should be accessible using this name plus some other information identifying a particular element in the set. Special cases of such sets are arrays.

Arrays are sets of data elements. Each element is stored at the specified position in an array and is randomly accessible (without the need for looking over the rest of the array) by using the name of the array and index (position) of this element in the array.

Assume we want to use the set of names for days of the week. Let us call this set - the array - days. The consecutive elements of this array are numbered with integer indices (one corresponding to Monday, two corresponding to Tuesday and so on). Yet we need some syntactic facility which allows to denote individual elements of an array. In our simplified (based on the REXX) language - as in Java, C and C++ - square brackets with an integer index placed in between are used to access elements of an array (note that this is a modification of the original REXX syntax, required for the purpose of this lecture only). So we can write: days[1] = "Monday"; days[2] = "Tuesday"; days[3] = "Wednesday"; days[4] = "Thursday"; days[5] = "Friday"; days[6] = "Saturday"; days[7] = "Sunday"; Array elements may be treated as variables. To these variables the given values are assigned. This is not the only way of setting values of the array elements. Arrays may be initialized easier with the help of special syntactic facilities. In the simplified REXX the initialization of arrays is done (as in Java) with curly brackets: days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

Note: in simplified REXX such an array initializer must occupy a single line of code and is suitable for one-dimensional arrays only. One has to remember to put the empty braces [] after the array's name.

This notation has exactly the same meaning as the previous one: to the element of array days, under the position 1 the value "Monday" is assigned. To the element under position 2 - the value "Tuesday", and so on. Of course, besides setting the value of the array element, one can get the value of an element as well. The following program displays the current day of week using the built-in function date() for this purpose.

/* Displays the current day of the week */

days[] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

dayc = date('B') /* number of days since 1 of January 0001 */

dayNum = dayc // 7 + 1

say 'Today is :' days[dayNum]

The built-in function date called with the 'B' argument returns the number of days that elapsed since the 1 of January 0001 (Gregorian calendar) until yesterday inclusive. Just because on 01.01.0001 was Sunday, the remainder of division of the returned number of days by 7 gives: 0 for Mondays, 1 for Tuesdays, ..., 6 for Sundays. Adding 1 to this result we get a suitable index in the array. Write down and run this program on your computer It is possible to use multidimensional arrays. In our simplified REXX such arrays are accessed in the following way:                 a[i][j][k]... where consecutive pairs of square brackets are used to index corresponding dimension. Typical example of a two-dimensional array is a matrix (a table of numbers organized in rows and columns). The next program multiplies square matrices. The element in row i and column j of matrix C, which is the product of matrices A and B, is the sum of products of elements in row i of the A matrix by elements in column j of the B matrix.

/* Multiplication of square matrices */

N = 2;

a[1][1] = 1; b[1][1] = 2;

a[1][2] = 2; b[1][2] = 2;

a[2][1] = 3; b[2][1] = 3;

a[2][2] = 7; b[2][2] = 2;

do i = 1 to N;

do j = 1 to N;

c[i][j] = 0;

do k = 1 to N;

c[i][j] = c[i][j] + a[i][k] * b[k][j];

end

end

end

say 'Result:'

do i = 1 to N

out = ''

do j = 1 to N

out = out right(c[i][j], 8)

end

say out

end

Result:         8        6        27       20

The result is given by rows, appending elements of each row to the variable out. The function right() puts its first argument (c[i][j] in this case) into a field of size determined by the second argument (8) with justification to the right. Thanks to this function we get pretty-printed output

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]