C++ Program to append an element into an array


An array is a linear sequential data structure to hold homogeneous data in consecutive memory locations. Like other data structures, an array also must-have features to insert, delete, traverse and update elements in some efficient way. In C++, our arrays are static. There are a few dynamic array structures also available in C++. For a static array, there may be a Z number of elements that can be stored inside that array. And till now we have n elements into it. In this article, we will see how to insert an element at the end of an array (which is also termed append an element) in C++.

Understanding the concept with examples

The this keyword is used in the following way

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
After inserting 23 at the end, the array will look like this:
[10, 14, 65, 85, 96, 12, 35, 74, 69, 23]

In the above example, consider we have an array A that can hold at most 50 elements inside it. So, the value of Z is 50. Now consider at first, we have 9 elements in it. So, the size of the array, n is 9. To insert another element at the end of the array, say 23 in our example. The element will be placed at the end and the number of elements in A will be increased by 1. Hence n becomes 10. Since we are inserting at the end, the process is straightforward. We can simply add the new element after all elements without changing the places of any existing element in the array. Now let us see the algorithm for a clear understanding along with the C++ implementation code.

Algorithm

  • take the array A as input, the number of elements n as input, and an element e which will be inserted into A

  • If n is < Z, which is the maximum possible places to be inserted into A

    • A[ n ] = e

  • end if

  • increase n as n := n + 1

  • return array A and new size n

Example

#include <iostream>
# define Z 50

using namespace std;

void displayArr(int arr[], int n){
   for( int i = 0; i < n; i++ ){
      cout << arr[ i ] << ", ";
   }
   cout << endl;
}
void insertAtEnd( int arr[], int &n, int e ){
   if( n < Z ) {
      arr[ n ] = e;
   }
   n = n + 1;
}

int main() {
   int arr[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   cout << "Array before insertion: ";
   displayArr( arr, n );
   
   cout << "Inserting 58 at the end:" << endl;
   insertAtEnd( arr, n, 58 );
   
   cout << "Array after insertion: ";
   displayArr( arr, n );
   
   cout << "Inserting 225 at the end:" << endl;
   insertAtEnd( arr, n, 225 );
   
   cout << "Array after insertion: ";
   displayArr( arr, n );
}

Output

Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 58 at the end:
Array after insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 58, 
Inserting 225 at the end:
Array after insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 58, 225,

Using Vectors Append Element

The vectors are a dynamic data structure that comes with C++ STL. We can get a similar functionality like arrays in vectors as well. Inside vectors, we get functionality to insert at end using push_back() function. The push_back function takes the new element as argument and inserts the element at the end of the given vector. The algorithm is straightforward. We do not need to do anything special, just call the function for the given vector object by passing the new element that we are going to insert. Let us see the C++ implementation directly.

Example

#include <iostream>
#include <vector>
# define Z 50

using namespace std;

void displayArr( vector<int> v ){
   for( int i = 0; i < v.size() ; i++ ){
      cout << v[ i ] << ", ";
   }
   cout << endl;
}

vector<int> insertAtEnd( vector<int> A, int e ){
   A.push_back( e );
   return A;
}

int main() {
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};  
   cout << "Array before insertion: ";
   displayArr( A );
   
   cout << "Inserting 58 at the end:" << endl;
   A = insertAtEnd( A, 58 );
   
   cout << "Array after insertion: ";
   displayArr( A );
   
   cout << "Inserting 225 at the end:" << endl;
   A = insertAtEnd( A, 225 );
   
   cout << "Array after insertion: ";
   displayArr( A );
}

Output

Array before insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Inserting 58 at the end:
Array after insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 58, 
Inserting 225 at the end:
Array after insertion: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 58, 225,

Conclusion

Arrays are one of the simplest data structures to hold homogeneous data consecutively. Arrays are data structures. Like other data structures, we can also insert, delete, update and traverse through the array elements easily. In this article we have seen two approaches to insert elements at the end, in other words, append elements into array. In the very first method we are using static arrays in C++. Since we are targeting the end position, we do not need to shift any element in the array, simply add a new element at the last index and also increase the total item count parameter for further uses. In the second case, we are using vectors. Vectors are like normal arrays in C++, but they are dynamic in nature. It automatically updates its total size when required. The C++ STL supports vectors which has a special function called push_back() to insert element at the back. However, we cannot add an element at the beginning with such straightforward direct method.

Updated on: 04-Oct-2023

31K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements