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 10 items
  int p[10];

Filling and Listing an Array

If you want to insert a value inside the array p on a particular place i (index), 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
int p[10];
p[0] = 5;

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
 int p[10];
 p[0]=5;
 p[1]=8;
 p[2]=7889;
 p[3]=1;
 p[4]=29;
 p[5]=9;
 p[6]=126;
 p[7]=6656;
 p[8]=778;
 p[9]=2;

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
 int p[10];
 scanf("%d", &p[0]);
 scanf("%d", &p[1]);
 scanf("%d", &p[2]);
 scanf("%d", &p[3]);
 scanf("%d", &p[4]);
 scanf("%d", &p[5]);
 scanf("%d", &p[6]);
 scanf("%d", &p[7]);
 scanf("%d", &p[8]);
 scanf("%d", &p[9]);

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.

Inserting values inside an array
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
	int i;
    int p[10];
    
    for (i = 0; i < 10; i++)
    {
    	scanf("%d",  &p[i]);
    }
    
    for (i = 0; i < 10; i++)
    {
    	printf("%d\n",  p[i]);
    }
    
    fflush(stdin);
    getchar();
    return 0;
}

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
#include "stdafx.h"

int _tmain(int argc, _TCHAR* argv[])
{
    int i, pocet;
    int p[5000];
    
    printf("Zadejte pocet vkladanych prvku: ");
    scanf("%d", &pocet);
    
    while (pocet < 1 || pocet > 5000)
    {
    	printf("Zadej znovu: ");
        scanf("%d", &pocet); 
    }
    
    for (i = 0; i < pocet; i++)
    {
    	scanf("%d",  &p[i]);
    }
    
    for (i = 0; i < pocet; i++)
    {
    	printf("%d",  p[i]);
    }
    
    fflush(stdin);
    getchar();
    return 0;
}

Filling an array and writing its content with the possibility to check the array and using a constant

Filling and Listing an Array with the Possibility to Enter the Number or Items
#include "stdafx.h"
#define N 5000

int _tmain(int argc, _TCHAR* argv[])
{
    int i, pocet;
    int p[N];
    
    printf("Zadejte pocet vkladanych prvku (1 - %d): ", N);
    scanf("%d", &pocet);
    
    while (pocet < 1 || pocet > N)
    {
    	printf("Zadej znovu: ");
        scanf("%d", &pocet); 
    }
    
    for (i = 0; i < pocet; i++)
    {
    	scanf("%d",  &p[i]);
    }
    
    for (i = 0; i < pocet; i++)
    {
    	printf("%d",  p[i]);
    }
    
    fflush(stdin);
    getchar();
    return 0;
}

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 výplaty
#include "stdafx.h"
#define N 1000

int _tmain(int argc, _TCHAR* argv[])
{
    int i, pocet, vyplata;
    int p[N];
    
    printf("Zadejte pocet vkladanych prvku (1 - %d): ", N);
    scanf("%d", &pocet);
    
    while (pocet < 1 || pocet > N)
    {
    	printf("Zadej znovu: ");
        scanf("%d", &pocet); 
    }
    
    for (i = 0; i < pocet; i++)
    {
    	scanf("%d",  &p[i]);
    }
    


    printf("Zadejte vyplaty (0 konci zadavani):\n");
    vyplata=1;
    i=1;
    while (vyplata!=0) 
    {
        scanf("%d",&vyplata);
        p[i-1] = vyplata;
    	i = i + 1;
    }        

    pocet=i-2;

    printf("Zadane vyplaty:\n"); 

    for (i = 0; i < pocet; i++)
    {
    	printf("%d\n",  p[i]);
    }

    printf("Pocet zadanych vyplat je %d\n",pocet);
    
    fflush(stdin);
    getchar();
    return 0;
}

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áš