EducationColleges and Universities

The array in Pascal. Programs for arrays in Pascal

Every year interest in programming increases. And if in institutes specializing in writing programs they make a bet on a programming language like C ++, then in schools and technical schools students get acquainted with Pascal. And already on the basis of this language begin to comprehend programming through the use of Delphi software. It should be noted immediately that these programming languages provide a huge space for the manifestation of their imagination. And if you can use the language "Pascal" to familiarize yourself with the basic concepts of programming, then on Delphi you can already write a full program. And an important place in writing programs sometimes takes the solution of arrays in Pascal.

The presence of a large number of very different variables

In the programming language, there are quite a lot of various variables, for which only one value is characteristic. They are able to store in themselves a single value, having a certain type. The exception is string variables. They are the totality of those data for which the character type is characteristic. But such variables are usually considered from the standpoint of an individual quantity.

It is not a secret to anyone that using a computer can significantly reduce the time to perform a certain work associated with large amounts of data. But how, when using only those variables that are known to human types, you can save the results of work in memory, and also process those data that contain a large number of rows? Tasks such occur quite often in any field of activity.

Naturally, it is always possible to enter the number of variables that is necessary to fulfill the set goals. You can also define some values for them. But the code of the program will only increase from this. It is difficult to read the code that has a large number of lines. Especially when you need to find errors.

Accordingly, programmers have pondered over such a question. That is why in those languages that have been developed to the present time, there are such variables that provide an opportunity to save a huge amount of data in themselves. The array in Pascal has changed a lot in the approach to programming. Therefore, it is considered an important variable in the programming language.

Using arrays can significantly reduce the amount of code

This term hides an ordered sequence of data for which one type is characteristic. In addition, all these data receive one name. It should also be noted that under the given definition many objects of the real world can approach: dictionaries, cartoons and many other things. However, the most simple array in "Pascal" is presented in the form of a kind of table. In each separate cell there is one variable. With the help of coordinates, you can determine the position of the variable that it will occupy in the common table.

What does a one-dimensional array mean?

The simplest is the table, which is linear. In this array, in order to determine the location of the parameter, it is sufficient 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 Array [] of .

As the numbers are those variables that can have an ordinal type. Pointing the range, it is worthwhile to understand that the seed can not be higher than the end. The type that elements of an array have, can be absolutely any - either standard, or already described earlier. The choice will depend on the need to solve a specific problem.

How is the linear array described?

It is possible to immediately describe one-dimensional arrays in Pascal. This should be done in a special section, which is necessary for this procedure. You will need to enter the following code: Var : Array [] Of .

In order to understand how to describe an array in Pascal, you should 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 related to integers. The array Z stores numbers whose type is Word.

Among all the actions that you can use when working with an array, you can select the assignment. He can be subjected to the whole table. For example, S: = VV. But it is worthwhile to understand that assignment operations can only be subjected to that array in Pascal, which has a certain type.

There are no more operations that can be immediately exposed to the whole array. However, you can work with elements in the same way as with other prime numbers that have a certain type. In order to access a separate parameter, you must specify the name of the array. By using square brackets, we need to determine the index that is characteristic for the desired element. For example: K [12].

The main differences between arrays and other variables

The basic difference between the components of a table and simple variables is that in parentheses it is possible to put not only an 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 assumes a certain value. From this it follows that you can use the cycle when filling, processing and printing an array.

This form of organization can occur in the case of string variables that are fairly close in their properties to arrays of type Char. But there are differences. They are as follows:

  1. String variables can always be typed in from the keyboard and printed on the screen.
  2. String variables are limited in their length. You can enter a maximum of 255 characters. The critical volume of the array is 64 KB.

By which methods can you display the array data on the screen?

Attention should be paid to the way the contents of the array are displayed on the display. There are several.

  1. Writeln (A [1], A [2], A [3]). Such an example, although primitive, is able to show how one can address directly to each individual element inherent in the table. However, some of the advantages that the Pascal arrays have over simple variables are not visible here.
  2. Program A1;
    Var B: Array [1..10] Of Integer;
    K: Integer;
    Begin
    For K: = 1 To 10 Do {This command organizes a loop with the parameter}
    Readln (A [K]); {A [I] is input by using the keyboard}
    For K: = 10 Downto 1 Do {The table is printed in reverse order}
    Write (A [K], 'VVV')
    End.

A similar program code for arrays in Pascal demonstrates how you can enter 10 numbers using the keyboard, print them out, rearranging the values in the reverse order. If the same program is rewritten using a large number of variables instead of an array, then 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 fill tables with those values that are equal to the square of the element indices. There is also the possibility to compose an array of lines in Pascal, which will allow all numbers to be entered automatically. As you can see, using the array greatly increases the capabilities of the Pascal programming language.

Processing of linear arrays is very common in a variety of tasks. Therefore, there is nothing strange in the fact that they are studied in institutions and schools. Moreover, the possibilities that arrays carry in themselves are quite extensive.

What is hidden under two-dimensional arrays?

You can imagine a table that consists of several lines at once. Each line contains several cells. In such a situation, in order to accurately determine the position of the cells, it is necessary to note not one index, as was the case with linear arrays, but two - the numbers that are characteristic for the row and column. A similar representation is characterized by two-dimensional arrays in Pascal.

How to make a description of tables of this kind?

The data structure that occurs in Pascal in order to store the values of such a table is called a two-dimensional array. A description of such an array is possible at once using two methods.

  1. Var B: Array [1..15] Of Array [1..30] Of Integer;
  2. 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, you need to select 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, therefore, it is not required to talk about this for a long time.

The first way with which you can sort

Sometimes there is a need to sort the data. To do this, the language has the appropriate commands. There are two algorithms by which the array can be sorted in Pascal. The meaning of the direct selection method lies in the fact that by nesting a cycle, absolutely every table variable will be compared with other values. In other words, if there is an array of 15 numbers, then first the number will go through the comparison procedure with the other numbers. This will happen until the moment, for example, that element that is greater than the first number is found. Later, this figure will be compared. This will be repeated until the moment when the largest element is found. This method is quite simple for those programmers who have just started working in the language.

The second method of sorting arrays

The second way is bubble. The essence of this technique lies in the fact that the neighboring elements are compared in pairs. For example, 1 and 2, 2 and 3, 3 and 4, and so on. If the value found is fully consistent with the sorting conditions, it will be moved to the end of the entire array, that is, it pops up as a "bubble" . This algorithm is the most difficult to remember. However, you do not need to memorize it. The main thing is to understand the entire structure of the code. And only in this case, you can claim to achieve great heights in programming.

Conclusion

We hope that you understand what arrays are, and how you can sort to find a certain value or achieve a specific goal. If you were selected to solve a specific task, "Pascal", the arrays in which occupy an important place, then they need to be thoroughly approached. This is influenced by a factor such 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 values, the study of which must occur without fail.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.unansea.com. Theme powered by WordPress.