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 inputting 5 integers in the ascending order. (Note: don’t hard code the integers of the arrays.) (3) Merge B with A in a way all values are sorted. (4) Print out the updated array A, after merging with B. For example: If your input for A is 1, 3, 11, 15, 20, 25, 34, 54, 56, 59, 66, 69, 71 and your input for B is 2, 4, 5, 22, 40 Finally, after merging A and B, A becomes 1, 2, 3, 4, 5, 11, 15, 20, 22, 25, 34, 40, 54, 56, 59, 66, 69, 71

Respuesta :

Answer:

#include <stdio.h>

#include <stdlib.h>

//Define the main function.

int main()

{

//Define the variables.

int nArr1=0,nArr2=0;

//Prompt the user to enter the size of the array.

printf("Enter the size of first array :\n");

scanf("%d",&nArr1);

printf("Enter the size of second array :\n");

scanf("%d",&nArr2);

//Define the array and loop variable.

int A[nArr1+nArr2], B[nArr2], aiter=0,biter=0,a=0;

//Prompt th user to enter arary A

printf("Enter Array A :\n");

for(aiter=0;aiter<nArr1;aiter++)

scanf("%d",&A[aiter]);

  //Prompt th user to enter arary B

printf("Enter Array B :\n");

for(biter=0;biter<nArr2;biter++)

scanf("%d",&B[biter]);

//Merge the arrays in to array A.

for(int i=0;i<nArr2;i++)

  A[nArr1+i]=B[i];

 

//Sort the merged array.

for (aiter = 0; aiter < nArr1+nArr2; ++aiter)

   {

       for (biter = aiter + 1; biter < nArr1+nArr2; ++biter)

       {

           if (A[aiter] >= A[biter])

           {

               a = A[aiter];

               A[aiter] = A[biter];

               A[biter] = a;

           }

       }

   }

//Display the result.

printf("\nThe final Array A after merge is:\n");

for(aiter=0;aiter<nArr1+nArr2;aiter++)

printf("%d ",A[aiter]);

printf("\n");

//Return the value 0.

return 0;

}

Step-by-step explanation: