Program To Input And Output All The Elements Of An Array Using C/C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code
  4. Download Code


To copy code: Click View Source“View Source” on the right of this code snippet.

Background and Explanation of the code

About Arrays

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[5];

where int is the data type, marks is the name of the array and [5] 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[0] = 5
a[1] = 10
a[2] = -2
a[2] = 3
a[4] = 12