Last Updated:

One-dimensional arrays in Pascal

Until now, you've had to interact with standard varieties of data, including whole and logical data. They also studied the material and symbolic varieties in depth.

Like any algorithm, you can conduct programming by resorting to four types that are considered basic, that is, basic. But if you want to deal with a more diverse programming world, you'll need more complex data types. It is these constructions, which include certain scalar types, that are usually classified as structures. What they are, read on.

One-dimensional arrays

The structure is a composite variant at these points. This is a type that is based on basic scalars. If the structure of the structure does not change, as such, and throughout the entire period while the program where it is described operates, it can be ranked by indicating the structure of the category static.

Array as a single component

An array is undoubtedly classified as one of the most common types of structures. This is a set of elements, which is characterized by one property - homogeneity. It is implemented in almost all programming options, languages that are used today.

Fact! The arrays contain a limited number of elements. Note that the components of an array are characterized by the same type, called the base type.

What never changes in the composition of an array is the homogeneity of the structure. It may contain elements such as integer, the same type of components of another plan. But do not think that the components of the object in question can include only scalar varieties.

There is another important feature that is traditionally inherent in the array. It consists in the fact that you can arbitrarily refer to almost any of its important components. What does that mean? The program is able to receive immediately the necessary element, following the serial number, that is, the index.

Quite often there is such a variant as an integer type. But modifications such as symbolic, as well as other, no less well-known logical ones, are also possible. How would an array be described in Pascal? In this programming language, an array-type task occurs, where a special word array is used, which actually points to the array. Let's see how the declaration of this object is carried out in the program

Type < _ type name >= array [ I ] of T;

The English letter I indicates here the type of index that is traditionally inherent in the array. A large T indicates a specific type of array constituent. You have the ability to describe variable array types at the same time, that is, directly in the section where variables are described. It looks like this:

Var a,b: array [ I ] of T;

In most cases, a small range is provided to characterize the type of index. This applies to values that occur with respect to any ordinal type. For example, there may be changes in the range from 1 to 20 for indices. In this case, you can characterize the length of the pascal array using the following expression:

ord ( I n )- ord ( I 1 )+1.

There is another version of the announcement, which is relevant for two types:

Type
Vector=array [1..10] of integer;
Stroka=array [0..255] of char;

Note here that the vector appears in a variant of the pascal array where 10 integers are marked. At the same time, in the form of an array of 256 characters, there is a stroka.

Resorting to the use of an array index, you can periodically refer to certain elements of any object that is an array as a standard variable. This means that you can get the value of the component in question, assign a particular value to it separately, or use its expressions. How do you describe the variables we talked about earlier? This refers to the vector and stroka varieties:

Var a: vector;
c: stroka;

Subsequently, in the program, we can do the same thing, but refer to the individual components of the array, among which are a or c.

Perform calculations on the index of the Pascal array

An important remark is that the array index does not have to be formed explicitly. This role can be an expression that corresponds to its parameters to indexes or use a variable.

In other words, any programmer has the ability to calculate an index. The mechanism we are considering can be considered a rather serious means of programming. But it can provoke adverse events and entail a common mistake. It concerns the results of calculations. As a result of our actions with you, the interval of acceptable index values may be outside the allowable indicators.

What does this mean in practice? This suggests that an attempt will be made to go to an element that is not actually detected.

Here is a common example of such an error:

Program primer _ error ;
Type
vector=array [1..80] of word;
var
n: integer;
a: vector;
begin
n:=45;
a[n*2]:=25;
end .

As we can see, the program fully corresponds to the syntactic features of the language, and therefore the system skips it. Despite this, as soon as we get to the execution stage, we will get an error due to the fact that we will go beyond the Pascal array.

If the expression is N equal to 45, if the value n *2= 90, the computer will try to access an array element called a [90]. The problem will be that such an element will not be available, because before that an array with a dimension of 80 was described.

One can only hope that high-quality software will issue a warning if an attempt is made to search for non-existent elements of the array. But do not forget to check the probable exits of both the left and right boundaries of the array.

The need for such an approach cannot be ruled out, because as a result, it will be possible to obtain after counting the number located to the left of the border.

What conclusion can be drawn from the above? Every programmer must be careful when interacting with array indexes.

List of basic manipulations

It is a well-known fact that the definition of a data type should be understood as limiting the scope of possible values. Also here you can imply a set of certain operations that are permissible in relation to the information data in question.

 

We were able to define a data type called a Pascal array. But what operations can be defined as such? The only course of action that is valid when interacting with an entire array is assignment. At the same time, an important nuance is that this is possible with the same type of array. For example, if you specify two variables in your program that belong to the same type. Here's what it looks like in practice:

Var
a , b : array [1..10] of real ;

We enter data

If you have to enter the value of the elements, first change the index value, and be sure to do it sequentially. That is, you should move from the first and gradually go to the last, introducing suitable elements for this.

To implement these actions with comfort and without difficulties, it is necessary to use a loop with a given number of repetitions. Such a cycle is also called a simple arithmetic. Here, the loop parameter plays the role of a variable. It is also an array index in the Pascal programming language.

The value inherent in these elements can be entered through the work of the assignment operator or directly from the keyboard.