Question

In: Computer Science

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

Solutions

Expert Solution

The answer to this question is as follows:

The code is as follows:

#include<stdio.h>
void mergeArrays(int A[], int B[], int size_1,
                           int size_2, int C[])
{
   int i = 0, j = 0, k = 0;

  
   while (i<size_1 && j <size_2)
   {
       //check if the first array element is greate than the
       //second array element if condition is true then
       //increment the result array and A array index otherwise
       //increment the B array index
       if (A[i] < B[j])
           C[k++] = A[i++];
       else
           C[k++] = B[j++];
   }

   //To store the elements that are left in A array
   while (i < size_1)
       C[k++] = A[i++];

   // To store the elements that are left in B array
   while (j < size_2)
       C[k++] = B[j++];
}

// Driver code
int main()
{
   int A[18],B[5],size_A,size_B;
   printf("Enter the size of the Array A:");
   scanf("%d",&size_A);
   printf("Enter the elements into array A:");
   for(int i=0;i<size_A;i++)
   {
   scanf("%d",&A[i]);
   }
   printf("Enter the size of the Array B:");
   scanf("%d",&size_B);
   printf("Enter the elements into array B:");
for(int i=0;i<size_B;i++)
   {
   scanf("%d",&B[i]);
   }
   int result[size_A+size_B];
   mergeArrays(A, B, size_A, size_B, result);

   //cout << "Array after merging" <<endl;
   for (int i=0; i < size_A+size_B; i++)
       printf("%d ",result[i]);

   return 0;
}

The input and output are provided in the screenshot below:


Related Solutions

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...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n]...
Divide and conquer problem. Suppose we are given two sorted arrays A[1 . . . n] and B[1 . . . n] and an integer k. Describe an algorithm to find the kth smallest element in the union of A and B in O(log n) time. For example, if k = 1, your algorithm should return the smallest element of A ∪ B; if k = n, your algorithm should return the median of A ∪ B.) You can assume...
In arduino: 1.Given two Arrays A= {2,4,7,8,3) and B={11,3,2,8,13). Write a program that find the numbers...
In arduino: 1.Given two Arrays A= {2,4,7,8,3) and B={11,3,2,8,13). Write a program that find the numbers into A but not in B. For the example C=A-B= {4,7} Print the values (Use for loops) 2.Create a vector of five integers each in the range from -10 to 100 (Prompt the user for the five integer x). Perform each of the following using loops: a) Find the maximum and minimum value b)Find the number of negatives numbers Print all results
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 the algorithm, following the ideas given in class, for merging two sorted arrays into one...
Write the algorithm, following the ideas given in class, for merging two sorted arrays into one sorted array. Note the algorithm is not the one for merge sort. 2) What is the best asymptotic upper bound for the algorithm? List reasoning steps.
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
This is for c++ Write a program that works with two arrays of the same size...
This is for c++ Write a program that works with two arrays of the same size that are related to each other in some way (or parallel arrays). Your two arrays must be of different data types. For example, one array can hold values that are used in a formula that produces the contents of the second array. Some examples might be:  from a previous program, populations and the associated flowrates for those populations (an int array of populations...
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.
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
Write a method, twoSumSorted2, that solves the following variant of the Two-Sum problem: Given a sorted...
Write a method, twoSumSorted2, that solves the following variant of the Two-Sum problem: Given a sorted array of integers where each element is unique and a target integer, return in an Array List, the indices of all pairs of elements that sum up to the target. Each pair of indices is also represented as an Array List (of two elements). Therefore, the method returns an Array List of an Array List of size 2. If no pair in the input...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT