Understanding Arrays in C++

Many times while programming you might have come across a problem where you need to make multiple variables of the same datatype in order to store similar data in it. But every time this is not a feasible option. So the concept of Array comes into the picture.

An array is basically a collection of values of the same datatype or you can also understand it as a collection of the same type of variables.

For example, you want to store the marks of one subject of a class having 40 students, then you will have to create 40 different variables like marks1, marks2, marks3… marks40. But this becomes very difficult for management so you can declare an array of size 40 which will allocate a continuous memory of 40 units.

                         


How to declare an array?

Syntax for declaring an array in C++ is:

datatype arrayName[size];

For example:
int marks[5];

This will create an array named marks of size 5 of datatype int.

How to access an array?

Arrays can be accessed using the index. You might wonder what is an index? An index is like a value used to access elements of the array.

Index of an array starts from 0 (zero) to (n-1) where n is the number of elements in the array or size of the array. If you consider the above example array index will be from 0 to (5-1) which is 0 to 4.

If you want to get third value from the array, you can access it by marks[2]. This shows that to access the nth element of the array you will have to use the index as (n-1).

Key point you should remember about the array is that it is a set of continuous memory locations which means if the memory address of marks[0] is 2010 then the memory address of marks[1] will be 2014, marks[2] will be 2018 and so on. It is because the array is of type int and in C++ an integer value occupies 4 bytes so, there is a gap of 4 bytes between each memory location.

How to initialize an array?

After you declare an array next step is to initialize it. There are multiple ways to initialize an array.

You can simply assign the value to the array using the assignment operator(=) and then in curly braces, specify all the elements of the array. For example, int marks[5] = { 90, 91, 86, 45, 90 };
When you initialize an array at the time of declaration and do not specify the size of the array, it will take of size as the number of elements in the initialization.

For example, int marks[] = { 90, 91, 84 };

It will automatically take the size as 3 as there are three elements assigned to the array.

The next method is the traversal method. If you want to take the input of the array through the user, you may use this method. In this method, we with the help of a for loop initialize each and every element with the user inputted value.

For example :


int i = 0;
int marks[5];  // declaration of the array 
for( i=0 ; i<5 ; i++) { cin >> marks[i]; assign the input value to the array }


Here is a program which shows how to take input from user and print an array.

#include <iostream>
using namespace std;

int main()
{
int num[5];   //declaration of the array.
cout << “Enter 5 elements of the array :”;
//Input each and every element of array using traversal method
for(int i = 0; i<5; i++)
{
cin >> num[i];          //assigning the input value to the array
}
cout << “Your array is :”;
for(int i = 0; i<5; i++)
{
cout << “num[i] = ” <<num[i];    //output value of the array element by element
}
return 0;
}
Output of the above code :
Enter 5 elements of the array :1
2
3
4
5

Your array is : num[0] = 1
num[1] = 2
num[2] = 3
num[3] = 4
num[4] = 5

The array discussed above was the single dimension array or 1D array. Simillary, we can have Multi-Dimensional array as well.

Multidimensional Array

Multidimensional array contains multiple dimensions like a 2D array will behave as a table have rows and column but in for of an array. Accessing of the array is still based on the index only.

To declare and initialize an 2D array :

int num[3][3] = { {1, 2, 3}, // Row 1 {4, 5, 6}, // Row 2 {7, 8, 9} }; //Row 3

Here is a program which shows how to input and output a 2D array.

#include <iostream>
using namespace std;
int main()
{
int num[3][3];         //declaration of the array.
cout << "Enter elements of the array :";
//Input each and every element of array using traversal method
for(int i = 0; i<3; i++) 
{
cout << "Enter 3 elements of Row " << i+1 << "\n";
for(int j=0; j<3 ;j++)
cin >> num[i][j]; //assigning the inputted value to the array
}
cout << "Your array is : \n";
for(int i = 0; i<3; i++) 
{
for(int j=0; j<3 ;j++)
cout << "num["<<i <<"][" << j << "] = " <<num[i][j] << "\t"; //output value of the array element by element
cout << "\n";
}
return 0;
}
Output
Enter elements of the array :Enter 3 elements of Row 1
1
2
3

Enter 3 elements of Row 2
4
5
6

Enter 3 elements of Row 3
7
8
9

Your array is :
num[0][0] = 1 num[0][1] = 2 num[0][2] = 3
num[1][0] = 4 num[1][1] = 5 num[1][2] = 6
num[2][0] = 7 num[2][1] = 8 num[2][2] = 9

This was all about the arrays in C++. Hope you enjoyed learning. If you have any queries or suggestions please leave a comment. We will come up with more useful content for you.