Arrays in C++ Programming language

In this tutorial, you will learn all about Arrays in C++ Programming language. An array is a set of similar items in adjacent memory locations. In C++ programming, sometimes a single variable is not sufficient to carry all the data. For example, let’s say we want to store the marks of 800 students, having 800 distinct variables for this task is not feasible, we can define an array with size 800 that can keep the marks of all students.

Arrays in C++

In another way, Arrays in C++ are a combination of elements of the same data type like int, char, float, double, etc, that are saved using the index value and can easily be obtained by index value only. It performs to save all the instances of variables into one single variable. In C++, an array can be declared using three different methods.

  • initializing array elements directly.
  • specifying the size of an array.
  • specifying arrays size with its elements.

To let the data be processed using any application, First, we need to take 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 variables.

Variables are used to store the values so that they could be used by the application to generate the expected outcome. As variables are stored, some values, occupy 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 belong to the same datatype. In this article, we are going to learn about the array using the C++ programming language.

How to Create Arrays?

The method of creating the array is definitely similar to variable creation. The first step is to declare the array. Once the array is declared, we can use both to initialize the array at the same time or it could be initialized later. While declaring the array we have to consider three things: datatype of the array, the name of the array, and its size. Below is the syntax that shows how to simply declare the array.
Method 1:

int arr[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;

Method 2:

int arr[] = {10, 20, 30, 40, 50};

In the above method 1. We have seen that the array that has specified the size 5 has accepted the 5 values. If one will try to submit more than 5 values, it will deliver an error. However, method 2 if you do not specify the size of the variable you can store as much value as you want.
Example:

Array index starts with 0, which determines the initial array element is at index 0, second is at index 1, and so on. We can use this information to display the array elements. See the code below:

#include <iostream>
using namespace std;
int main()
{
   int arr[] = {10, 20, 30, 40, 50};
   cout<<arr[0]<<endl;
   cout<<arr[1]<<endl;
   cout<<arr[2]<<endl;
   cout<<arr[3]<<endl;
   cout<<arr[4]<<endl;
   return 0;
}
Output
10 20 30 40 50

Conclusion

The array in C++ is supposed very basic feature as it helps in memory management and also improves the efficiency of the program. It can be used in various algorithms to hang to its ability to offer multidimensional data storage. It is always optimal to use an array when there is a necessity to store values of the same datatype. Arrays do not just help in maintaining resources but also reduces the program execution timing.

Leave a Reply

Your email address will not be published. Required fields are marked *