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;
#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 = 0, j = 0, k = 0,temp=0;
//merging both array into single array.
// Traverse both array
while (i<size && j <size)
{
// Check if current element of first
if (a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
// Store remaining elements of first array
while (i < size)
c[k++] = a[i++];
// Store remaining elements of second array
while (j < size)
c[k++] = b[j++];
//sorting into descending order
//comparing the elements of merged array for descending
order.
for(i=0;i<size*2;i++)
{
for(j=i+1;j<size*2;j++)
{
if(c[i]<c[j])
{
temp =c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
}
void printArray(int m[], int length)// code for printing the sorted
array
{
for(int i = 0; i < length; i++)
cout<< m[i]<<" ";
cout<<endl;
}
output:
please refer to the screenshot of the code to understand the indentation of the code.