1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
using namespace std;
int *merge(int *a1, int n1, int *a2, int n2)
{
int NewSize = n1 + n2;
int i=0,i1 = 0, i2 = 0;
int arr[NewSize]; //1'st problem
if (NewSize == 0)
return NULL;
for (i = 0; i < NewSize;i++)
{
if (a1[i1] <= a2[i2])
{
arr[i] = a1[i1];
i1++;
}
else
{
arr[i] = a2[i2];
i2++;
}
cout << arr[i];
}
return arr;
}
int main()
{
int arr1[] = { 1, 3, 5, 7, 9 };
int arr2[] = { 2, 4, 6, 8, 10 };
int* arr=merge(arr1, 5, arr2, 5);
for (int i = 0; i < 10; i++)
{
cout << arr[i] <<endl;
}
}
|