Arrays 10th grade Kssh 4 Stepanov P.G. Arrays (by hour) n Array definition. Description of an array in Pascal. Filling methods. Array definition. - presentation


Answers to the topic One-dimensional arrays of integers

Textbook on Computer Science 9th grade Bosova

Read the presentation materials for the paragraph contained in the electronic supplement to the textbook. Use these materials when preparing answers to questions and completing assignments.

Presentation: Go

Can an array contain integer and real values ​​at the same time?

Yes, it can, if the array is of real type.

Why is an array description necessary?

An array description is necessary to allocate a memory area of ​​the required size to store the array.

Arrays:

a) for i:=1 to 10 do a :=random(101)-50 This array will be formed as follows: array values ​​from a[1] to a[10] will be assigned a random integer belonging to the segment [-50;
50]; b) for i:=1 to 20 do a:=a:=i The following will be true for array values ​​from a[1] to a[20]: the i-th array will be assigned the i-th value (the value coinciding with element index);

c) for i:=1 to 5 do a :=a :=2*i-1 The following will be true for the array values ​​from a[1] to a[5]: the i-th array will be assigned the difference of numbers (2*i) and units.

The number of residents is randomly from 50 to 200 people, and the number of houses is n=30

To solve the problem, we will declare an array with an integer data type.
For each house, we will assign a random number of people living in the range from 50 to 200. And at the end we will add up the values ​​of the array, which contains information about the number of people living in a certain house. Pascal problem:

program z_5; var i, s: integer; a: array [1..30] of integer;

BEGIN

s:=0;
for i:=1 to 30 do begin a := random(50, 200); s:=s + a ;
end; writeln('In 30 houses ', s, ' inhabitants.'); END.

A program that calculates the average temperature for a week. Initial data is entered from the keyboard.

Initial data:
P=12;
B=10; C=16; H=18; P=17; C=16; B=14. Output
: 14.71

Pascal code:

program z_6;
var i: integer; s:real; a: array [1..7] of real; d: array [1..7] of string; BEGIN s:=0; writeln('Enter temperature'); for i:=1 to 7 do begin if i=1 then write('Monday>>'); if i=2 then write('Tuesday>>'); if i=3 then write('Wednesday>>'); if i=4 then write('Thursday>>'); if i=5 then write('Friday>>'); if i=6 then write('Saturday>>'); if i=7 then write('Sunday>>'); readln(a ); s:=s + a ; end; s:=s/7; writeln('Average temperature for the week: ', s:0:2); END.

Given an array of ten integers. Determine how many array elements have the maximum value.

program z_7; var i, s, m: integer; a: array [1..10] of integer; begin s:=0; m:=0; writeln('Enter 10 integers:'); for i:=1 to 10 do begin write (i, ' number: '); readln(a ); if a > s then s := a ;
end; for i := 1 to 10 do begin if a=s then m:=m+1; end;

writeln(m, ' array element(s) have maximum value.'); end.

Which stage of solving a problem on a PC is the most time-consuming?

First, using a loop with the for parameter,
we will announce the grades to 20 students. Ratings will be random. Then we will determine the number of twos, threes, fours and fives received for the dictation.

program z_8; var i, s2, s3, s4, s5: integer; a: array [1..20] of integer; begin for i:=1 to 20 do begin a := random(2, 5); if a = 2 then s2:=s2+1; if a = 3 then s3:=s3+1; if a = 4 then s4:=s4+1; if a = 5 then s5:=s5+1; end;

writeln('Twos: ', s2, '; Threes: ', s3, '; Fours: ', s4, '; Fives: ', s5); end.

Recruitment for the school basketball team has been announced. The height of each of n students who want to join this team is known. Create an algorithm for calculating the number of applicants who have a chance to make the team if the player’s height is at least 170.

Random student height in the range from 150 to 200 cm, number of applicants n=50;
Program in PascalABC: program z_9; var i, s: integer; a: array [1..50] of integer; begin s:=0; for i:=1 to 50 do begin a := random(150, 200); if a>=170 then s:=s+1; end; writeln(s, ' student(s) may qualify for the basketball team.'); end.

10.

program z_10; var a, b: array [1..10] of integer; s: array [1..10] of real; i:integer; max: real; BEGIN randomize; for i:=1 to 10 do begin a :=random(10)+1; b :=random(10)+1; s :=a *b /2; writeln('a[', i, '] = ', a :6, ' b[' , i, '] = ', b : 6, ' s[', i, '] = ', s :6: 2); end; max:=s[1]; for i:=2 to 10 do if s >max then max:=s ; for i:=1 to 10 do if s =max then writeln ('i = ', i, ' a = ', a , ' b = ', b , ' s = ', s ); END.

Enter information about 10 European countries into arrays. List the names of the countries in order of increasing population density.

The area is given in thousand km^2, the population is in thousand people.
program z_11; var p:array [1..10] of real = (244.1, 357, 132, 43.1, 504.8, 301.2, 312.7, 92.0, 337, 551); n: array [1..10] of string; i, j, imin: integer; x: real; r:string; const k: array [1..10] of real = (58100, 81200, 10300, 5190, 39100, 57200, 38500, 9800, 5060, 57800); BEGIN n[1]:='UK'; n[2]:='Germany'; n[3]:='Greece'; n[4]:='Denmark'; n[5]:='Spain'; n[6]:='Italy'; n[7]:='Poland'; n[8]:='Portugal'; n[9]:='Finland'; n[10]:='France'; for i:=1 to 9 do begin imin:=i; for j:=i+1 to 10 do if p[j] x:=p ; p :=p[imin]; p[imin]:=x; r:=n ; n :=n[imin]; n[imin]:=r; end; writeln('Sorted information'); for i:=1 to 10 do writeln (n , ' — ', p ); END.

Paragraph 2.1TopParagraph 2.3

Independent work on the topic “One-dimensional arrays” test in computer science and ICT (grade 9) on the topic

Independent work No. 1

OPTION 1

1. Create a program to input and output an array: 5, -2, 3, -6, 7, 9, -4, 8, 1, 2, 7, -3. 2. Make a block diagram of the algorithm and a program for calculating the number of positive elements in the array A(10). 3*. Write a program to fill an array, without using the keyboard, with the numbers: 1, 3, 5, 7, 9, 11, 13, 15, 17, 19.

OPTION 2

1. Make a program for input and output of the array: 3, 4, -9, 8, 1, -6, 2, -5, -1, 7, 5, 9. 2. Make a block diagram of the algorithm and a calculation program in the array A (10) the number of negative elements. 3*. Write a program to fill an array without using the keyboard with the numbers: 2, 4, 6, 8, 10, 12, 14, 16, 18, 20.

OPTION 3

1. Make a program for input and output of the array: 4, -8, -7, -3, 1, 8, 4, 8, 6, 1, 15, 5. 2. Make a block diagram of the algorithm and a program for calculating the array A( 10) the number of even elements. 3*. Write a program to fill an array, without using the keyboard, with the numbers: 1, -3, 5, -7, 9, -11, 13, -15.

OPTION 4

1. Make a program for input and output of the array: 5, -3, 4, -8, -9, 1, 9, 6, -3, 4, 8, 2. 2. Make a block diagram of the algorithm and a calculation program in the array A (10) number of odd elements. 3*. Write a program to fill an array, without using the keyboard, with the numbers: -2, 4, -6, 8, -10, 12, -14, 16, -18, 20.

Independent work No. 2

OPTION 1

1. Make a block diagram of the algorithm and a program for calculating the sum of all numbers that are multiples of 3 in the array A(N). 2. Make a program for calculating the sum of negative elements in the array B(8). 3*. Write a program to calculate the arithmetic mean of array elements.

OPTION 2

1. Make a block diagram of the algorithm and a program for calculating the sum of all numbers that are multiples of 5 in the array A(N). 2. Make a program for calculating the sum of positive elements in the array B(8). 3*. Write a program to calculate the arithmetic mean of negative elements.

OPTION 3

1. Make a block diagram of the algorithm and a program for calculating the sum of all numbers that are multiples of 8 in the array A(N). 2. Make a program for calculating the sum of even elements in the array B(8). 3*. Write a program to calculate the arithmetic mean of odd-numbered elements.

OPTION 4

1. Make a block diagram of the algorithm and a program for calculating the sum of all numbers that are multiples of 7 in the array A(N). 2. Make a program for calculating the sum of odd elements in the array B(8). 3*. Write a program to calculate the arithmetic mean of even-numbered elements.

Independent work No. 3

OPTION 1

1. Make a block diagram of the algorithm and a program for calculating the product of positive elements in the array A(N). 2. Create a program for calculating the minimum element and its number in the array B(N). 3*. Write a program to calculate the sum of elements from the first negative to the end.

OPTION 2

1. Make a block diagram of the algorithm and a program for calculating the product of negative elements in the array A(N). 2. Create a program for calculating the maximum element and its number in the array B(N). 3*. Write a program to calculate the sum of elements from the first positive to the end.

OPTION 3

1. Make a block diagram of the algorithm and a program for calculating the product of even elements in the array A(N). 2. Write a program for calculating the minimum number of positive elements in the array B(N) and its number. 3*. Write a program to calculate the sum of elements from the second negative to the end.

OPTION 4

1. Make a block diagram of the algorithm and a program for calculating the product of odd elements in the array A(N). 2. Create a program for calculating in the array B(N) the maximum among negative elements and its number. 3*. Write a program to calculate the sum of elements from the second positive to the end.

Independent work No. 4

OPTION 1

1. Write a program for arranging elements in the array A(N) in ascending order. 2. Write a program for swapping the first and last elements in the array B(N). 3*. Write a program to remove the second element of the array C(N).

OPTION 2

1. Write a program for arranging elements in the array A(N) in descending order. 2. Write a program for swapping the second and penultimate element in the array B(N). 3*. Write a program to remove the fifth element of the array C(N).

OPTION 3

1. Write a program for arranging elements in the array A(N) so that the negative ones are at the end. 2. Write a program for swapping the first negative and maximum elements in the array B(N). 3*. Write a program to remove the minimum element of the array C(N).

OPTION 4

1. Write a program for arranging elements in the array A(N) so that the positive ones are at the end. 2. Write a program for swapping the first positive and minimal elements in the array B(N). 3*. Write a program to remove the maximum element of the array C(N).

Independent work No. 5

OPTION 1

1. Write a program to increase all elements of the array by 4. 2. Write a program to insert the first element equal to 7. 3* into the array A(N). Write a program for forming two arrays from the array A(N), in which the first array is filled with positive elements, and the second - with negative elements.

OPTION 2

1. Write a program to reduce all elements of an array by 4. 2. Write a program to insert the last element equal to 7. 3* into the array A(N). Write a program for forming two arrays from the array A(N), in which the first array is filled with negative elements, and the second with positive elements.

OPTION 3

1. Write a program to increase all array elements by 5 times. 2. Write a program for inserting the fifth element into array A(N), equal to 8.3*. Compose a program for forming two arrays from the array A(N), in which the first array is filled with the preceding maximum elements, and the second - with the remaining elements.

OPTION 4

1. Write a program to reduce all array elements by 10 times. 2. Write a program for inserting into array A(N) the third element from the end of the array, equal to 7. 3*. Compose a program for forming two arrays from the array A(N), in which the first array is filled with the preceding minimum elements, and the second - with the remaining elements.

Pascal: Lesson No. 6. One-dimensional arrays (complex problems in Pascal)

This lesson will cover different levels of complexity of problems in Pascal for working with one-dimensional arrays.

The goal of the site labs-org.ru is to provide users with practical skills in working with the language. Lessons on Pascal, presented in increasing complexity and supported by clear, solved examples, will allow you to easily master the material being studied.

Back

To contents

Further

First, let's look at a not very complicated problem with one-dimensional arrays in Pascal:

Example: Find the maximum element of a numeric array and its index ( max_num

and
max_index
).

Result:

Think about whether a counter is useful in the program or not, whether additional variables are needed.

Show program listing:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21const m = 20; var arr: array[1..m] of byte; max_index: byte; i:byte; begin randomize; for i := 1 to m do begin arr := random(100); write(arr :3); end; max_index := 1; for i := 2 to m do if arr > arr[max_index] then begin max_index := i; end; writeln; writeln('Max = ',arr[max_index]); writeln('position: ', max_index); end.

const m = 20; var arr: array[1..m] of byte; max_index: byte; i:byte; begin randomize; for i := 1 to m do begin arr := random(100); write(arr :3); end; max_index := 1; for i := 2 to m do if arr > arr[max_index] then begin max_index := i; end; writeln; writeln('Max = ',arr[max_index]); writeln('position: ', max_index); end.


Problem Array 15. You need to fill an array with numbers entered by the user and calculate their sum. If the user enters zero or the array size is exceeded, then prompts for input should stop. Use a loop with a postcondition (repeat) Result:

Number: 3 Number: 2 Number: 5 Number: 0 3 2 5 0 sum = 10

Problem Array 16. Given an array of 50 elements, the values ​​of which are generated by the random function and lie in the range from -50 to 49 inclusive. You need to copy values ​​in the range from -5 to 5 inclusive from one array to another array and count their number.

Array problem 17. In a one-dimensional array (filling the array with random numbers from -50 to 49) find the sum of negative elements. If this sum is less than -100, then you need to add the minimum positive element to it.

The following complex problem with one-dimensional arrays should be analyzed in detail.

Example: You have a one-dimensional array containing numbers from 0 to 49 inclusive. It is required to exclude from it all elements whose values ​​are less than 15.

The difficulty of this task is that you don’t just need to delete elements whose values ​​are , but you also need to move the remaining elements behind the one being deleted to the left. As if compressing the array so that there are no “empty” elements left.

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 24 25const n=20; var arr: array[1..n] of integer; i,j,m:integer; begin randomize; for i:=1 to n do begin arr :=random(50); write(arr :4); end; writeln; m:=n; i:=1; while i<=m do if arr <15 then begin for j:=i to m-1 do arr[j]:=arr[j+1]; m:=m-1 end else i:=i+1; writeln('Result:'); for i:=1 to m do write(arr :4) end.

const n=20; var arr: array[1..n] of integer; i,j,m:integer; begin randomize; for i:=1 to n do begin arr :=random(50); write(arr :4); end; writeln; m:=n; i:=1; while i<=m do if arr <15 then begin for j:=i to m-1 do arr[j]:=arr[j+1]; m:=m-1 end else i:=i+1; writeln('Result:'); for i:=1 to m do write(arr :4) end.
Let's consider the presented algorithm for solving this complex problem with a one-dimensional array:

  • line 12: Here the assignment of the value n to the variable m is required because n is a constant and cannot be changed. Since the while loop will remove some elements while “watching” the array, the value of m, which represents the length of the array, will decrease.
  • line 21: If the next element is not deleted, then we move on to viewing the next element (i := i + 1) and do not reduce the array (m does not change).

Problem Array 18. Given an array of N elements in the range [100;300]. Compress the array, leaving in it only those elements whose sum of digits is even. 101 245 167 295 133 >>> 101(2) 167(14) 295(16)

Array task 19. Fill an array of 10 elements with random numbers in the interval [0..4] and determine whether it contains identical neighboring elements. Example:

Source array: 4 0 1 2 0 1 3 1 1 0 Answer: yes

Array 20 task.

  1. Determine the number of the first element in array A, equal to X.
  2. Determine the number of the first element, equal to X, in the first half of the array A (the array has an even number of elements).
  3. Determine the number of the first element, equal to X, in the second half of the array A (the array has an even number of elements).

Array Problem 21. Find the number of different numbers in a one-dimensional array

Let's consider the solution algorithm:

Array task 22. Fill an array of 10 elements with random numbers in the interval [-10..10] and reverse separately for the 1st and 2nd halves of the array. Example:

Array task 23. Fill an array of 12 elements with random numbers in the interval [-12..12] and perform a cyclic shift to the RIGHT by 4 elements. Example:

Practice solving problems on the topic by clicking on the icon:

Back

To contents

Further

Static Arrays

An array in computer science is a structure that includes a number of elements of the same type. They are especially useful in cyclical work with the same type of data. Arrays can be classified in two ways: by type of memory organization and by dimension. For static arrays, space is allocated at the compilation stage, for dynamic arrays - while the program is running. As a rule, one-dimensional and two-dimensional arrays are considered.

Basic Concepts

For "dummies" it is better to start with static one-dimensional ones. The array declaration in the var block contains the following components:

  1. Variable name.
  2. The service word is array (literally “set”).
  3. The starting and ending indexes are in square brackets, the separator is 2 dots.
  4. Data type starting from of.

An entry like “var mass: array[0.10] of integer” can be read as “an array named mass with 11 elements of integer type.” In general, the range starts at 0 and ends with a number that will exceed the maximum number of elements required. A given interval can begin with 1 or any other number if this is preferable for perception or more convenient for a particular task.

In the case of a static array, the description must be treated carefully, since if insufficient memory is allocated, a runtime error associated with out of range will occur. It occurs even in the simplest task of filling an array from the keyboard and then printing it. Using mass as an example, accessing a specific element will look like mass[x], where x is any variable or number (integer type).

It is in this form that it can be passed for printing or reading to the corresponding standard function. The task will be simplified by a for loop regarding the variable x, with a range in accordance with the one described earlier. It can be further limited (for example, by a number entered by the user), but it must not exceed the boundaries of the allocated memory.

Other nuances

Sometimes it is necessary to store a certain amount of data of the same type that is not entered by the user - for example, the alphabet of vowels to count their number in a word. In such cases, the variable is considered constant, and it is described as “const vowels: array[0.5] of char = ('e', 'y', 'u', 'i', 'o', 'a')". An interesting feature of the Pascal language is the ability to use letters (characters) as indices. For example, "var alpha_count: array[az] of integer" is a good option for storing the number of occurrences of each letter in text.

A two-dimensional array is no more difficult to declare than a one-dimensional one: “var table: array[1.10,1.10] of integer.” It can be visually represented as a table of 10 rows and 10 columns containing integers in cells. Since accessing (table[y, x]) requires 2 parameters, nested for loops are used to search. They are organized so that all elements in a line are considered sequentially (for y, all possible x are examined in turn).

Rating
( 2 ratings, average 5 out of 5 )
Did you like the article? Share with friends:
Для любых предложений по сайту: [email protected]