Question

In: Computer Science

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};

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;

}

Solutions

Expert Solution

The C++ code along with the comments is given below.

#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,j,temp;

    //copy array a into array c
    for(i=0, j=0; i<size; i++, j++)
    {   c[i]=a[j];
    }
  
    //copy array b to the end of array c
    for(i=size, j=0; i<2*size; i++, j++)
    {   c[i]=b[j];
    }
  
    //sort the array c in descending order
    for(i=0; i<2*size; i++)
    {   for(j=i+1; j<2*size; j++)
        {   if(c[j]>c[i])   //if c[j]> c[i], then swap the values to make it in descending order
            {   temp=c[j]; //store c[j] to a variable
                c[j]=c[i]; //update c[j] with c[i]
                c[i]=temp; //update c[i] with value in the temp, now the values are swapped
            }
        }
    }
}

void printArray(int m[], int length)
{
    for(int i = 0; i < length; i++)
        cout<< m[i]<<" ";
    cout<<endl;
}

The screenshot of the function snippet is given below.

The output of the code is given below.

Explanation

  • Two sorted arrays a and b are given. The aim is to create an array which merges the two sorted arrays in descending order. Let the resultant array be called c.
  • Copy the contents of the array a to the array c. This is done by using a for loop to traverse each element of the array and stores them into the array c. Traversal of both arrays starts from the position one or the zeroth index.
  • Append the contents of the array b to the array c. This is also done by using a for loop. But, the array c already has some contents in it. So, the starting position of the array c will be the index 'size' or (size+1)'th position.
  • Now, the array c contains the contents of both the arrays a and b.
  • Sort the array c in descending using any sorting method. Here, bubble sort is used.
  • Each element is compared with the other elements and if an element is in wrong position, it will be swapped.
  • In C++, there is no inbuilt method to swap two values. So, with the help of an extra variable, the two values will be swapped.
  • The resultant array will be displayed in the output console.

Hope this helps. Doubts, if any, can be asked in the comment section.


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]...
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]...
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...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and...
Write a C program. Problem 1: You are given two sorted arrays, A and B, and A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order without using any other array space. Implementation instruction: (1) Define one array of size 18 for A and the other array of size 5 for B. (2) Initialize A by inputting 13 integers in the ascending order, and Initialize B by...
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:
Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
In this task you will implement a C++ function with arguments: a sorted integer array, size...
In this task you will implement a C++ function with arguments: a sorted integer array, size of the array, and a target integer value. Find all combinations of two elements in the sorted array which sum up to the target value. When found, add the combinations into an array, and print it. Where there is greater than one combination, you may use the number 200 as a separator for the combinations in the output array. Do not use more than...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT