C++ Arrays
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:
dataType arrayName[arraySize];
Example:
string name[5];
We have now declared a variable that holds an array of five strings. To insert
values to it, we can use an array literal - place the values in a comma-
separated list, inside curly braces:
string name[5] = {"Abida", "Soliya", "Makeena", "Raha","Uzair"};
To create an array of five integers, you could write:
int x[5] = {10, 20, 30,40,50};
Access Elements in C++ Array
In C++, each element in an array is associated with a number. The number
is known as an array index. We can access elements of an array by using
those indices.
// syntax to access array elements
array[index];
Example
int x[4] = {10,20,30,40};
cout << x[0];
// Outputs 10
Change an Array Element
To change the value of a specific element, refer to the index number:
X[0]=50;
Loop Through an Array
You can loop through the array elements with the for loop.
Example
// Create an array of integers
int marks[5] = {500,300,350,451,351};
// Loop through integers
for (int i = 0; i < 5; i++) {
cout << marks[i] << "\n";
}
This example outputs the index of each element together with its value:
Example
int marks[5] = {500,300,350,451,351};
for (int i = 0; i < 5; i++) {
cout << i << " = " << marks[i] << "\n";
}
The foreach Loop
There is also a "for-each loop" (introduced in C++ version 11 (2011)),
which is used exclusively to loop through elements in an array.
Syntax
for (type variableName : arrayName) {
// code block to be executed
}
Example
Loop through integers:
// Create an array of integers
int myNumbers[5] = {10, 20, 30, 40, 50};
// Loop through integers
for (int i : myNumbers) {
cout << i << "\n";
}
Omit Array Size
In C++, you don't have to specify the size of the array. The compiler is
smart enough to determine the size of the array based on the number of
inserted values:
int marks[] = {200,500,350}; // Three array elements
The example above is equal to:
int marks[3] = {200,500,350}; // Three array elements
Omit Elements on Declaration
It is also possible to declare an array without specifying the elements on
declaration, and add them later:
Example
int marks[5];
marks[0] = 200;
marks[1] = 300;
marks[2] = 500;
marks[3] = 700;
marks[4] = 600;
Note: The example above only works when you have specified the size of
the array.
If you don't specify the array size, an error occurs:
Example
int marks[];// Array size is not specified
marks[0] = 200;
marks[1] = 300;
marks[2] = 500;
marks[3] = 700;
marks[4] = 600;
Get the Size of an Array
To get the size of an array, you can use the sizeof() operator:
Example
int myNumbers[5] = {10, 20, 30, 40, 50};
cout << sizeof(myNumbers);
The sizeof() operator returns the size of a type in bytes.
To find out how many elements an array has, you have to divide the
size of the array by the size of the first element in the array:
Example
int myNumbers[5] = {10, 20, 30, 40, 50};
int Length = sizeof(myNumbers) / sizeof(myNumbers[0]);
cout << Length;
Loop Through an Array with sizeof()
Example
int myNumbers[] = {10, 20, 30, 40, 50};
for (int i = 0; i < sizeof(myNumbers) / sizeof(myNumbers[0]); i++) {
cout << myNumbers[i] << "\n";
}
Example : Take Inputs from User and Store Them in an Array.
Example : Display Sum and Average of Array Elements Using for Loop.
#include<iostream>
using namespace std;
int main(){
//int marks[] = {200,500,350};
int marks[3]={10,20,30};
int sum=0;
float avg;
int length=sizeof(marks)/sizeof(marks[0]);
cout<<endl<<length;
for(int i=0;i<3;i++){
sum=sum+marks[i];
}
cout<<endl<<"sum is "<<sum;
avg=sum/length;
cout<<endl<<"average is "<<avg;
return 0;
}