Content of the lesson:
An array can be described as a variable which can store more values (not only one).
It is a data structure which can store a sequence of values. An empty array can be illustrated as the following image:
As you see, the array is empty (there is nothing in the boxes) but the translator usually assigns them with a value after launching the program (usually using the value 0 in case of integers). Without any action, your array would look like the following one:
0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
You can fill such a data structure with any numbers:
5 | 8 | 7889 | 1 | 29 | 9 | 126 | 6656 | 778 | 2 |
It is a structure which contains several places to store values. A good analogy is for example
The previous array figures as one variable which contains 10 numbers. How to fill an array and use it?
We will use the analogy of squared paper - imagine you have cut away a row of 10 boxes. You want your program to write proper numbers in these boxes, so you have to tell it something like this:
You can see that every box has its identifier (a number) and this number is called an index. Using an index you can identify the right box. This situation is illustrated in the following table (at index 5 is stored value 29):
index | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
---|---|---|---|---|---|---|---|---|---|---|
value | 5 | 8 | 7889 | 1 | 29 | 9 | 126 | 6656 | 778 | 2 |
Declaration of an array of n items should look like this one:
type
pole = array[1..n]of integer;
After declaring this data type, you can declare a variable of this type:
type
pole = array[1..n]of integer;
var
p:pole;
In case you want to use an array of 10 items in your program, you should use:
type
pole = array[1..10]of integer;
var
p:pole;
If you want to insert a value inside the array p on a particular place, you can use this command:
p[i]:=hodnota;
The previous notation says that hodnota will be inserted at the i position inside the p array. Example of inserting value 5 in the first item.
type
pole = array[1..10]of integer;
var
p:pole;
begin
p[1]:=5;
end.
If you want to fill the whole array using previous values, you will have to assign the value to every item separately.
type
pole = array[1..10]of integer;
var
p:pole;
begin
p[1]:=5;
p[2]:=8;
p[3]:=7889;
p[4]:=1;
p[5]:=29;
p[6]:=9;
p[7]:=126;
p[8]:=6656;
p[9]:=778;
p[10]:=2;
end.
What are the advantages and disadvantages of this procedure? User is not allowed to enter any values, only the programmer can do it. If you want a user to be able to enter his values, you should change the example:
type
pole = array[1..10]of integer;
var
p:pole;
begin
readln(p[1]);
readln(p[2]);
readln(p[3]);
readln(p[4]);
readln(p[5]);
readln(p[6]);
readln(p[7]);
readln(p[8]);
readln(p[9]);
readln(p[10]);
end.
This procedure has also one disadvantage - user cannot insert as many variables as he want. On the top of that, imagine a situation when you want to insert 100 values for example... The second problem can be gracefully solved using a FOR cycle.
Program NaplneniAVypisPole;
type pole = array[1..10]of integer;
var p:pole;
i:integer;
begin
for i:=1 to 10 do
begin
readln(p[i]);
end;
for i:=1 to 10 do
begin
writeln(p[i]);
end;
readln;
end.
Program NaplneniAVypisPole;
type pole = array[1..5000]of integer;
var p:pole;
i, pocet:integer;
begin
writeln('Enter the number of items (1-5000)');
readln(pocet);
while ((pocet<1) or (pocet>5000)) do
begin
writeln('Enter the number of items
again:');
readln(pocet);
end;
for i:=1 to pocet do
begin
readln(p[i]);
end;
for i:=1 to pocet do
begin
writeln(p[i]);
end;
readln;
end.
Program NaplneniAVypisPole;
const n = 5000;
type pole = array[1..5000]of integer;
var p:pole;
i, pocet:integer;
begin
writeln('Enter the number of items (1-',n,')');
readln(pocet);
while ((pocet<1) or (pocet>n)) do
begin
writeln('Enter the number of items
again:');
readln(pocet);
end;
for i:=1 to pocet do
begin
readln(p[i]);
end;
for i:=1 to pocet do
begin
writeln(p[i]);
end;
readln;
end.
Write a program for entering payments for employees in a company. It should accept payments until user enters 0 (this will be the end of inserting). After getting all values the program should write them and show their number.
Program Vyplaty;
const n = 1000;
type pole = array[1..n]of integer;
var p:pole;
i,pocet,vyplata:integer;
begin
writeln('Enter payments (0 for end):');
vyplata:=1;
i:=1;
while (vyplata<>0) do
begin
readln(vyplata);
p[i]:=vyplata;
i:=i+1;
end;
pocet:=i-2;
writeln('Entered payments:);
for i:=1 to pocet do
begin
writeln(p[i],' ');
end;
writeln('Count of entered payments is ',pocet);
readln;
end.
The program is not completely correct. Try to solve any mistake you find.
Create a program for inserting payments for employees (the maximum number is 100 payments). Then the program should write all payments, count an arithmetic mean of payments and write the lowest and highest values. It should also write the final sum needed for all payments. Try to format the output of payments to separate values with the comma character and to write a dot behind the last payment value.