C++ Tutorial C++ Advanced C++ Resources
Python Java C++ C C# PHP R SQL DS Algo InterviewQ

C++ - Arrays



Arrays are used to store multiple values in a single variable. In C++, all elements of an array must be of same datatype.

Create an Array

It starts with defining the datatype of the array, name of the array followed by size of the array enclosed in square brackets [ ].

Initialize an Array

An array can be initialized by separating it's elements by comma , and enclosing with curly brackets { }.

Please note the following points while working with Arrays:

  • Array can be initialized later using index, if it not initialized at the time of declaration.
  • if a variable is used for the size of array, then the variable must not be unassigned before declaring the array.

Syntax

//declaring an array of size 3.
int MyArray [3];  

//array of size 'n'. 
//'n' must have assigned value before declaration.
float MyArray [n]; 

//declaring and initializing an array of size 3.  
int MyArray [3] = {10,20,30}; 

Access element of an Array

An element of an Array can be accessed with it's index number. Index number for an Array in C++ starts with 0.

C++ Arrays Indexing

Below mentioned example illustrates on indexing for an Array in C++.

#include <iostream>
#include <string>
using namespace std;
 
int main (){
  string weekday[5] = {"MON", "TUE", "WED", "THU", "FRI"};
  cout<<weekday[4]; 
  return 0;
}

The output of the above code will be:

FRI

Change elements of an Array

Any element of an Array can be changed using its index number and assigning a new value.

#include <iostream>
#include <string>
using namespace std;
 
int main (){
  string weekday[5] = {"MON", "TUE", "WED", "THU", "FRI"};
  weekday[0] = "MON_new";
  weekday[3] = "THU_new";
  cout<<weekday[0]<<"\n"; 
  cout<<weekday[3]; 
  return 0;
}

The output of the above code will be:

MON_new
THU_new

Loop over an Array

With for loop or while loop, we can access each elements of an Array.

For Loop over an Array

In the example below, for loop is used to access all elements of the given array.

#include <iostream>
#include <string>
using namespace std;
 
int main (){
  string weekday[5] = {"MON", "TUE", "WED", "THU", "FRI"};
  for(int i = 0; i < 5; i++) {
    cout<<weekday[i]<<"\n";     	
  }
  return 0;
}

The output of the above code will be:

MON
TUE
WED
THU
FRI

While Loop over an Array

Similarly, while loop is also be used to access elements of the array.

#include <iostream>
using namespace std;
 
int main (){
  int numbers[5] = {10, 20, 30, 40, 50};
  int i = 0;
  while(i < 5) {
    cout<<numbers[i]<<"\n"; 
    i++;    	
  }
  return 0;
}

The output of the above code will be:

10
20
30
40
50

Multi-dimensional Array

Multi-dimensional array can be viewed as arrays of arrays. C++ allows you to define array of any dimension.

Syntax

//2-dimension array
int MyArray [n][m];    

//3-dimension array
int MyArray [p][q][r]; 

//2-dimensional array with size (2,3)
int MyArray [2][3] = {10,20,30}; 

#include <iostream>
using namespace std;
 
int main (){
  int numbers[2][3] = {10, 20, 30, 40, 50, 60};
  for(int i = 0; i < 2; i++) {
    for(int j = 0; j < 3; j++) {
      cout<<numbers[i][j]<<" ";
    }
    cout<<"\n";
  }
  return 0;
}

The output of the above code will be:

10 20 30 
40 50 60 




5