Arrays:

Arrays are data type in C++ which are used to store multiple values of same data type.


Why we need Arrays?

In C++, if you want to store marks of ten students, you will have to declare ten variables. But if you use arrays, you do not have to declare ten variables. As data type of all these variables is same, you can use only one array variable to store them.
datatype name[size] = {1, 2, 3}

Here size stores, how many elements are there in array and if you want to access data on some individual at any index, you can access,

array.cpp
#include <iostream>
using namespace std;

int main(){
    int marks [5] = { 76, 55, 92, 65 };
    return 0;
}

Accessing Elements of Arrays:

We can access individual elements of arrays using indexes. name[0] will access first element of array.
Similarly name[2] will access data at index 2 and it will be on third number because index starts from zero.


array.cpp
#include <iostream>
using namespace std;

int main(){
    int marks [4] = { 76, 55, 92, 65 };
    cout<<marks[0];      
    cout<<marks[1];       
    cout<<marks[2];      
    cout<<marks[3];      
    return 0;
}

Problem:

It is easy to print when size of array is 10, 20, 30 or 100. But imagine if the size of array is 1 million, is it good to print individual indexes like this? What we are doing here? We are doing the same work over and over again. And we have seen that when we need to repeat our same block of code, we use loops.

Solution:

array.cpp
#include <iostream>
using namespace std;

int main(){
    int marks [5] = { 76, 55, 92, 65 };
    for(int i=0; i<5; i++){
        cout<<marks[i]<<endl;
    }
    return 0;
}

Taking input in arrays:

array.cpp
#include <iostream>
using namespace std;

int main(){
    int marks [10] ;
    for(int i = 0; i < 10;  i++){ 
        cin>>marks[i];
    }
}

Changing elements of array:

You can change the elements of array at any index you want.

array.cpp
#include <iostream>
using namespace std;

int main(){
    string names [3] = { “Andrew”, “Ema”, "Jo" }; 
    for(int i=0; i<3; i++){
        cout<<marks[i];
    }
    cout<<endl; //print new line 
    names[0] = “Leah”;
    for(int i=0; i<3; i++){
        cout<<marks[i];
    }
    cout<<endl;
    return 0;
}

Note: If you do not specify the size of array, it is ok.


array.cpp
#include <iostream>
using namespace std;

int main(){
    int ages[] = { 20, 22, 24 } 
    return 0;
}

Note: Point: If we do not know the size of array, to which number we will run loop?


Find size of array:

sizeof() operator as discussed earlier will give us the size of array. The size of array does not give us the number of elements in the array. As size of int is 4bytes and we have three integers, so size of array will be 12 bytes. For this purpose, we have to divide size of array with size of datatype.
int totalItems = sizeof(array) / sizeof(datatype)

array.cpp
#include <iostream>
using namespace std;

int main(){
    int marks [] = { 10, 20, 30 };
    int size = sizeof(marks)/ sizeof(int);
    for(int i = 0; i < size;  i++){ 
        cout<<marks[i];
    }
}