Array in "Pascal". Programs for arrays in Pascal

Table of contents:

Array in "Pascal". Programs for arrays in Pascal
Array in "Pascal". Programs for arrays in Pascal
Anonim

Every year interest in programming increases. And if in institutions specializing in writing programs they rely on such a programming language 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 their imagination. And if with the help of the Pascal language you can get acquainted with the basic concepts of programming, then in Delphi you can already write a full-fledged program. And quite an important place in writing programs is sometimes occupied by solving arrays in "Pascal".

The presence of a large number of very different variables

array in Pascal
array in Pascal

There are quite a lot of various variables in a programming language, which are characterized by the presence of only one value. They are able to store in themselves one value that has a certain type. String variables are the exception. They areis a collection of those data for which the character type is characteristic. But even such variables are usually considered from the position of a separate value.

It's no secret that with the help of a computer you can significantly reduce the time to perform certain work related to large amounts of data. But when using only those variables that have types known to humans, how can you store the results of work in memory, as well as process those data that contain a large number of lines? Such tasks are quite common in any field of activity.

Of course, you can always enter as many variables as you need to achieve your 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 it is necessary to find errors.

Accordingly, the programmers thought about this question. That is why the languages that have been developed so far have such variables that make it possible to store 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 a programming language.

Using arrays can drastically reduce code size

Under this term is hidden an ordered sequence of data, which is characterized by one type. In addition, all this data gets the same name. It should alsoIt should be noted that many objects of the real world can fit 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 coordinates, you can determine the position of the variable that it will occupy in the general table.

What does a one-dimensional array mean?

arrays in pascal
arrays in pascal

The simplest table is the one 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 based on them.

To describe one-dimensional arrays in "Pascal", just enter the following code: Type Array of.

The numbers are those variables that can have an ordinal type. When specifying a range, it is worth understanding that the initial number cannot be higher than the final one. The type that the array elements have can be absolutely anything - either standard or already previously described. The choice will depend on the need to solve a particular problem.

How is a linear array described?

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

To understand how you can 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. The variable K hides the character type and those elements. Which are integers. The Z array stores numbers whose type is Word.

Among all the actions that can be used when working with an array, assignment can be distinguished. The entire table can be subjected to it. For example, S:=VV. But it should be understood that assignment operations can only be subjected to an array in "Pascal" that has a certain type.

There are no more operations that can be performed on the entire array at once. However, you can work with elements in the same way as with other prime numbers that have a certain type. In order to refer to an individual parameter, you must specify the name of the array. By using square brackets, you must determine the index that is characteristic of the desired element. For example: K[12].

Main differences between arrays and other variables

task pascal arrays
task pascal arrays

The basic difference between table components and simple variables is that it is possible to put in brackets not only the index value, but also such an expression that can lead to the desired value. An example of indirect addressing might be: V[K]. In this case, the variable K takes on a certain value. From thisit follows that you can use a loop when filling, processing and printing an array.

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

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

What methods can be used to display array data on the screen?

You should pay attention to the way the contents of the array are displayed. There are several.

  1. 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.
  2. Program A1;

    Var B: Array [1..10] Of Integer;

    K: Integer;

    Begin

    For K:=1 To 10 Do {This command loops with parameter }

    Readln(A[K]); {A[I] is being entered using the keyboard }

    For K:=10 Downto 1 Do {The table is being printed in reverse order}

    Write(A[K], 'VVV') End.

A similar code of the program for arrays in "Pascal" demonstrates how you can enter 10 numbers using the keyboard, print them out, rearranging the values in reverse order. If the same program is rewritten fromusing 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.

Increase in possibilities through the use of arrays

array program in pascal
array program in pascal

It is also possible to fill tables with values that are equal to the square of element indices. It is also possible to create such an array of strings in "Pascal", which will allow all numbers to be entered automatically. As you can see, using an array greatly enhances the capabilities of the Pascal programming language.

Processing of linear arrays is very common in various 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 contains 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?

tasks pascal arrays
tasks pascal arrays

The data structure that occurs in the Pascal language in order to store the values of such a table isthe name of a two-dimensional array. Description of such an array is possible immediately 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, which 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 allocate two indices. 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, so there is no need to talk about it for a long time.

First way to sort

one-dimensional arrays in pascal
one-dimensional arrays in pascal

Sometimes it becomes necessary to sort 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 nesting the loop, absolutely every table variable will be compared with other values. In other words, if there is an array of 15 numbers, then first number 1 will be compared with other numbers. This will happen until, for example, the element that is greater than the first number is found. Subsequently, the comparison will take place exactly this figure. This will be repeated until the largest is found.element from all proposed. This method is quite simple for those programmers who have just started working in the language.

Second array sorting method

The second way is bubble. The essence of this technique lies in the fact that neighboring elements are compared in pairs. For example, 1 and 2, 2 and 3, 3 and 4, etc. In the event that the found value fully complies with the sorting conditions, it will be moved to the end of the entire array, i.e. it will pop up as a “bubble”. This algorithm is the most difficult to remember. However, you don't need to grind it. 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

solving arrays in pascal
solving arrays in pascal

We hope you understand what arrays are and how you can sort to find a specific value or achieve a specific goal. If you have chosen "Pascal" for solving a particular problem, in which arrays occupy an important place, then you will need to approach their study thoroughly. 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 rightly considered the main quantities, the study of which must take place without fail.

Recommended: