How to fill a one-dimensional array. Arrays in Pascal

Defining Arrays

One-dimensional arrays

Basic concepts:

An array is identified by a single name. So the entire set of real numbers

1.6, 14.9, -5.0, 8.5, 0.46

can be considered an array and denoted by one name, for example A. The variables that form the array are called array elements. Each array element is indicated by the array name with an index enclosed in parentheses.

A(1), A(2), A(3), ..., A(n).

An index determines the position of an element of a data array relative to its beginning.

For the example discussed above, the elements of array A are:

A(1)=1.6, A(2)=14.9, A(3)=-5.0, A(4)=8.5, A(5)=0.46

Before using an array, the program must include a DIM statement that specifies the maximum allowed index. This will enable the BASIC system to reserve an area of ​​sufficient size in memory.

DIM statement notation format:

DIM array_name (maximum_index)

"ArrayName" is usually chosen using the same rules as simple variable names.

"Maximum_index" indicates the maximum index allowed in the program and must be positive.

Examples of array descriptions:

DIM X(50) " declaration of a one-dimensional numeric array X for 50 numbers;

DIM V$(12) "declaration of a one-dimensional array V for 12 character elements.

Declaring a variable size array.

Types of errors

If you specify in the program an array element with an index greater than the dimension value declared in the DIM statement or accepted by default, then error message 9 is displayed:

Subscript out of range (out of array limits).

If the DIM statement is specified after an array name is used, or the array is re-declared, message 10 appears:

Redimensioned array (re-setting the array dimension).

There are two ways to assign values ​​to array elements:

1) static, using the DATA, READ operators and the assignment operator;

2) dynamic, using the INPUT operator and the RND function.

When working with arrays, it is very convenient to use the FOR...NEXT loop operator. The loop parameter is used as the array index.

1. Example of static filling of an array.

DATA plum, pineapple, pear

DATA apple, cherry, apricot

The FOR...NEXT loop sequentially assigns values ​​to all variables in the list.

2. Example of dynamically filling an array

INPUT "Enter the number of array elements ";N

This example uses a variable array size setting.

3. Example of filling an array using the standard RND function

Array in programming is a set of elements of the same type (of the same type).

There are several types of arrays - one-dimensional(vector) and multidimensional.

Elements in an array are characterized by their names and serial numbers - indices.

An index is the ordinal number of an element in an array.

In Pascal, each element is assigned one or more indices that describe the element's position in the array.

One-dimensional array

The array syntax in Pascal is:

Var a: array Of integer ;
Where:
1 – subscript
10 – superscript
A – array variable name
– range of values
Integer – data type
A[ i ] – accessing an array element in Pascal

The type of array elements can be any valid type in Pascal, except files (even an array).

Array example: A = (1,-5,230,55,-88,0,100)

When an array is declared, its superscript must be strictly defined.

When describing an array, memory is allocated, and the compiler must know how much memory needs to be allocated for the described array.

There is no limit on the number of indexes in a Pascal array. However, the array itself must not be larger than 65537 bytes.

An array can also be declared in the type declaration section:

Type mass = array Of real ; Var a,b,c: mass ;
Array elements are accessed in a loop.

The most efficient way to process array elements in Pascal is the loop operator with a parameter.

Why do you think? Yes, because we know a finite number of elements in the array.

Algorithms for filling an array in Pascal

  1. Entering array elements using a computer is carried out using the following construction:

    For i:= 1 To 10 Do read(A[i]);

  2. Setting the array randomly.

    The array can be set randomly using a random variable sensor.

    To run a random variable sensor in Pascal, you need to write a special construction - Randomize;

    The new value is generated using the Random(n) function, where n is an integer. In this case, any number with a range from 0 to n is generated.

    K:= Random(100);
    If the Random function is used without a parameter, it generates a real number (type real) in the range 0< X < 1

    X:= Random ;

Filling an array randomly

This construction in Pascal implements random filling of an array.

Randomize ; For i:= 1 To 10 Do Begin A[i] := random*100-70 ;

write(A[i]:6:2) ; End ; Sections:

Computer science

  1. Introduce students to the possibility of filling and processing an array.
  2. Create a graphical interface for a project to fill an array and calculate the sum of elements in a given array.
  3. Develop cognitive interest in the subject
  4. Foster a responsible attitude towards learning

DURING THE CLASSES

1. Updating the lesson

Organizing time

Frontal survey on the previous topic “The concept of an array. One-dimensional array"

2. Formation of skills and abilities

Explanation of new material

Array Declaration

Declaring an array is similar to declaring variables; you only need to specify the range of index changes. For example, declaring a one-dimensional integer array containing 10 elements is done as follows:

A: array of integer;

Basic tasks when working with arrays

1. Formation (filling) of the array

1.1. according to the formulas For i:=1 to 10 do a[i]:= i*i;

1.2. generate randomly For i:=1 to 10 do a[i]:= random(20):

The built-in function RANDOM(MAX), returns a random integer uniformly distributed in the range from 0 to MAX – 1 (MAX is the access parameter)

1.3. enter from the keyboard For i:=1 to 10 do read(a[i]);

2. Sort the array (ascending, descending);

3. Search for elements in an array;

4. Selecting elements from an array by condition;

Filling the array randomly.

To start working with an array, you need to fill it, i.e. assign specific values ​​to array elements. To generate a sequence of random numbers, we use the Random(100) function. When you run the program, this function will output a pseudo-random sequence of integers in the range from 0 to 100.

To generate sequences of random numbers that differ from each other, it is recommended to use the Randomize operator

Actions with one-dimensional arrays

1. Calculation of the sum of elements

For I:= 1 To 10 Do s:=s+ a[i]; (usual accumulation of amount in s)

2. Calculation of the product

For I:= 1 To 10 Do р:=р* a[i]; (usual accumulation of product in p)

3. Search for an element with a given value

3. Note Development of skills and abilities in practice

Project “Sum of elements in an array”. Let's develop a project “Sum of elements in an array”, which will fill the array with random numbers and calculate the sum of these numbers

First, let's create a procedure for filling the array

1.Launch the Delphi programming system.

2. Work on the project begins with creating a graphical interface, for this in the window Form Builder Control elements are placed on the form. To create a graphical interface for the project, we will place on the form two text fields for displaying numerical data (one for filling an array, the other for displaying a sum) and two buttons for implementing event procedures: filling an array and sum

3. With Toolbars place an Editl text field and a Buttonl command button on the Forml

The next step is to create event procedure code. Double-clicking the button for which you need to create program code brings up a window Program code with an empty event procedure template.

4. Double-click on the Buttonl button, a template event procedure TForml.ButtonlClick will appear: Declare an array A and description of variables I, S in the var variable description section

A:array of integer;

procedure TForm1.Button1Click(Sender: TObject);

For I:= 1 To 10 Do

A[I] := Random(10);

Edit1.Text:= Edit1.Text +" " + IntToStr(a[i]);

5. Save Project As

6. Compiling the project (Project - Compile)

Now let's create a procedure to calculate the sum of the elements in the filled array

By using Toolbars Let's place a button Button2 and a text field Edit2 on the Forml. Double-clicking the button Button2, for which you need to create a program code, opens a window Program code with an empty event procedure template.

procedure TForm1.Button2Click(Sender: TObject);

For I:= 1 To 10 Do

Edit2.Text:= Edit2.Text +" " + IntToStr(s)

Saving the project of the entire project (Save Project).

Let's compile the project (by pressing the F9 key).

Click the Fill Array and Sum buttons.

The results of the amounts for various filling options will be displayed in the text field

4. Summing up

5. Homework: Create a project “Product of array elements”, which involves filling an array with random numbers and the ability to display the product of all elements in the array in a text field.

We can fill the elements of a one-dimensional array with values: by entering values ​​from the keyboard; randomly; according to the formula. Methods for specifying one-dimensional arrays Loops are used to input and output numeric array values. The procedure takes a parameter by reference, an Mssiv array of a given type, and an integer variable n responsible for the number of array cells to fill. Formation of a one-dimensional array randomly.


Share your work on social networks

If this work does not suit you, at the bottom of the page there is a list of similar works. You can also use the search button


Filling.

We can fill the elements of a one-dimensional array with values:

Entering values ​​from the keyboard;

Randomly;

According to the formula.

Methods for defining one-dimensional arrays

Loops are used to input and output numeric array values.

Let's consider procedures that would form a one-dimensional array in two ways

1) randomly,

2) entering elements from the keyboard

Let's assume that we will be working with an array of integers. Let it be enough for us to have a maximum number of elements equal to 50. The procedure takes a parameter by reference, a Massiv array of a given type and an integer variable n , which is responsible for the number of array cells to be filled. We will also need a local variable i , which will act as a loop parameter and is used to specify a number that determines the location of the element in the array.

1. Formation of a one-dimensional array randomly. Let's set the value of each element to the result of the random function Random(10). We will set the filling of the array using a cyclic for operator, in the body of which the random number is calculated by the Random(10) function, after which this value is assigned to the next i -th element of the array.

Procedure InsertMas1(Var massiv:mas; n:integer);

I: integer;

Begin

Randomize;

For i:=1 to n do

Massiv[i] := Random(10);

End ;

2. Formation of a one-dimensional array by entering elements from the keyboard.

Procedure InsertMas2(Var massiv:mas; n:integer);

I: integer;

Begin

For i:=1 to n do

Begin

write("Enter", i ,"th array element ");

readln(array[i]);

End;

End;

The array is displayed on the screen as follows:

Procedure PrintMas(massiv:mas; n:integer);

I: integer;

Begin

For i:=1 to n

Write(Massiv[i]:5);

End.

We must remember that in all three cases we cannot do without organizing a cycle.

Finding the maximum (minimum) element of an array.

Let us have a one-dimensional array:

20,-2, 4, 10,7, 21,-12, 0, 4, 17.

Let's think about what operations need to be performed if we need to find the maximum element. Naturally, the comparison operation We do not think about the fact that we always compare a pair, “running” through all the elements of the array. We will construct the algorithm for finding the maximum (minimum) element in such a way as to compare a pair of numbers, repeating the comparison action the required number of times.

So we need to answer two questions:

1) what numbers are included in the pair that makes up the relation operation;

2) how many times the comparison operation must be repeated. Let's introduce an additional variable named max. It will be one of the numbers, the second number is the next element of the array. In order to carry out the first comparison operation, it is necessary to assign some initial value to the variable max. There may be two options here:

1) assign the first element of the array to the variable max;

2) assign a number that is obviously smaller than all elements of the array.

The array contains information about the number of students in each group of the first year. Determine the group with the maximum number of students, assuming that the group number corresponds to the serial number of the number in the array (we assume that there is only one such group).

In other words, we must find the maximum element and its number.

program max_num;

type mas=array[ 1.. 10] of byte;

var a: mas;

num, i: byte;

max: byte;

begin

(fill block)

for i:=l to 7 do

readln(a[i]);

(search for the maximum and its number)

max:==0;

(enter the smallest number for this array)

for i:=l to n do

if a[i]>max then begin

num:=i;

max:=a[i]

end;

writeln("maximum number of students=",max);

writeln("group number=",num);

end.

3) Find the minimum element among the even elements of the array.

Explanation: we cannot assign the first element of the array to the min variable, because it may be odd. Therefore, we must choose some very large number for this data type.

If we announce elements of the array are integer, then like this the number will be +32767.

program min_even;

a:array of integer;

i:integer;

min:integer;

begin

for i:=l to 10 do beein

writeln("enter the next array element");

readln(a[i]) ;

end;

min:=32767;

for i:=l to 10 do

if (a[i]

if min=32767 then writeln ("there are no even elements in the array") else writein ("the minimum element among the even elements of the array=",min)

end.

Please note: it is necessary to check whether the value of the min variable has changed, because there might not be even elements.

Other similar works that may interest you.vshm>

8729. DEFINITION AND METHODS OF SPECIFICATION OF A FINITE MACHINE. SYNTHESIS PROBLEM. ELEMENTARY MACHINES 189.1 KB
Definition and methods of specifying a finite state machine. DEFINITION AND METHODS OF SPECIFICATION OF A FINITE MACHINE. Definition of a finite state machine. Methods for specifying a finite state machine.
3552. Individual homework in chemistry. Chemistry homework 475.47 KB
Guidelines include individual homework on the following topics: classes of inorganic compounds, chemical equivalent, atomic structure, chemical bonding, chemical thermodynamics, chemical kinetics, concentration of solutions, ionic reactions and hydrolysis of salts, redox reactions, electrochemical processes, properties of metals.
12127. Strategic minerals (PGM, Ni, Co, Cr, Cu) of Paleoproterozoic layered mafic massifs of the northeast of the Fennoscandian shield 17.77 KB
Brief description of the development. Advantages of the development in comparison with analogues. An important aspect of the development is the ability to minimize the negative technogenic impact on the environment by sharply reducing the extensive use of heavy mining and drilling equipment at the reconnaissance and prospecting stages. Areas of commercial use of the development.
9554. MATHEMATICS. METHODOLOGICAL GUIDE AND TASKS 268.34 KB
The academic discipline “Mathematics” is intended to implement state requirements for the minimum content and level of training of graduates of secondary vocational education.
18129. Creative tasks as a means of developing imagination 91.06 KB
These studies reflect the diversity of scientific ideas and practical approaches to organizing the creative activity of students in the educational process; however, the aspect of purposefully providing creative tasks to younger schoolchildren in the learning process as a means of developing imagination has not yet been sufficiently studied. Based on the identified contradictions in the analysis of philosophical psychological and pedagogical literature, as well as as a result of studying the experience of primary schools, a research problem was formulated consisting of theoretical...
19517. Development of technical specifications for the automation of the Bukva store 155.63 KB
Competent sale of goods based on client requirements, that is, consultation with specialists. Therefore, it is necessary for the store to receive information about the state of the market and provide the market with information about available goods and services. Interaction with the media consists of the store providing data about itself, its goods and services; subsequently, from this data, advertising for the laptop store will be generated, which is perceived by the market for goods and services. Expansion of product types Store advantages: Extensive experience...
3548. Chemistry homework and guidelines for completing them 229.61 KB
These homework assignments are intended for the systematic work of students of all specialties on the chemistry course in accordance with the curriculum. Completing assignments helps students develop independent work skills.
19091. ANALYSIS OF TECHNICAL SPECIFICATIONS AND BASIC TECHNICAL REQUIREMENTS FOR THE DEVELOPED DESIGN 911.42 KB
Server room (server room or simply server room) is a dedicated technological room with specially created and maintained conditions for the placement and operation of server and telecommunications equipment. The permissible temperature in the server room should be
1763. Implementation of a task as a class using the C++ standard template library (STL) container to store information 190.6 KB
The syntax of C++ is inherited from the C language. One of the design principles was to maintain compatibility with C. However, C++ is not strictly a superset of C; many programs that can be equally successfully translated by both C compilers...
10124. Development of technical specifications for the provision of advertising services, cleaning services, security, and staffing 31.88 KB
Development of technical specifications for advertising services: legal regulation of advertising services. Development of technical specifications for cleaning services: basic concepts and types of services. Development of technical specifications for security services: legal regulation. Development of technical specifications for personnel services: basic concepts.

1 Method (keyboard filling. Dynamicinputdata)

M:array of integer;

For I:=1 To 10 Do Begin

Write("Enter ",I," value ");

2 Method (using a random number generator)

M: array of integer;

For I:=1 To 25 Do Begin

M[I]:=Random(50);

3 Method (static data input)

M: array of integer = (31,28,31,30,31,30,31,31,30,31,30,31);

For I:=1 To 9 Do

1.4 Examples of problem solving

1. Algorithms for searching and assigning values ​​to array elements

1. Create a program for processing an array of dimension n filled with integers entered from the keyboard. Print the indices and values ​​of positive array elements.

A:ARRAY OF INTEGER;

(Array Filling)

FOR I:=1 TO N DO Begin

Write("Enter ",I," array element "); ReadLn(A[I]);

(Processing Array Elements)

FOR I:=1 TO N DO

IF A[I]>0 THEN WriteLn("Positive element = ",A[I]," its index = ",I);

2. Create a program for calculating and printing the values ​​of the function Y=sin(x-1)/2x. Set the argument values ​​in an array X consisting of 6 elements. Write the function values ​​into the Y array.

X,Y:ARRAY OF REAL;

FOR I:=1 TO 6 DO Begin

Write("Enter ",I," argument value "); ReadLn(X[I]);

FOR I:=1 TO 6 DO Begin

Y[I]:=SIN(X[I]-1)/(2*X[I]);

WriteLn(" X= ",X[I]:4:1," Y=",Y[I]:5:2);

3. Given an array M, consisting of 30 elements. The elements of the array are arbitrary integers. Display the value of every fifth and positive element. Output the specified elements into a string.

M:ARRAY OF INTEGER;

ClrScr;

Randomize;

WriteLn("Array element values");

FOR I:=1 TO 30 DO Begin

M[I]:=Random(20)-4;

Write(M[I]:3);<=30 DO Begin

WriteLn("Values ​​of every fifth and positive array element");

While I

    IF M[I] > 0 THEN Write(M[I]:3);

    Examples for independent solutions:

    Given a one-dimensional array of dimension 10, filled with integers entered from the keyboard, and the value N. Replace negative elements with N. Display the modified array in one line.

    Given a one-dimensional array of dimension N, filled with random numbers in the range from -15 to 20. Display the values ​​of array elements whose absolute value is >10.

    Given a one-dimensional array of dimension N, filled with random numbers. Square every third element of the array if the element is negative.