Understanding Array in C++
Definition Of Array
An array is a collection of data having the same data type and the entire collection can be accessed using the same name.
A specific element of an array can be obtained based on the index of the element.
Explanation of Arrays
Each data in an array is known as an array element. The reason that we need "Arrays" concept in programming can be explained using an example. Let us consider the tracking of business sales for 2017. It is necessary that the sales receipts have to be filed. This can be done by having a separate file for each month, but it would be more convenient if we can simplify it by maintaining a single folder with 12 different files, one for each month.
Let us imagine the designing of a program to keep track of the business sales. Conventionally speaking, the program could declare 12 separate variables for each month's total sales. This is analogous to having 12 separate files for your receipts. But with the help of arrays, the total business sales can be stored in a single array of 12 elements. This is analogous to filing your sales receipts in a single folder with 12 files.
Single-Dimensional Arrays
In a single-dimensional array, the array name is followed by a number which is mentioned inside square brackets. This number denotes the size of the array. In the business sales program example mentioned above, an array of type "int" and name "sales" can be declared as :
int sales[12];
This declaration denotes that an array of name "sales" is created with 12 elements. Each element is an exact equivalent of a single int variable. The array elements are always numbered from 0, so the 12 elements of sales are numbered from 0 to 11 (including 11). Regarding the business sales program example, January's total sales would be stored in salesArray[0], February's in salesArray[1], and so on.
When an array is declared, some memory is allocated by the compiler to hold the entire array.
The declaration properties in an array are the same as any non-array data type. ie., An array can be declared anywhere a non-array data type is declared. Each item in an array can be accessed by using the array name followed by the item index inside square brackets. In the above-mentioned example, consider the "sales" array stores the value 100 in the first array element ie., sales[0]):
sales[0] = 100;
Similarly, the element at a particular index can be assigned to another variable or to an array element like,
sales[8] = sales[10];
The above statement sets the value at index 8 to value at index 10.
In Arrays concept, the allowable indexes in an array range from 0 to (n-1) in an array of size n. Hence while trying to access a value at index n, the index will be out of range and hence produces erroneous results.
General Syntax of Array
data_type array_name[size]
for example,
int a[5];
int sales[12];
Simple Array Example Program in C++
/* Understanding Array Example Program In C++ Programming Language*/
#include <iostream>
#include<conio.h>
using namespace std;
int main() {
// declaring and Initializing sales array in C++
int sales[12] = {100, 90, 80, 110, 115, 210, 80, 95, 125, 135, 140, 174};
int i; // Variable for access sales array index
cout << "Understanding Array :Sales Demo : In C++ Example Program\n";
for (i = 0; i < 12; i++) {
// Accessing sales value using for loop
cout << "Position : " << i << " , Sales Value : " << sales[i] << " \n";
}
getch();
return 0;
}
Sample Output
Single Dimensional Array :Sales Demo : In C++ Example Program
Position : 0 , Sales Value : 100
Position : 1 , Sales Value : 90
Position : 2 , Sales Value : 80
Position : 3 , Sales Value : 110
Position : 4 , Sales Value : 115
Position : 5 , Sales Value : 210
Position : 6 , Sales Value : 80
Position : 7 , Sales Value : 95
Position : 8 , Sales Value : 125
Position : 9 , Sales Value : 135
Position : 10 , Sales Value : 140
Position : 11 , Sales Value : 174
Program Flow
When you run the above example program, the program prints the values in the predefined array one by one from index 0 to 11 ie., totally 12 values.
The program starts with a comment that describes what the program is all about.
int sales[12] = {100, 90, 80, 110, 115, 210, 80, 95, 125, 135, 140, 174};
This line creates an array named "sales" of size 12. The comma-separated values on the right side of the "=" symbol refer the array values. The element at index 0 is 100, the element at index 1 is 90 and so on...
int i;
This line declares an int value for iteration usage using for loop.
for (i = 0; i < 12; i++){
cout << "Position : " << i << " , Sales Value : " << sales[i] << " \n";
}
This starts the iteration process. For every value of i, the value of i and the value of the element at position i is printed using "cout" statement. The resultant Output will be the same as in the Sample Output.
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Example Program For Copy Constructor In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Simple Example Program For Constructor Overloading In C++
- Factorial Using Function Example Program In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor In C++
- Simple Program for Read user Input Using cin
- Simple Stack Program in C++ Programming
- Factorial Using Loop Example Program In C++
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Do While Loop Example Program In C++
- Simple Program for Virtual Base Class Using C++ Programming