Introduction to Arrays in C++
Arrays in C++ are a collection of similar data types like int, char, float, double, etc., that are stored using the index value and can easily be accessed by index value only. Moreover, it implements to store all the instances of variables into one single variable. In C++, an array can be declared using three methods: by specifying the size of an array, by initializing array elements directly, and by specifying arrays size with its elements.
To let the data processed using any application, we first need to bring the data into the application. This means there should be some space in the application where the value should be stored until the program runs. In order to serve the purpose of storing the values, the programming language offers us a variable. Variables are used to store the values so that they could be used by the application to generate the expected outcome. As variables store, some value occupies space in the memory allocated to the application. So the optimal approach of coding is to ensure the usage of the variable should be as low as possible. In order to mitigate the memory allocation issue due to the creation of a high number of variables, the concept of array came into existence. The array can be considered as the list of values that belongs to the same datatype. In this article, we are going to learn about the array using the C++ programming language.
How Do Arrays work in C++?
Below is the explanation of how arrays work:
- The role of the array is to store the values of the same datatype. It is supposed to work the same way as that of the variable, and the only additional point it has over the variable is, it is capable of holding several values at the same time. When we create the array in C++ or any of the programming language, we have to state the number of variables that we want to store in the array.
- It has to be noted that the size of the array remains fixed throughout the application runtime and cannot be changed dynamically. Once the size of the array is defined, we can store the same count of values in it. If the data type of the array is defined as an integer, it will not accept any value that is not an integer. In order to locate the value held by the array, one will need to use the index.
- For instance, if the array is capable of holding two values, then the second value will be stored at the one index of the array as the index of the array begins with zero. In the next section, we will learn array creation.
How to Create Arrays in C++?
Below explanation says that how to create arrays in c++:
The approach of creating the array is exactly similar to variable creation. The first step is to declare the array. Once the array is declared, we can either initialize the array at the same time, or it could be initialized later. While declaring the array, we have to mention three things: the datatype of the array, the name of the array and its size. Below is the syntax that shows how to declare the array merely.
Syntax:
Datatype array_name[size];
Ex. int first_array[10];
The array defined here can ten integer values. The name of the array is first_array, and the number defined inside the large bracket states the size of the array. Now let’s see how to declare and initialize the variable at the same time.
Code:
Int first_array[4] = { 1,2,3,4}
Int first_array[]= {1,2,3,4,5,6}
In the above example, we have seen that the array that has defined the size 4 has accepted the 4 values. If one will try to submit more than 4 values, it will throw an error. Also, if you do not specify the size of the variable, you can store as much value as you want.

4.5 (8,205 ratings)
View Course
Types of Arrays in C++
In the C++ programming language, we do have mainly two types of variables: Single Dimensional Arrays and multidimensional Arrays. The single-dimensional stores values hold the values in the form of the list, while the multidimensional array store the value in the matrix. Below we will see each of the types using an example.
1. Single Dimensional Array
The single-dimensional array may be defined as the type of array capable of holding the values of the same data centre in the form of a list. It is the simplest form of an array as it doesn’t require much effort to define and initialize such an array. It can be defined as int a[10], where int is the data type is the array name and the size of the array is 10. The example below will make things more clear.
Code:
#include <iostream>
#include <conio.h>
using namespace std;
void main()
{
int val_array[3];
int int_val=1,counter;
cout<<"Please enter three numbers that you want to multiply"<<endl;
for(counter=0;counter<3;counter++)
{
cin>>val_array[counter];
int_val = int_val*val_array[counter];
}
cout<<"The multiplication of these three numbers is = "<<int_val;
getch();
}
The above program is written to accept three values from the user, and then those values will be processed to generate the multiplication value of all of them. The array user here is name val_array, and the array is capable to hold three values. The loop is used to intake the value in the array, and then they were multiplied. The end result of the multiplication has been stored in the int_val variable. As the function is void in nature, it will not return any value.
Output:
2. Multidimensional array
The multidimensional array may be defined as the array that holds the values in the way a matrix does. The two-dimensional array is used very often, and with the increase in the size of dimension, the array gets complicated. For instance, it is easy to work with a two-dimensional array rather than working with a three-dimensional array. The two-dimensional array needs two sizes to be defined for one dimension each. The two-dimensional array can be in the program as int a[5][3]. This array will hold the value in the form of a matrix with 5 rows and three columns. Let us understand this with the help of an example.
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int val_array[5][5];
int count_rows,count_cols,counter1,counter2;
cout<<"Please enter the size of the rows and columns that you wnant to input: ";
cin>>count_rows>>count_cols;
cout<<"PLease enter the values for matrics in row wise manner"<<endl;
for(counter1=0;counter1<count_rows;counter1++)
for(counter2=0;counter2<count_cols;counter2++)
cin>>val_array[counter1][counter2];
cout<<"The matrix will be as follows"<<endl;
for(counter1=0;counter1<count_rows;counter1++)
{
for(counter2=0;counter2<count_cols;counter2++)
cout<<val_array[counter1][counter2]<<" ";
cout<<endl;
}
getch();
return 0;
}
In this program, we have used a two-dimensional array. The way array was defined using two sizes, it states that the array used is two dimensional. If there would have been three sizes, then the array would be three dimensional. The user is asked to input the number of rows and columns they want in the matrix. Once the user specifies the numbers, they are asked to input the values that they want in the rows and columns of the matrix. Here the user has submitted 2 3, which means they want two rows and three columns in the matrix. They had to submit six values as the matrix with two rows and three columns with six values. When all the values have been submitted, they have been represented in the form of a matrix. The entire program is implemented using the two-dimensional array.
Output:
Method of Passing Array
Methods are shown below:
To pass the variable in any method as a parameter, all it needs to accept the value from where it is called is datatype and the name of the variable that will be holding the Value. The approach of passing the array to the method is similar to the way any normal variable is passed. The only difference is, rather than mentioning a single variable; we will need to use the array with a specific size in the place of the array. Let’s understand this using syntax.
Syntax:
//defining method that accepts an array as a parameter.
int handle_array(int a[6]);
Here the method name is handle_array, and it has an array as a parameter. The name of an array is a, and the array is capable of holding six values. Now let’s see how the argument can be passed to the method handle_array.
Syntax:
int arrr[6] = {1,2,3,4,5,6};
handle_array(arr) ;
First, we need to assign the values to other variables that we have to pass to the handle_array method. Once the values are assigned, we need to pass the array as an argument while calling the handle_array function. In the above snippet, it is clear how the array has been assigned with the values and passed as an argument to pass the values that the handle_array function has been defined.
Conclusion
The array in C++ is considered an essential feature as it helps in memory management and improves the program’s efficiency. It can be used in several algorithms to die to its ability to offer multidimensional data storage. It is always optimal to use an array when there is a need to store values of the same datatype. It does not just help in conserving resources but also reduces the program execution timing.
Recommended Articles
This has been a guide to Arrays in C++. Here we discuss how Arrays work in C++, how to create, types of Arrays, passing, codes, syntax, and outputs, etc. You can also go through our given articles to learn more-