Home >Backend Development >C++ >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 .
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 obvious that we have two arrays A and B. Pushing B into A means inserting all elements from B into array A. These elements will be added to the end of A. But to achieve this, we have to check one thing, whether the remaining vacancies in A (i.e. the maximum size of A minus the number of existing elements in A) are the same or greater than the number of elements in B. Otherwise, we can't push them into A. Let's take a look at the algorithm and the C implementation code.
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 accommodate the entire B, then
for each element e in B, do
append e to array A
End loop
end if
return array A and new size n
#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 ); }
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,
The same thing can be done using vectors. Vectors are dynamic arrays present in C STL. If we consider using vectors, we don't need to care about the available space when inserting elements. Because the vector is dynamic, it automatically adds new slots when needed. The algorithm is the same as the available slot check.
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 loop
return array A and new size n
#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 ); }
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,
The previous method was a manual process. However, we can use the insert() function in vector STL to achieve the same functionality. insert()The function accepts a position pointer (using an iterator) and an iterator, copies an element from one container object and inserts it from the position index into another container object. Let's look at the C implementation to get a clear view.
#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 ); }
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,
In this article we saw a few different ways to insert or push an array element to the end of another array. In the first example, we use a simple C array, special attention needs to be paid to the space available in the static array. In the next two methods, we don't need to worry about this because we are using dynamic vectors that will automatically allocate space when needed.
The above is the detailed content of C++ program to push an array into another array. For more information, please follow other related articles on the PHP Chinese website!