Interest in programming is growing every year. And if the institutes specializing in writing programs rely on a programming language such as C ++, then in schools and technical schools students get acquainted with Pascal. And already on the basis of this language, they begin to comprehend programming through the use of Delphi software. It should be noted right away that these programming languages provide a huge space for the manifestation of your imagination. And if with the help of the Pascal language you can get acquainted with the basic concepts of programming, then you can already write a complete program on Delphi. And quite important place in writing programs sometimes takes the decision of arrays in "Pascal".
The presence of a large number of different variables
In a programming language there are a lot of various variables that are characterized by the presence of only one value. They are able to store in themselves a single value that has a certain type. String variables are an exception. They are the totality of the data for which the character type is characteristic. But such variables are usually considered from the standpoint of a single quantity.
It’s no secret to anyone that using a computer you can significantly reduce the time it takes to do certain work related to large amounts of data. But how, when using only those variables that have types known to humans, can you save the results of work in memory, as well as process data that contains a large number of lines? Such tasks are encountered quite often in any field of activity.
Naturally, you can always enter as many variables as are necessary to achieve your goals. You can also define some values for them. But the program code from this will only increase. It is difficult to read the code that has a large number of lines. Especially when you need to find errors.
Accordingly, programmers thought about this issue. That is why in those languages that have been developed to date, there are such variables that provide the ability to store a huge amount of data in themselves. An array in Pascal has changed a lot in its programming approach. Therefore, it is considered an important variable in a programming language.
Using arrays can significantly reduce the amount of program code.
This term hides an ordered sequence of data for which one type is characteristic. In addition, all this data is given the same name. It should also be noted that many objects of the real world can come under this definition: dictionaries, cartoons, and much more. However, the easiest way to present an array in Pascal is in the form of a kind of table. Each individual cell contains one variable. Using the coordinates, you can determine the position of the variable that it will occupy in the general table.
What does a one-dimensional array mean by itself?
The simplest is the table that is linear. In this array, in order to determine the location of the parameter, it is enough to specify only one number. More complex arrays are formed on their basis.
In order to describe one-dimensional arrays in Pascal, simply enter the following code: Type <Type name> Array [<Range of numbers>] of <Element type>.
The numbers are those variables that can have an ordinal type. When indicating a range, it should be understood that the initial number cannot be higher than the final. The type possessed by the elements of the array can be absolutely anything - either standard, or already previously described. The choice will depend on the need to solve a specific problem.
How is the description of a linear array going?
It is possible to immediately describe one-dimensional arrays in Pascal. This must be done in a special section, which is necessary precisely for this procedure. You will need to enter the following code: Var <Variable>: Array [<Range of numbers>] Of <Type of the selected element>.
In order to understand how to describe the array in Pascal, you must enter the following code:
- Var
- S, VV: Array [5..50] Of Real;
- K: Array ['C' .. 'R'] Of Integer;
- Z: Array [-10..10] Of Word;
- E: Array [3..30] Of Real.
In this example, the variables S, VV, and T are an array of those numbers that are real. Under the variable K, the character type and those elements are hidden. Which are integers. The Z array stores numbers of type Word.
Among all the actions that can be used when working with an array, assignment can be distinguished. The whole table may be exposed to it. For example, S: = VV. But it’s worthwhile to understand that assignment operations can only be performed on an array in Pascal that has a specific type.
There are no more operations that can be exposed to the entire array at once. However, you can work with elements on the same principle as with other primes that have a certain type. In order to refer to a separate parameter, you must specify the name of the array. Through the use of square brackets, it is necessary to determine the index that is characteristic of the desired element. For example: K [12].
The main differences between arrays and other variables
The basic difference between table components and simple variables can be considered that in brackets it is possible to put not only the index value, but also an expression that can lead to the desired value. An example of indirect addressing can be as follows: V [K]. The variable K in this case takes on some specific value. From this it follows that you can use the loop when filling, processing and printing the array.
This form of organization can occur in the case of string variables that are fairly similar in their properties to arrays of type Char. But there are differences. They are as follows:
- String variables can always be entered from the keyboard and printed on the screen.
- String variables are limited in length. A maximum of 255 characters can be entered. The critical volume of the array is 64 kb.
Through the use of what methods can the array data be displayed on the screen?
Attention should be paid to the way the contents of the array are displayed. There are several of them.
- Writeln (A [1], A [2], A [3]). Such an example, although primitive, is able to show how you can refer directly to each individual element inherent in the table. However, some of the advantages that Pascal arrays have over simple variables are not visible here.
- Program A1;
Var B: Array [1..10] Of Integer;
K: Integer;
Begin
For K: = 1 To 10 Do {This command organizes a loop with a parameter}
Readln (A [K]); {A [I] is entered using the keyboard}
For K: = 10 Downto 1 Do {The table is printed in the reverse order}
Write (A [K], 'VVV')
End.
A similar program code for arrays in Pascal demonstrates how to enter 10 numbers using the keyboard, print them, rearranging the values in the reverse order. If the same program is rewritten using a large number of variables instead of an array, the code will be significantly increased. And this greatly complicates the process of reading the program.
Increased capabilities through the use of arrays
You can also populate the tables with those values that are equal to the square of the element indices. It is also possible to compose such an array of strings in Pascal that will allow all numbers to be entered automatically. As you can see, using an array significantly increases the capabilities of the Pascal programming language.
Processing linear arrays is very common in a variety of tasks. Therefore, there is nothing strange in the fact that they are studied in institutes and schools. In addition, the possibilities that arrays carry are quite extensive.
What is hidden under two-dimensional arrays?
You can imagine a table that consists of several rows at once. Each individual row has several cells. In such a situation, in order to accurately determine the position of the cells, it is necessary to mark not one index, as was the case with linear arrays, but two - numbers that are characteristic of a row and a column. Two-dimensional arrays in Pascal are characterized by a similar representation.
How to describe tables of this kind?
The data structure that occurs in the Pascal language in order to store the values of such a table is called a two-dimensional array. The description of such an array is possible immediately using two methods.
- Var B: Array [1..15] Of Array [1..30] Of Integer;
- Var B: Array [1..15, 1..30] Of Integer.
In all these cases, a two-dimensional array is described that has 15 rows and 30 columns. Those descriptions that were given above are absolutely equivalent. To start working with any one of the elements, it is necessary to distinguish two indexes. For example, A [6] [5] or A [6.5].
The output to the screen will be almost the same as in the case of a one-dimensional array. You only need to specify two indexes. In all other respects, there are no differences as such, and therefore it is not necessary to talk about this for a long time.
The first way you can sort
Sometimes it becomes necessary to sort the data. For this, the language has corresponding commands. There are two algorithms by which an array can be sorted in Pascal. The meaning of the direct selection method lies in the fact that by means of a nested loop, absolutely every table variable will be compared with other values. In other words, if there is an array of 15 numbers, then first 1 number will go through the comparison procedure with other numbers. This will happen until, for example, an element is found that is greater than the first number. Subsequently, this figure will be compared. This will be repeated until the largest element of all proposed is found. This method is simple enough for those programmers who have just started working in the language.
Second Array Sort Method
The second way is bubble. The essence of this technique lies in the fact that there is a comparison of neighboring elements in pairs. For example, 1 and 2, 2 and 3, 3 and 4, and so on. In the event that the found value fully matches the sorting conditions, it will be moved to the end of the entire array, that is, it will pop up like a “bubble” . This algorithm is the most difficult to remember. However, memorizing it is not necessary. The main thing is to understand the whole structure of the code. And only in this case can one claim to achieve great heights in programming.
Conclusion
We hope that you understand what arrays are, as well as how you can sort to find a specific value or achieve some specific goal. If you have chosen Pascal to solve a specific problem, in which arrays occupy an important place, then you will need to thoroughly study them. This is influenced by such a factor as the presence in the language of a sufficiently large number of variables that are used in certain situations to simplify the entire code as a whole. Arrays are rightfully considered the main quantities, the study of which must take place without fail.