One-dimensional arrays

One-dimensional array is a series of identical properties that are described by one identifier (array name) and sometimes they are also called vectors. Before we start working with an array, we need to tell the compiler to use the array. We have two ways to declare an array:

  1. When declaring a variable to use the array 
  2. Defining a new type 

When declaring a variable, we'll include data about array immediately after the variable name and a colon. The following is the word Array, which indicates the use of the array, the index type in square brackets, word of and a data type used in an array.

var

Array1: Array [1..10] of Integer;

Array2: Array [1..20] of String ;


We use the definition of a new type if we will work with the same type of array more than once in one program. When defining a new type we start with the word type, continue with the array name (at the beginning of the array name, the letter T is used as the default, which indicates the use of the type), followed by the equal sign, the word Array, the index type in square brackets, word of and data type used in the array. Then we have to declare the variable that will be used in the array.

type

TArray = Array [1..10] of Integer;

var

Array1, Array2: TArray;

When we declare an array, it is empty and contains no elements. These must be added to the array using the assignment command. We know that we use the assignment sign := to assign the value of some variable. For example, if you want an integer variable a to assign a value of 20, use the command a:= 20;. We will also do the same when filling the array.

Each array element represents a separate variable that the values ​​of that type can be assigned to or read from. Access to the individual elements of the array is possible thanks to the index. We suppose we create an array, which we call array. If we want to assign a value of 20 to the tenth element of our array, we will do it similar to assigning a variable value, but we will add the index value to square brackets after the array name, so the command will be as follows:

              Array1 [10]: = 20;

Now, we will demonstrate the operation of the array on specific examples. We define the array with ten elements. We can assign values ​​to elements in two ways:

1. Assigning a value to an element in the body of the procedure

Each of the elements we declare in the body of the procedure with using the index (Figure 111).

Figure 111: Declaring elements in body procedure
Figure 111: Declaring elements in body procedure

2. Assigning a value to an element when declaring a variable 

Each of the elements we declare when declaring the variable, where we write the individual elements in brackets in such order, in which they are to be assigned to each index (Figure 112).

Figure 112: Assigning a value to an element when declaring a variable
Figure 112: Assigning a value to an element when declaring a variable

Array elements can be written to the graphical area using the TextOut command. Since we have an array of integer elements, we must not forget to convert elements from integer data type to string. Regardless of which array definition method we use, the result will be the same (Figure 113).

Figure 113: Array text output
Figure 113: Array text output

This is how we have declared the value of the elements manually. However, we can also declare individual array elements by generating random values ​​using the Random function. Again, we define the array with ten random elements, which we then write to the graphical area (Figures 114 and 115).

Figure 114: Random generation of array elements
Figure 114: Random generation of array elements
Figure 115: Randomly generated array elements
Figure 115: Randomly generated array elements

Now let's see what can be done with an array a its elements. Let's adjust the procedure we used for the previous example. First, we want to load an array with ten random numerical values ​​from interval <1, 20> and print them to the graphical area. Then we want to find the highest value among them.

We know that the Random function generates random numbers from interval <0, n>, where n is the number entered in brackets, that is, Random (50) will generate numbers from interval <0, 49>. If we want the function to omit the number zero and add number 50 to the interval, we simply add the number one to the Random function. This means that any generated number will be increased by one.

The principle of finding maximum in an array is comparison of its individual values. We need to set a maximum to get started. This can be the first element of the array. It will gradually compare all the elements of the array. If the element to be compared is higher than the specified maximum, we replace it with that element. Thus, the cycle passes through all the elements and ultimately prints the maximum found (Figure 116).

Figure 116: Finding the highest array value
Figure 116: Finding the highest array value

As can be seen in the figure, the cycle starts the comparison from the second element because it is useless to compare the first element with the specified maximum (with itself).

Now let's see how the sum of the array elements can be found. This time, through the edit line, we determine the number of array elements that we randomly generate from an interval <5, 60> and we'll list them in the graphical area. Calculating the sum of array elements is easy. We add the current value of the array element to the sum of the previous array elements through variable sum. We must repeat this process from the first to the last element of the array to sum all the elements of the array, so we will use the For loop with n number of repetitions (Figures 117 and 118).

Figure 117: Sum of Array Values
Figure 117: Sum of Array Values
Figure 118: Randomly Generated Array Values and their sum
Figure 118: Randomly Generated Array Values and their sum

Warning! The specified value in the edit line must not be higher than the top of the array range.

Thanks to the well-known sum of array elements, we can easily calculate the arithmetic mean. It is sufficient to divide the current sum by the number of array elements. In this case, however, we need a real decimal number as a result. Therefore, we do not use division with features div or mod, but division by a slash /. We know that the number in the decimal format is of real type, so the mean variable must be Real and converted to the graphical area by the FloatToStr command (Figure 119). 

Figure 119: Arithmetic mean of array values
Figure 119: Arithmetic mean of array values
Vytvorte si webové stránky zdarma! Táto stránka bola vytvorená pomocou služby Webnode. Vytvorte si vlastný web zdarma ešte dnes! Vytvoriť stránky