Arrays are the data structures in C++ language that are fixed-size data structures and cannot change the size of an array after being declared. In arrays, you can store multiple values of the same datatype.
Adding elements to an array is one of the widely used tasks performed in C++ coding. To add elements to an array, you must have to create a new array with a max size, copy the existing elements into the new array, and afterwards add the new element to that array.
This article presents a detailed guide to insert an element to C++ array with examples.
How to Add an Element to C++ Array
There are different methods to add elements in C++ array, let’s discuss them.
Method 1: Enter Elements to Array One-by-One
First, you have to assign the size of an array, which could be any size. Then you have to enter the elements one by one that need to be input into the array of size. Remember that you cannot change the size of the array after you fixed it at the start. Once you input the elements, it will be added to an array and will be printed out using the cout function.
Follow the below-given example of adding an element at the end of array in C++ code:
using namespace std;
int main()
{
int array[6], i, x;
cout<<"Enter any 5 Array Elements: ";
for(i=0; i<5; i++) cin>>array[i];
cin>>array[i];
cout<<"\nEnter New Element to Insert in an Array: "; cin>>x;
cin>>x;
array[i] = x;
cout<<"\nThe Updated Array is:\n";
for(i=0; i<6; i++)
cout<<array[i]<<" ";
cout<<endl;
return 0;
}
In the above code, we initialize a maximum size of an array, which is 6 and enter the first 5 elements one by one. Then we add a new element to an array and print the updated array having 6 elements.
Following is the output of the program.
Method 2 : Enter Elements to a specific Position/Index in an Array
In the above method, you can only add an element after the end of previous array elements. However, if you want to manually add an element to a specific index or position in an array, you can use this method. Here in this method, we will use the max size of an array like the one we already did in the above method and then print that array. After that, we have to select the position where we want to add the element to that array.
Let’s follow up with an example shown below:
using namespace std;
int main()
{
int array[20], i, element, position, size;
cout<<"Please enter the size of an array: ";
cin>>size;
cout<<"Enter "<<size<<" array elements : ";
for(i=0; i<size; i++)
cin>>array[i];
cout<<"Select the position: ";
cin>>position;
for(i=size; i>=position; i--)
array[i] = array[i-1];
cout<<"\nPlease Enter the value in array: ";
cin>>element;
array[i] = element;
size++;
cout<<"\nNoe the new array we have is:\n";
for(i=0; i<size; i++)
cout<<array[i]<<" ";
cout<<endl;
return 0;
}
In the above code, we enter the array size and add the elements into an array. Then we select the position where we want to add the element to an array. As soon as the element is entered, it will be added to the array list at that position.
The output of the following array is showed below:
Conclusion
Arrays are the consecutive space in our system memory where we can put or take homogeneous kinds of data from the user. It is the finest form of data structure in C++ language. In the above guidelines, we have seen different approaches to add a new element in an array. First one is straightforward method which helps to add element at the last of array and in second approach the new element of array could be shift anywhere in the array where you select position and it will insert element before that position in an array.