Question

In: Computer Science

Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...

  1. Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points)

#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++

Solutions

Expert Solution

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.


Related Solutions

Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include 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] =...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #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]...
in C++ Given two integer arrays sorted in the ascending order, code the function SortArrays to...
in C++ Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #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};...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays...
Write and test a merge function that uses a recursive algorithm to merge two sorted arrays of integers. Neither list contains duplicates, and the resulting list should not contain duplicates either. Hint: You may want to call a helper function from merge. PROGRAM: C
Given two sorted linked lists, merge them into a third sorted linked list. If an element...
Given two sorted linked lists, merge them into a third sorted linked list. If an element is present in both the lists, it should occur only once in the third list. Code needed in java.
Written in MIPS assembly language If given an array which is sorted in ascending order, as...
Written in MIPS assembly language If given an array which is sorted in ascending order, as well as its length and a target value. You are asked to find the index of this target value in the array using binary search algorithm. Here is an example: array 1, 4, 6, 8, 10, 12 length 6 target 8 2 In this case, the returned result should be assigned to be 3, as array[3] is equal to target. (Note: The target will...
You are given two arrays A1 and A2, each with n elements sorted in increasing order....
You are given two arrays A1 and A2, each with n elements sorted in increasing order. For simplicity, you may assume that the keys are all distinct from each other. Describe an o(log n) time algorithm to find the (n/2) th smallest of the 2n keys assuming that n is even.
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
This phyton program takes in three integer parameters and displays them in ascending order. You can...
This phyton program takes in three integer parameters and displays them in ascending order. You can assume that the numbers entered are integers. You CAN'T use lists, min(), max(), or the sort utility. The point is to use if tests. sortThreeIntegers( 39, 2, 5 ) should display: The numbers in order are: 2 5 39 sortThreeIntegers( 14, -12, -1000 ) should display: The numbers in order are: -1000 -12 14 def sortThreeIntegers( x, y, z ): < your code goes...
Write a method that takes two Sorted Arrays of different sizes and merges them into one...
Write a method that takes two Sorted Arrays of different sizes and merges them into one sorted array, and use the method to write a full recursive Merge Sort Algorithm.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT