Arrays in programming languages C and C++
Array variables are used when it is necessary to remember in memory more data of the same type, e.g. int. Consider the following task: It is necessary to enter grades from n subjects and calculate the highest grade.
We reserve the memory for the grade data, and then using the cycle we try to enter n grade (eg N = 5):
We reserve the memory for the grade data, and then using the cycle we try to enter n grade (eg N = 5):
Example 1: Take inputs of grades from user using for loop
int grade, n=5;
//Taking inputs from user and store them in array
for(int i=0; i < n; i++)
{
cout<<"Enter "+(i+1)+". grade";
cin>>grade;
}
//Taking inputs from user and store them in array
for(int i=0; i < n; i++)
{
cout<<"Enter "+(i+1)+". grade";
cin>>grade;
}
However, because grades only remembers one number, after leaving the for cycle only the last grade will be remembered. The task will not be solved in this way. The highest grade (maximum) cannot be determined further, as not all grades are memorized. Grades could be remembered if we used an variable of an array instead of the usual variable, which can store more data of the same type. Let's introduce a arrays:
int grades[n];
…
The name of the array is: grades. The previous expression reserved memory for n integers, because the value in square brackets is actually the dimension of the array.
int grades[n];
…
The name of the array is: grades. The previous expression reserved memory for n integers, because the value in square brackets is actually the dimension of the array.
The figure shows the memory reserved for at least 5 elements. The numbers below the field represent the index of the array field. The first member of the sequence has the index 0, and the last n-1, ie. in this case it is 4. If we want to enter a grade of 5 in the field with index 2 (third field) we have written:
grade [2] = 5;
Here's what the memory state would look like in that case:
grade [2] = 5;
Here's what the memory state would look like in that case:
Entering grades using the cin input command, through the for loop would now look like:
Example 2: Take inputs of array elements from user using for loop
int n=5;
int grade[n];
//Taking inputs from user and store them in array
for(int i=0; i < n; i++)
{
cout<<"Enter "+(i+1)+". grade";
cin>>grade[i];
}
int grade[n];
//Taking inputs from user and store them in array
for(int i=0; i < n; i++)
{
cout<<"Enter "+(i+1)+". grade";
cin>>grade[i];
}
The array of grades would be filled in order through cycles. In the 1st cycle when i = 0, the value entered at standard input would go to the field with index 0, in the 2nd cycle when i = 1 in the grades [1], which is the second field in the array and so on until the end of array. At the end of the for loop, the state in memory would look like:
Determining the maximum value of an array (maximum)
Now that all the grades are stored in memory, we can extract them as needed and, say, determine the highest grade in the array.
The maximum grade is determined as follows:
int max = grades [0];
First, an integer variable is defined that will represent the maximum grade and it is assumed that one is the first grade in the array, ie. its initial value , grades [0]. It is further moving trough the loop and the next grade in the order of grades [i] is accessed. It is checked in each cycle whether the new score is higher than the current maximum and if so, that value becomes the maximum. It looks like this:
The maximum grade is determined as follows:
int max = grades [0];
First, an integer variable is defined that will represent the maximum grade and it is assumed that one is the first grade in the array, ie. its initial value , grades [0]. It is further moving trough the loop and the next grade in the order of grades [i] is accessed. It is checked in each cycle whether the new score is higher than the current maximum and if so, that value becomes the maximum. It looks like this:
Example 3:Determining the largest value in an array
#include < stdio.h >
#include < stdlib.h >
int main()
{
#include < stdlib.h >
int main()
{
const int n = 5;
int grade[n];
//Taking inputs from user and store them in array
for(int i=0; i < n; i++)
{
//Determining the largest value in an array
int max=grades[0];
for(int i=1; i< n; i++)
{
cout << "max grade is " << max;
}int grade[n];
//Taking inputs from user and store them in array
for(int i=0; i < n; i++)
{
cout << "Enter "+(i+1)+". grade";
cin >> grade[i];
}cin >> grade[i];
//Determining the largest value in an array
int max=grades[0];
for(int i=1; i< n; i++)
{
if(grades[i]>max)
}
max=grades[i];
cout << "max grade is " << max;
Determining the maximum sum of consecutive subarray
Within a one-dimensional array of numbers it is necessary to find the subset of consecutive numbers that has the bigest sum (sum). Eg for the array:
3.5, -10, -34.16 2 the bigest sum has a subarray 16, 2 which value is 18. For a complete explanation and animation explaining the algorithm, see webpage: Maximum Sum of Contiguous Subarrays |
Declaring and defining arrays
Definition of an array of 10 integers (then the memory is reserved for that array):
int A[10];
This will provide memory space for 10 integers. If we tried to access the 11th element of the array, the program would abort execution at that point due to an error.
The disadvantage of arrays is that, when we do not know in advance how many elements the array will contain, we have to reserve more space than we expect, just in case. Some of these places will probably remain unused, but the program will not be interrupted due to that.
The possibility to reserve the number of seats exactly as many as will be needed is solved by using collections, but that is the subject of an exhibition at a higher course.
int A[10];
This will provide memory space for 10 integers. If we tried to access the 11th element of the array, the program would abort execution at that point due to an error.
The disadvantage of arrays is that, when we do not know in advance how many elements the array will contain, we have to reserve more space than we expect, just in case. Some of these places will probably remain unused, but the program will not be interrupted due to that.
The possibility to reserve the number of seats exactly as many as will be needed is solved by using collections, but that is the subject of an exhibition at a higher course.
Defining an array of real numbers
An array of real numbers is defined similarly to an array of integer, with double or float instead of int.
float A[10]; or
double A[10];
float A[10]; or
double A[10];
Giving values to elements of array
The elements of an array are accessed using indexes starting from 0. E.g. if the number of real numbers is five , defining and assigning values to the elements would be:
double [ ] A= new double [5];
A[0] = 1.1;
A[1] = 2.0;
A[2] = 2.5;
A[3] = - 1.6;
A[4] = 3.3;
The array would then look like:
double [ ] A= new double [5];
A[0] = 1.1;
A[1] = 2.0;
A[2] = 2.5;
A[3] = - 1.6;
A[4] = 3.3;
The array would then look like:
Giving values to array elements wich they are known
If we know the values of the array elements in advance, then the array is initialized together with the array definition. The previous array is initialized as follows:
double A[ ] = {1.1, 2.0, 2.5, -1.6, 3.3};
double A[ ] = {1.1, 2.0, 2.5, -1.6, 3.3};
EXAMPLE 5: Entering elements of an array from a user and determining the number of positive ones:
Task text: Enter the number of elements, then enter the members of the integer array and determine how many of them are positive.
Solution:
First you need to enter the number of elements of the sequence n, in order to be able to define an array of real numbers:
First you need to enter the number of elements of the sequence n, in order to be able to define an array of real numbers:
int n;
cout << "Enter the number of elements of an array" << endl;
cin >> n;
cout << "Enter the number of elements of an array" << endl;
cin >> n;
Then the memory is reserved for a series of integers that will have n elements:
// Define a string of n elements
int A [n];
Now you can enter the elements:
//Entering an array
for(int i=0; i < n; i++)
{
cout<<"Enter "+(i+1)+". element of an array";
cin>>A[i];
}
for(int i=0; i < n; i++)
{
cout<<"Enter "+(i+1)+". element of an array";
cin>>A[i];
}
The array of real numbers is then printed as follows using a loop:
//Printing an array
for(int i=0; i < n; i++)
{
cout << A[i] << " ";
}
for(int i=0; i < n; i++)
{
cout << A[i] << " ";
}
In the second part, using a loop with the same parameters as when entering elements and printing array elements, a number of elements that are positive are calculated:
// Determining the number of positive elements
// Determining the number of positive elements
int numOfPos=0;
for(int i=0; i < n; i++)
{
if( A[i] > 0)
{
numOfPos++;
}
}
cout<< "Number of positive one is " << numOfPos<< endl;
}
for(int i=0; i < n; i++)
{
if( A[i] > 0)
{
numOfPos++;
}
}
cout<< "Number of positive one is " << numOfPos<< endl;
}
EXAMPLE 6: Entering array elements from a user and specifying the bigest element:
Task text: Enter the number of elements, then enter the real numbers of the array and determine maximum of them.
Solution:
We must first define the array.
We must first define the array.
int n;
cout << "Enter the nuber of elements" << endl;
cin >> n;
cout << "Enter the nuber of elements" << endl;
cin >> n;
Suppose that the five elements of a string from a user are entered and that the values are entered as shown in the figure below
Determining the maximum can be done as follows:
- Assume that the maximum is the first element of the array
double max;
max= a[0];
max= a[0];
- Then the current element of the array is changed through the for loop and it is checked whether it is greater than the assumed maximum. If so, that element becomes the maximum.
for(int i=0; i < n; i++)
{
if( a[i] > max)
{
max=a[i];
}
}
cout<< "max=" << max << endl;
}
{
if( a[i] > max)
{
max=a[i];
}
}
cout<< "max=" << max << endl;
}
The determination of the minimum is done in a similar way, only as a condition it is checked whether the current member is lower than the assumed minimum.
double min;
min= a[0];
for(int i=0; i < n; i++)
{
if( a[i] < min)
{
min=a[i];
}
}
cout<< "min=" << min << endl;
}
min= a[0];
for(int i=0; i < n; i++)
{
if( a[i] < min)
{
min=a[i];
}
}
cout<< "min=" << min << endl;
}
Two-dimensional arrays
Two-dimensional arrays or matrices are used to place numbers in multiple rows and columns. Read more about it on the website:
Two dimensional array-matrix |
Image by Gerd Altmann from Pixabay
|
Previous
|< Loops in C/C++ |
Next
Dynamic array-vector >| |