Data storage

Let's say you've developed a computer game called "User Hostile" in which the players compete against an intricate and unfriendly computer interface. Now you need to write a program that tracks the monthly sales of this game over a five year period. Or let's say you need to inventory Hacker Hero Trading Cards.
Very soon you will come to the conclusion that you need more than simple basic data types to store and process information.
 

Arrays (lists). Introduction

In order to make it convenient to work with a large amount of data, a group of cells is given a common name. Such a group of cells is called an array
Array – it is a group of memory cells of the same type, located side by side and having a common name. Each cell in the group has a unique number.

When working with arrays, you need to learn how to solve three tasks:
x allocate memory of the required size for an array
x write data to the desired cell
x read data from cell


Arrays in Pascal


Traditionally Pascal uses static arrays like

var a: array [1..10] of integer;

The boundaries of an array must be set by constants, and you cannot change the size of an array during program operation. But you can make an index not only of an integer, but also, say, of a character or enumerated type. For example, to count the occurrence of each letter, you can use an array

var LettersCount: array ['a'..'z'] of integer;

and work with it to your heart's content:

LettersCount['z'] := 1;
LettersCount['d'] := LettersCount['d'] + 1;

The disadvantages of such arrays are known: if it is not known in advance how many elements will need to be used, then the maximum size memory is allocated for the array. As a result, in most cases we "stock up for the future", and sometimes this "reserve" turns out not to be enough. That is why such arrays are called  static: their size is static and must be set at the program compilation stage. However, in Pascal there are  dynamic arrays, the size of which can not only be set, but also changed in the course of the program. It is these arrays and the advantages of using them that will be discussed further.

Creating an array

When creating an array, space is allocated in memory (a certain number of cells)
1) Arrays can be created by simply listing the elements:
var a: array of integer;
SetLength(a, 3);
a[0] := 1;
a[1] := 2;
a[2] := 3;

2) Arrays can be composed of data of any type - integer or real numbers, character strings 
var a: array of char;
SetLength(a, 3);
a[0] := 'a';
a[1] := 'b';
a[2] := 'c';
3) An array always "knows" your size. The  length function is used to determine the size of an array. Often the size of the array is stored in a separate variable so that the program can be easily changed to work with a different array size. Example:
N := 10; // in the variable N we store the size of the array
SetLength(a, N); // set array and size N
writeln(length(a)); //display the size of the array
The size of an array can be set from the keyboard.

Referencing an array element

Much of the usefulness of arrays comes from the fact that its elements can be accessed individually.
The way to do this is to use an index to number the elements.
Index is a value that points to a specific array element

REMEMBER!
NUMBERING OF ARRAYS IN PASCAL STARTS FROM ZERO!

(This is mandatory — you must start from scratch. This is especially important to remember)

Examples of accessing array A:
x := (A[3] + 5) * A[1] // read the values ​​of A[3] and A[1]
A[0] := x + 6 // write new value to A[0]
Let's analyze the program for working with array elements.
var i: integer;
a: array of integers;

begin
    i := 1;
    setlength(a, 5); //create an array of 5 elements 
    a[0] := 23; // to each of the 5 elements of the array (indexes from 0 to 4)
    a[1] := 12; // write a specific value
    a[2] := 7;
    a[3] := 43;
    a[4] := 51;
    a[2] := a[i] + 2 * a[i - 1] + a[2 * i]; // change the value of the element with index 2 to the result of the expression
    // because i=1, then substituting the value of the variable i into the expression we get
    // next expression  a[2] := a[1] + 2*a[0] + a[2];
    writeln(a[2] + a[4]);
end.


As a result of running this program the value of the sum of the elements of the array with index 2 and with index 4 equal to 116 will appear on the screen. As you can see from the example, we can access any element of the array. And also calculate the required number of the element using various formulas (for example, as in the program A[i-1] or A[2*i], in these cases, the indexes of the elements will be calculated and depend on the value of i.)

Let's look at an example program
var a: array of integer;

begin
    setlength(a, 5);
    a[5] := 5;
    a[-1] := 0;
end.

Because the array is declared with 5 elements, so the elements will be numbered from 0 to 4. We see that the program in the 6th line refers to a non-existent element а [5] and on the 7th line to the also non-existent a[-1].

It turns out that the program went beyond the bounds of the array
Array out of bounds is accessing an element with an index that does not exist in the array.
In such cases, the program usually crashes with run-time error


 
 

When working with arrays, you usually have to work with all the elements of the array at once.
Iterating through the elements: we look through all the elements of the array and, if necessary, perform some operation on each of them.
For this, a loop with a variable is most often used, which changes from 0 to N-1, where N is the number of array elements.
Under N we will consider the current size of the array, that is
N := length(A)
...
for i := 0 to n - 1 do begin
     // here we work with a[i]
end;
...
In the specified loop, the variable i will take the values ​​0, 1, 2, ..., N-1.  Thus, at each step of the loop, we access a specific element of the array with the number i.
Thus, it is enough to describe what needs to be done with one element of the array a[i] and place these actions inside such a cycle.

Let's write a program that fills the array with the first N & nbsp; natural numbers, that is, at the end of the program, the elements of the array should become equal
a[0] = 1
a[1] = 2
a[2] = 3
...
a[N - 1] = N
It is easy to see the pattern: the value of an array element must be greater by 1 than the index of the element.
The loop will look like this
for i := 1 to n - 1 do
    a[i] := i + 1;

Complete the task.