Czech version
logolink

< Back to the list of lessons

Array

AlgoritmyContent of the lesson:

  • Characteristic of the Array Data Type
  • Defining the Array Data Type
  • Filling and Listing an Array
  • Inserting any Number of Elements into Array
  • Program for Payments

Characteristic of Array Data Type

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

  • a row from squared paper if you write a whole number in each box (an array of integers)
  • a row of drawers which contain lists of paper where you can write anything (an array of strings)

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:

  • Write the number 5 into the first box.
  • Write the number 8 into the second box.
  • Write the number 7889 into the third box.
  • ...
  • Write the number 2 into the tenth box.

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

Defining the Array Data Type

Declaration of an array of n items should look like this one:

Declaration of an array of n items
type   pole = array[1..n]of integer;

After declaring this data type, you can declare a variable of this type:

Declaration of an array of n items
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:

Declaration of an array of 10 items
type   pole = array[1..10]of integer;
var   p:pole;

Filling and Listing an Array

If you want to insert a value inside the array p on a particular place, you can use this command:

Inserting a value inside an array
 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.

Inserting a value inside an array
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.

Inserting values inside an array
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:

Inserting values inside an array
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.

Filling an array and writing its content
 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.

Filling an array and writing its content with the possibility to enter the number or items

Filling and Listing an Array with the Possibility to Enter the Number or Items
 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.

Filling an array and writing its content with the possibility to enter the number or items and using a constant

Filling and Listing an Array with the Possibility to Enter the Number or Items
 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.

Program for Payments

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 for Payments
 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.

Program for Payments, Second Version

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.

Additional Texts

Links

Questions

  1. What does the term array mean in Pascal?
  2. Try to illustrate the appearance of the array structure.
  3. How can you declare the array data type?
  4. What are the advantages of the array data type?
  5. Write a source code which will fill an array of n items with values gained from an user.
webdesign, xhtml, css, php - Mgr. Michal Mikláš