Whats in this post?
- Background and Explanation of the code
- Download Code
To copy code: Click “View Source” on the right of this code snippet.
Background and Explanation of the code
An array is a linear collection of similar elements with identical data type that shares a common name. Arrays are used to handle large amount of data without the need to declare many individual variables separately. The array elements are stored in the contiguous memory locations(i.e. one after the other). Generally, the size of an array remains fixed and must be specified during array declaration and before they can be used to store information.
An array is usually represented as: int marks;
where int is the data type, marks is the name of the array and  is the size of the array enclosed in the square brackets.
Explanation of the program
1. The above program first reads the array elements of an array and then prints the values stored in them.
2. The a[i] corresponds to the (i-1)th element of the array ‘a’. The loop variable ‘i’ in for loop starts from ‘0’ (representing index for first element) to 4 so as to input or display ‘5’ elements of array ‘a’.
3. The statement cin>>a[i] in the for loop inputs various elements of array ‘a’ corresponding to each variable of index ‘i’, which are then displayed using another ‘for’ loop and using the statement cout<<a[i].
Enter elements of an array
5 10 -2 3 12 (Press Enter)
a = 5
a = 10
a = -2
a = 3
a = 12