Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

//\"It should display2,4,8,8,12,34,56,56,56,56,56,62,70,89 But it displayseveryt

ID: 3611440 • Letter: #

Question

//"It should display2,4,8,8,12,34,56,56,56,56,56,62,70,89 But it displayseverything, except it turns the 89 into a 0. What is wrong with mycode?



#include<iostream>
using namespace std;

void merge(const int L[], const int lsize, const int M[], const intmsize,int res[], int& rsize)

{

    int lindex=0;
    int mindex=0;
    int rindex=0;
  
   for(int j=0; j<rsize;j++)

   {
      
   if(M[mindex]<L[lindex])
      
   {
          
   res[rindex]=M[mindex];
      
   mindex++;
    rindex++;
      
   }
  
   else if(L[lindex]<M[mindex])
  
   {
     res[rindex]=L[lindex];
     lindex++;
     rindex++;
     }
      
   else if(M[mindex]==L[lindex])
  
   {
          
   res[rindex]=M[mindex];
     res[rindex+1]=L[lindex];
     mindex++;
     lindex++;
     rindex+=2;
   j++;
    
   }
  
   }
  

}

int main()

{
  
   int lsize=8;
    int L[]={8,8,12,34,56,56,56,89};
    int msize=6;
    int M[]={2,4,56,56,62,70};
    int rsize=lsize+msize;
    int res[rsize];

   merge(L,lsize,M,msize,res,rsize);

   for(int k =0;k < rsize;k++)

   {
      
   cout<<res[k]<<endl;


   }


}


Explanation / Answer

/* The changes made are highlighted in the program. You did not consider the cases when lindex = lsize or rindex= rsize */ #include using namespace std; void merge(const int L[], const int lsize, const int M[], const intmsize,int res[], int& rsize) {     int lindex=0;     int mindex=0;     int rindex=0;    //cout