Time limit: 500ms
Memory limit: 256mb
Description:
Based on Exercise 1 above, your task is to merge one integer array A_2[100] to another integer array A_1[100]. Both A_1[], A_2[] store sorted integers in descending order and all integers of A_1[] and A_2[] are not distinct.
You could assume that A_1[] and A_2[] are always enough to store all the integers, before and after merging (i.e. length A_1 <= 100, length A_2 <= 100, length A_1 + length A_2 <= 100).
int Merge(int A_1[][], int A_2[][], int n_1, int n_2);
Example:
Given A_1[] = [5,3,2,1], A_2[] = [6,3,2,1]
merge A_2[] = [6,3,2,1] to A_1[] = [5,3,2,1]
Array A_1[] will be something like: [6,5,3,3,2,2,1,1], the function returns new length n_1’ = 7
Sample input:
4
5 3 2 1
4
6 3 2 1
Sample output:
6 5 3 3 2 2 1 1
-------------------------Copy the following code, complete it and submit-------------------------
#include <stdio.h>
int Insert(int value, int A[], int n)
{
// insert value into A[], return new length n
}
void Print(int A[], int n)
{
// print A[0], A[1], ..., A[n-1] in one line, every two integers are separated by a space
}
int Merge(int A_1[], int A_2[], int n_1, int n_2){
}
int main()
{
int A_1[100], A_2[100];
int n_1, n_2; // the number of integers in A
int value;
// printf("Array initialization: \n");
// printf("How many integers in FIRST array initially: ");
scanf("%d", &n_1);
// printf("What are the integers: ");
int temp = 0;
for(int i=0; i<n_1; i++){
scanf("%d", &value);
temp = Insert(value, A_1, temp);
}
// printf("How many integers in SECOND array initially: ");
scanf("%d", &n_2);
// printf("What are the integers: ");
temp = 0;
for(int i=0; i<n_2; i++){
scanf("%d", &value);
temp = Insert(value, A_2, temp);
}
// printf("Merge the SECOND array to the FIRST array: \n");
n_1 = Merge(A_1, A_2, n_1, n_2);
// printf("FIRST array: \n");
Print(A_1, n_1);
return 0;
}
-------------------------------------------End of Code-------------------------------------------