In: Computer Science
#include <iostream>
using namespace std;
void SortArrays (int a[], int b[], int c[], int size);
void printArray(int m[], int length);
const int NUM = 5;
int main()
{
int arrA[NUM] = {-2, 31, 43, 55, 67};
int arrB[NUM] = {-4, 9, 11, 17, 19};
int result[2*NUM];
SortArrays(arrA, arrB, result, NUM);
printArray(result, 2*NUM);
return 0;
}
void SortArrays (int a[], int b[], int c[], int size)
{
}
void printArray(int m[], int length)
{
for(int i = 0; i < length; i++)
cout<< m[i]<<" ";
cout<<endl;
}
Please Use C++
Code:
#include <iostream>
using namespace std;
void SortArrays (int a[], int b[], int c[], int size);
void printArray(int m[], int length);
const int NUM = 5;
int main()
{
int arrA[NUM] = {-2, 31, 43, 55, 67};
int arrB[NUM] = {-4, 9, 11, 17, 19};
int result[2*NUM];
SortArrays(arrA, arrB, result, NUM);
printArray(result, 2*NUM);
return 0;
}
void SortArrays (int a[], int b[], int c[], int size)
{
int i=size-1,j=size-1,k=0;/* i is for a indexing
j is for b indexing
k is for c indexing
*/
while(i>=0&&j>=0)
{
if(a[i]>b[j]) /*a[i]>b[j] means a[i] is highest
so c[k]=a[i] then increese k and decrease i
*/
{
c[k]=a[i];
k++;
i--;
}
else /*a[i]<b[j] means b[j] is highest
so c[k]=b[j] then increese k and decrease j
*/
{
c[k]=b[j];
k++;
j--;
}
}
while(i>=0) /*if elements in a[] is left copy them in c[] */
{
c[k]=a[i];
k++;
i--;
}
while(j>=0) /*if elements in b[] is left copy them in c[] */
{
c[k]=b[j];
k++;
j--;
}
}
void printArray(int m[], int length)
{
for(int i = 0; i < length; i++)
cout<< m[i]<<" ";
cout<<endl;
}
Output:
it still works if the values in the arrays are change but values in the 2 arrays must be in ascending order.
below is the modification.
If you have any doubt or need any help please leave a comment.