Arrays in programming languages C and C++
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.
Figure 1: Defining an 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:
Figure 2: Accessing array elements
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];
}
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: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];
}
Figure 3: Example of an array, with a state in memory
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:
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;
