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