Single / One Dimensional Arrays in C++
What is a Single / One Dimensional arrays in C++?
Explanation
Single / One Dimensional Array is an array having a single index value to represent the arrays element.
Syntax:
type array_name[array_size_1]
Example :
#include <iostream.h> void main() { int i; float mark[6]; cout << "Enter the marks of your 6 subjects:: \n"; for(i=0; i<6; i++) { cin >> mark[i]; } float sum=0; for(i=0;i<6;i++) { sum += mark[i]; } float ave = sum/6; cout << "Average Marks is::" << ave << '\n'; } |
Result :
Enter a the mark of your 6 sujects::
45
56
67
58
60
59
Average Marks is::57.5
In the above example, the array "mark" refers the elements of an array by the index value "6". The marks entered are stored in the array using the index value of the array in a loop. The array elements are retrieved to calculate the sum of the array, then the average is found.