Array
Arrays are used to store collection of values in a single variable, instead of declaring separate variables for each value. It stores a fixed-size sequential collection of elements of the same type. Each value present inside an array is known as element. To declare an array, define the variable with square brackets.
In an array depending on the total number of element present inside system will create that many consecutive memory spaces and each will be named with help of index numbers(0,1,2,3,4,5,6,....). We can access an array element by referring to the index numbers. string[] can only store collection of string values, int[] can only store collection of integer values, char[] can only store collection of character values.
CODE
EG 1 . Creating an array called musical instruments and printing out only one element of an array in console
#include <iostream>
using namespace std;
int main() {
string musicalInstruments[] = {"piano","guitar","violin","drums" };
cout << musicalInstruments[2] << endl; //Output : violin
}
EG 2 . Creating an array called numbers and printing out all the elements in console using for loop
#include <iostream>
using namespace std;
int main() {
int numbers[] = {56,987,65,32,78,6565,947,654};
for (int i=0; i<8; i++) {
cout << numbers[i] << endl;
}
}
(Click Here, For more exercises)