C++ Program to push an array into another array


A linear sequential data structure called an array is used to store homogeneous data in a series of memory regions. An array needs to have certain features to insert, delete, traverse, and update elements effectively, just like other data structures do. Our arrays in C++ are static. In addition, C++ offers a few dynamic array structures. There may be a maximum of Z elements that can be stored inside a static array. And there are currently n elements in it. In this article, we will see how to push the elements of one array inside another array in C++.

Understanding the concept with examples

Given array A = [10, 14, 65, 85, 96, 12, 35, 74, 69]
Another given array B = [56, 42, 15, 18, 20, 32]

Pushing the elements of B into A, signifies that A becomes:
A = [10, 14, 65, 85, 96, 12, 35, 74, 69, 56, 42, 15, 18, 20, 32]

In the above example, it is clear that we have two arrays A and B. Pushing B into A means inserting all elements present in B into the array A. The elements will be added at the end of A. But to implement this, we must check one thing the remaining slots in A (which is the maximum size of A − the number of existing elements in A) are the same or greater than the number of elements in B. Otherwise we cannot push them inside A. Let us see the algorithm along with the C++ implementation code.

Algorithm

  • take the array A and B as input, the number of elements n in A as input, the number of elements m in B as input

  • If A has enough space to hold the entire B, then

  • for each element e in B, do

  • append e to array A

  • end for

  • end if

  • 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;
}

void pushArrayToAnother( int A[], int &n, int B[], int m ) {
   if( (Z - n) >= m ){
      for( int i = 0; i < m; i++ ) {
         insertAtEnd( A, n, B[i] );   
      }
   }
}

int main() {
   int A[ Z ] = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   int n = 12;
   
   int B[ Z ] = {56, 84, 23, 12, 17, 19, 65, 32};
   int m = 8;
   
   cout << "First Array: ";
   displayArr( A, n );
   
   cout << "Second Array: ";
   displayArr( B, m );
   
   pushArrayToAnother( A, n, B, m );
   cout << "Array A after pushing B:" << endl;
   displayArr( A, n );
}

Output

First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Second Array: 56, 84, 23, 12, 17, 19, 65, 32, 
Array A after pushing B:
57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,

Using Dynamic Arrays or Vectors

The same thing can be done using vectors. Vectors are dynamic arrays present inside C++ STL. If we consider vectors, we do not need to care about the available spaces to insert elements. Because vectors are dynamic. It will automatically add new slots when needed. The algorithm is the same without available slot checking.

Algorithm

  • take the array A and B as input, the number of elements n in A as input, the number of elements m in B as input

  • for each element e in B, do

    • append e to array A

  • end for

  • return array A and new size n

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;
}

void pushArrayToAnother( vector<int> &A, vector<int> B ){
   for( int i = 0; i < B.size() ; i++ ) {
      A.push_back( B[i] );
   }
}

int main(){
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};

   vector<int> B = {56, 84, 23, 12, 17, 19, 65, 32};

   cout << "First Array: ";
   displayArr( A );

   cout << "Second Array: ";
   displayArr( B );

   pushArrayToAnother( A, B );
   cout << "Array A after pushing B:" << endl;
   displayArr( A );
}

Output

First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Second Array: 56, 84, 23, 12, 17, 19, 65, 32, 
Array A after pushing B:
57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,

Using the insert() function in vectors

The previous method is a manual process. However, we can do the same thing using the insert() function in vector STL. The insert() function takes a position pointer (using an iterator) and an iterator to copy from one container object and copy the elements into another container object from the position index. Let us see the C++ implementation for a clear view.

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;
} 

void pushArrayToAnother( vector<int> &A, vector<int> B ) { 
   A.insert( A.end(), B.begin(), B.end() ); 
}

int main() {
   vector<int> A = {57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14};
   
   vector<int> B = {56, 84, 23, 12, 17, 19, 65, 32};
   
   cout << "First Array: ";
   displayArr( A );
   
   cout << "Second Array: ";
   displayArr( B );
   
   pushArrayToAnother( A, B );
   cout << "Array A after pushing B:" << endl;
   displayArr( A );
}

Output

First Array: 57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 
Second Array: 56, 84, 23, 12, 17, 19, 65, 32, 
Array A after pushing B:
57, 10, 14, 19, 86, 52, 32, 14, 76, 65, 32, 14, 56, 84, 23, 12, 17, 19, 65, 32,

Conclusion

In this article, we have seen a few different approaches to inserting or pushing one array element at the end of another array. In the first example, simple C++ arrays are used where we need special care about the available spaces inside the static array. In the next two methods, we do not need to care about this because we are using vectors which is dynamic and will automatically allocate spaces whenever it needs.

Updated on: 13-Dec-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements