how to combine to arrays of size m and of size n to m+n size

0 votes
asked Sep 7, 2018 by vamshi232 (120 points)

3 Answers

0 votes
answered Sep 7, 2018 by Victor
Here is a quick way you can do it:

Let m_arr be an array of size m

Let n_arr be an array of size n

int *b_array = malloc((m + n) * sizeof(int));

memcpy(b_arr, m_array, m * sizeof(int));

memcpy(b_arr + m * sizeof(int), n_array, n * sizeof(int));
0 votes
answered Sep 9, 2018 by Fady Serhan

//simple c++ function

    // assume the arr3 is the correct size (m+n)

void merge2 (int* arr1, int* arr2, int *arr3, int m, int n)

{

for(int i=0; i<m ; i++)  //filling first array

{

arr3[i]= arr1[i];

}

for(int i=0; i<n ; i++)  //filling second array

{

arr3[i+m]= arr2[i];

}

}

0 votes
answered Sep 10, 2018 by anonymous
#include<iostream>
using namespace std;
int main()
{int m,n,i;
cout<<"Eneter size of first array and second array "<<endl;
cin>>m>>>n;
int a[],b[];
for( i=0;i<m;i++)
{
 cin>>a[i];
}
 cout<<a[i]<<endl;

for(i=0;i<n;i++)
{
 cin>>b[i];
}
 cout<<b[i]<<endl;
int m+n;
c[];
for(i=0;i<m+n;i++)
{
cout<<c[i];
}
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...