Write a function to merge two sorted arrays. (in the comments below, @pre refers
ID: 3611416 • Letter: W
Question
Write a function to merge two sorted arrays.(in the comments below, @pre refers to a condition true to thefunction call, whereas @post refers to a condition true immediatelyfollowing the function call):
typedef int Item;
void merge(const Item L[], const int lsize, const Item M[], constint msize, Item res[], int& rsize);
//@pre arrays L and M are considered sorted (innon-decreasing order with possible duplicates).
//@pre: lsize is set the number of vaild elements in L.
//@pre: msize is set to the number of vaild elements in M.
//@pre: res is as least as large as L and M combined.
//@post: the array res contains copies of every item in L andM in sorted order.
//@post: rsize is set to the number of vaild elements in res.
//It is required that the resulting array, res, be generated duringa single linear pass through the arrays L
// and M.
Example:
Argument Value
L 8 8 12 34 56 56 56 89
lsize 8
M 2 4 56 56 62 70
msize 6
res 2 4 8 8 12 34 56 56 56 56 56 62 70 89
rsize 14
Explanation / Answer
please rate - thanks #include using namespace std; typedef int Item; void merge(const Item L[], const int lsize, const Item M[], constint msize, Item res[], int& rsize); void fill(Item[], int&, const Item[],int,int&); void print(const Item[],int,string); int main() {intL[]={8,8,12,34,56,56,56,89},lsize=8,M[]={2,4,56,56,62,70},msize=6,res[50],rsize=0; merge(L,lsize,M, msize,res,rsize); print(L,lsize,"Original Array 1"); print(M,msize,"Original Array 2"); print(res,rsize,"Merged Array"); system("pause"); return 0; } void print(const Item A[], int len,string mess) {int i; coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.