C++ Chapter7
C++ Chapter7
Chapter
7
Array in real life
Arrays are used to store multiple values in a single variable, instead of declaring
separate variables for each value.
To declare an array, define the variable type, specify the name of the array
followed by square brackets and specify the number of elements it should store.
Example
double grade[27];
Here, grade is an array that can hold a maximum of 27 elements of double type.
C++ Array Declaration
dataType arrayName[arraySize];
int x[6];
• int - type of element to be stored
• x - name of the array
• 6 - size of the array
C++ Array Initialization
In C++, it's possible to initialize an array during declaration.
int x[3];
int x[3] = {19, 10, 8}; x[0]=19;
x[1]=10;
x[2]=8;
• int - type of element to be stored
• x - name of the array
• 3 - size of the array
C++ Array Initialization
The array indices start with 0. Meaning x[0] is the first element stored at index 0.
create an array of four integers