Filling an array from the Pascal keyboard. Two-dimensional arrays

Filling an array from the Pascal keyboard.  Two-dimensional arrays
Filling an array from the Pascal keyboard. Two-dimensional arrays

A one-dimensional array is a named sequence consisting of numbered elements of the same type. Elements can be any available in Pascal(except for file) data type. A number, also called an index, is assigned to each element of the array. The index must be an ordinal type. A one-dimensional array can be declared as a variable:

var<имя переменной>: array of<тип элементов>;

like this:

type<имя типа>= array of<тип элементов>;

Here m is the number of the first element, and n– the last one. For example, if the range is specified like this: then this means that a one-dimensional array of 10 elements is defined, with indices from 1 to 10.

To access an array element, you need to specify its name and number: mas[i], here mas is the name, i is the number. In the program below we will declare an array and produce simple operations over its elements.

1
2
3
4
5
6
7
8
9
10
11
12

program array_primer;
uses crt;
var mas, A: array [ 1 ..10 ] of real ;
begin
clrscr;
mas[ 1 ] := 32 ;
mas[ 5 ] := 13 ;
mas[ 9 ] := 43 ;
A[ 1 ] := (mas[ 9 ] — mas[ 1 ] ) * mas[ 5 ] ;
write (A[ 1 ] : 5 : 2 ) ;
readkey;
end.

In a sense, you can work with arrays just like with regular variables, but imagine, for example, a situation where you need to fill an array consisting of tens or thousands of elements. It will be more convenient to do this through a loop. The following construct fills an array with numbers and displays them on the screen.

for i:=1 to n do
begin
mas[i]:=i;
write(mas[i]:3);
end;

If you want the array to consist of values ​​entered from the keyboard, then simply replace the assignment with read operator. There are also situations when you need to fill an array with random numbers. The program below assigns a random value to each element in turn.

1
2
3
4
5
6
7
8
9
10
11
12
13
14

program array_random;
uses crt;
var i: integer ;
mas: array [ 1 ..100 ] of integer ;
begin
clrscr;
randomize;
for i:= 1 to 100 do
begin
mas[ i] := random(10 ) ;
write (mas[ i] : 2 ) ;
end ;
readkey;
end.

Problems related to various kinds of algorithms applicable to arrays are widespread. Among them, methods of searching and sorting elements are especially popular. But each of these algorithms requires individual study, so you can familiarize yourself with them in other articles:

Sorting algorithms:

Search algorithms:

Less complex and at the same time popular methods include methods for determining the number of positive and negative, minimum and maximum elements. Let's look at them.

Finding the maximum element in an array:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

program array_max;
uses crt;
type array= array [ 1 ..10 ] of word ;
var i, max: integer ;
A: massive;
begin
clrscr;
for i:= 1 to 10 do
begin
write ('Item #', i: 2, '=') ;
read(A[i]); (keyboard input)
end ;
max:= A[ 1 ] ; (let the first element be the maximum)
for i:= 1 to 10 do
if max writeln ;
write( ‘Maximum element = ‘, max) ;
readkey;
end.

In order to make a program to find the minimum element you just need to change the sign< в 15 строке на >.

Determining the number of positive elements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

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 be filled. 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's say we 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 assignment 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 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 environment due to a sharp reduction in the extensive use of heavy mining and drilling equipment at the reconnaissance and search stages. Regions commercial use 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 work experience primary school The 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 curriculum. Completing assignments helps students develop independent work skills.
19091. ANALYSIS OF TECHNICAL SPECIFICATIONS AND BASIC TECHNICAL REQUIREMENTS FOR THE DESIGN BEING DEVELOPED 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. Permissible temperature in the server room there 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 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.

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

IN in this example a variable array size is used.

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 ;

The lesson explains how to work with one-dimensional arrays in Pascal, how to use a random number generator - the function random in Pascal. An example of how to derive Fibonacci numbers is considered.


The materials on the site are aimed at practical language acquisition Pascal programming. Brief theoretical information do not claim to provide complete coverage of the material on the topic; the necessary information can be found on the Internet at large quantities. Our tasks include providing the opportunity to gain practical programming skills in Pascal. Solved visual examples and tasks are presented in order of increasing complexity, which will make it easy to study the material from scratch.

Array Declaration

There are two types of arrays in Pascal: one-dimensional and two-dimensional.
Defining a one-dimensional array in Pascal sounds like this: a one-dimensional array is a certain amount elements belonging to the same data type, which have the same name, and each element has its own index - a serial number.
The description of an array in Pascal (declaration) and access to its elements is as follows:

Array Declaration

var length: array [ 1 .. 3 ] of integer ; begin length[ 1 ] : = 500 ; dlina[ 2 ] : = 400 ; dlina[ 3 ] : = 150 ; ...

var length: array of integer; begin length:=500; length:=400; length:=150; ...

  • dlina — array identifier (name);
  • for the declaration, the service word Array is used (translated from English as “array” or “set”);
  • - the number (index) of the first element is placed in square brackets, then two dots and the index of the last element of the array, i.e. in essence, the number of elements is indicated; the number of elements in an array is called array dimension
  • of integer (from English “from integers”) - indicates what type the array is, of here is an auxiliary word.
  • Announce the size can be used through a constant:

    Initializing an Array

    In addition, the array itself can be constant, i.e. all its elements in the program are predetermined. The description of such an array looks like this::

    const a: array [ 1 .. 4 ] of integer = (1 , 3 , 2 , 5 ) ;

    const a:array of integer = (1, 3, 2, 5);

    Filling with consecutive numbers:

    Result: A = 8, A = 9, A = 10, ..., A[N] = A + 1

    Keyboard input:

    Example: Let's look at how an array is entered in Pascal:

    writeln("enter the number of elements: "); readln(n); (if the quantity is not known in advance, we ask for it) for i:= 1 to n do begin write("a[", i, "]="); read(a[i]); ...end; ...


    ✍ Example result:

    Enter the number of elements: 3 a=5 a=7 a=4

    Printing Array Elements

    Example: Let's look at how to display an array in Pascal:

    1 2 3 4 5 6 7 8 9 10 11 12 13 var a: array [ 1 .. 5 ] of integer ; (array of five elements) i:integer ; begin a[ 1 ] : = 2 ; a[ 2 ] : = 4 ; a[ 3 ] : = 8 ; a[ 4 ] : = 6 ; a[ 5 ] : = 3 ; writeln("Array A:"); for i : = 1 to 5 do write (a[ i] : 2 ) ; (output of array elements) end.

    var a: array of integer; (array of five elements) i: integer; begin a:=2; a:=4; a:=8; a:=6; a:=3; writeln("Array A:"); for i:= 1 to 5 do write(a[i]:2); (output array elements) end.

    ✍ Example result:

    Array A: 2 4 8 6 3

    To work with arrays, it is most often used in Pascal with a parameter, since you usually know how many elements are in the array, and you can use a loop counter as indices of the elements.

    Array 0 task. You must specify a real array of dimensions 6 (i.e., six elements); fill the array with input values ​​and display the elements on the screen. Use two loops: the first for inputting elements, the second for output.


    In this example of working with a one-dimensional array, there is an obvious inconvenience: assigning values ​​to elements.

    Processing arrays in Pascal, as well as filling an array, usually occurs using a for loop.

    Random function in Pascal

    In order not to constantly request the values ​​of array elements, a random number generator in Pascal is used, which is implemented by the Random function. In fact, pseudo-random numbers are generated, but that’s not the point.

    To generate numbers from 0 to n (not including the value of n itself, integers in the interval of integer; i:integer; begin randomize; for i:=1 to 10 do begin f[i]:=random(10); ( interval ) write(f[i]," "); end;

    ✍ Example result:

    9 8 9 2 0 3 6 9 5 0

    For real numbers in the interval and display elements on the screen: define three positions for displaying each element.

    Fibonacci numbers in Pascal

    The most common example of working with an array is the output of a series of Fibonacci numbers in Pascal. Let's consider it.

    Example: Fibonacci number series: 1 1 2 3 5 8 13…

    f[ 0 ] : = 1 ; f[ 1 ] : = 1 ; f[ 2 ] : = 2 ; ...

    f:=1; f:=1; f:=2; ...

    f[ 2 ] : = f[ 0 ] + f[ 1 ] ; f[ 3 ] : = f[ 1 ] + f[ 2 ] ;
    f[ i] : = f[ i- 2 ] + f[ i- 1 ] ;

    f[i]:=f+f;

    We received the formula for the elements of the series.

    Example: Calculate and print the first 20 Fibonacci numbers.

    1 2 3 4 5 6 7 8 9 10 11 var i: integer ; f: array [0 .. 19] of integer; begin f[ 0 ] : = 1 ; f[ 1 ] : = 1 ; for i: = 2 to 19 do begin f[ i] : = f[ i- 1 ] + f[ i- 2 ] ; writeln (f[ i] ) end ; end.

    var i:integer; f:arrayof integer; begin f:=1; f:=1; for i:=2 to 19 do begin f[i]:=f+f; writeln(f[i]) end; end.

    In this example, the principle of working with number series becomes clear. Usually, to display a number series, there is a formula for determining each element this series. So, in the case of Fibonacci numbers, this formula-rule looks like f[i]:=f+f. Therefore it must be used in for loop when forming array elements.

    Array 2 task. Given a row of 10 arbitrary numbers: a, a, ... , a (use the random() function). Calculate and print the sum of triplets of adjacent numbers: a+a+a , a+a+a , a+a+a , …… , a+a+a

    Pseudocode:

    Finding the maximum element by its index:


    Array_min task: Find the minimum element of the array. Print the element and its index.

    Array 4 task. Given an array of 10 integer elements. Find the number of negative ones and display the number on the screen.

    Array 5 task. Find the minimum and maximum of n entered numbers (array). Determine the distance between these elements. 3 2 6 1 3 4 7 2 >>> min=1, max=7, distance=3

    Array 6 task. Dan integer array size N. Print all even numbers contained in this array in descending order of their indices, as well as their number K. N=4 mas: 8 9 2 5 >>> 2 8 quantity= 2

    Array 7 task. Enter an array of 5 elements from the keyboard, find the two maximum elements and their numbers in it.

    Example:

    Initial array: 4 -5 10 -10 5 maximum A=10, A=5

    Search in an array

    Let's consider complex example working with one-dimensional arrays:

    Example: Given an array of 10 numbers. Determine whether the number entered by the user is in the array. If there is, output "found", if not – "not found".
    The difficulty of the task is that to print the words "found" or "not found" needed once.


    To solve this problem, you will need a break statement to exit the loop.
    Solution Option 1. For loop:

    Show solution

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 var f: array [ 1 .. 10 ] of integer ; flag:boolean ; i, c: integer ; begin randomize; for i: = 1 to 10 do begin f[ i] : = random(10 ) ; write (f[ i] , " " ) ; end ; flag: = false ; writeln( "enter sample") ; readln(c); for i: = 1 to 10 do if f[ i] = c then begin writeln ("found" ) ; flag: = true ; break ; end ; if flag= false then writeln ("not found" ) ; end.

    var f: array of integer; flag:boolean; i,c:integer; begin randomize; for i:=1 to 10 do begin f[i]:=random(10); write(f[i]," "); end; flag:=false; writeln("enter sample"); readln(c); for i:=1 to 10 do if f[i]=c then begin writeln("found"); flag:=true; break; end; if flag=false then writeln("not found"); end.

    Let's consider effective solution:

    Task: find an element equal to X in the array, or establish that it does not exist.

    Algorithm:

    • start from 1st element (i:=1);
    • if the next element (A[i]) is equal to X , then finish the search otherwise go to the next element.

    solution in Pascal Option 2. While loop:

    We invite you to watch a detailed video analysis of searching for an element in an array (an effective algorithm):

    Array 8 task. Fill an array of 10 elements with random numbers in the interval and print the numbers of all elements equal to X .

    Example:

    Initial array: 4 0 1 2 0 1 3 4 1 0 What are we looking for? 0 A, A, A

    Cyclic shift

    Example: shift the array elements to the left by 1 position, the first element replaces the last one.



    Solution:

    Algorithm:
    A:=A; A:=A;... A:=A[N];

    Program:

    Array 9 task. Fill an array of 10 elements with random numbers in the interval [-10..10] and perform a cyclic shift to the left without first element.
    Example: Initial array: 4 -5 3 10 -4 -6 8 -10 1 0 Result: 4 3 10 -4 -6 8 -10 1 0 -5

    Rearranging elements in an array

    Let's look at how an array is rearranged or reversed.

    Example: rearrange array elements in reverse order


    Solution:

    Algorithm:

    Pseudocode:

    Program:

    Array 10 task. Fill an array of 10 elements with random numbers in the interval [-10..10] and reverse all elements except the last one.
    Example: Source array: -5 3 10 -4 -6 8 -10 1 0 4 Result: 0 1 -10 8 -6 -4 10 3 -5 4

    Selecting elements and saving to another array

    Example: find elements in an array that satisfy some condition (for example, negative) and copy them to another array

    Solution:

    Solution: count the number of elements found using the count counter, install the next element in place B. Using a variable count must be assigned 1 .


    Output of array B:

    writeln("Selected items"); for i:=1 to count-1 do write(B[i], " ")

    Array task 11. Fill an array with random numbers in the interval and write into another array all the numbers that end in 0.
    Example: Source array: 40 57 30 71 84 Ends with 0: 40 30

    Sorting Array Elements

    Bubble sorting

    • In this type of sorting, the array is represented as water, small elements are bubbles in the water that float to the top (the lightest).
    • During the first iteration of the loop, the elements of the array are compared with each other in pairs: the penultimate with the last, the penultimate with the penultimate, etc. If the previous element turns out to be larger than the subsequent one, then they are exchanged.
    • During the second iteration of the loop, there is no need to compare the last element with the penultimate one. The last element is already in place, it is the largest. This means that the number of comparisons will be one less. The same goes for each subsequent iteration.

    Execution in Pascal:

    1 2 3 4 5 6 7 8 for i: = 1 to N- 1 do begin for j: = N- 1 downto i do if A[ j] > A[ j+ 1 ] then begin with : = A[ j] ; A[ j] : = A[ j+ 1 ] ; A[ j+ 1 ] : = с; end ; end ;

    for i:=1 to N-1 do begin for j:=N-1 downto i do if A[j] > A then begin from:= A[j]; A[j] := A; A := c; end; end;

    Array 12 task. Fill an array of 10 elements with random numbers in the interval and sort the first half of the array in ascending order, and the second in descending order (using the ‘Bubble’ method). Example: Source array: 14 25 13 30 76 58 32 11 41 97 Result: 13 14 25 30 76 97 58 41 32 11

    Sorting by selection

    • the minimum element is searched in the array and placed in first place (switched places with A);
    • Among the remaining elements, a search is also made for the minimum one, which is placed in second place (switched places with A), etc.

    begin c: = A[ i] ; A[ i] : = A[ min] ; A[ min] : = c; end ; end ;

    for i:= 1 to N-1 do begin min:= i ; for j:= i+1 to N do if A[j]< A then min:=j; if min <>i then begin c:=A[i]; A[i]:=A; A:=c; end; end;

    Array 13 task: Fill an array of 10 elements with random numbers in the interval and sort it in ascending order of the sum of the digits Example: Source array: 14 25 13 12 76 58 21 87 10 98 Result: 10 21 12 13 14 25 76 58 87 98

    Quick sort or quick sort

    Algorithm:

    Execution in Pascal:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 procedure QSort ( first, last: integer ) ; var L, R, c, X: integer ; begin if first< last then begin X: = A[ (first + last) div 2 ] ; L: = first; R: = last; while L <= R do begin while A[ L] < X do L: = L + 1 ; while A[ R] >X do R: = R - 1 ; if L<= R then begin c: = A[ L] ; A[ L] : = A[ R] ; A[ R] : = c; L: = L + 1 ; R: = R - 1 ; end ; end ; QSort(first, R) ; QSort(L, last) ; end ; end .

    procedure QSort(first, last: integer); var L, R, c, X: integer; begin if first< last then begin X:= A[(first + last) div 2]; L:= first; R:= last; while L <= R do begin while A[L] < X do L:= L + 1; while A[R] >X do R:= R - 1; if L<= R then begin c:= A[L]; A[L]:= A[R]; A[R]:= c; L:= L + 1; R:= R - 1; end; end; QSort(first, R); QSort(L, last); end; end.

    Array 14 task:
    Fill an array of 10 elements with random numbers in the range [-50..50] and sort it using the quick sort algorithm.