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

PLEASE HELP WITH C++ Suppose A, B, C are arrays of integers of size M, N, and M+

ID: 3807118 • Letter: P

Question

PLEASE HELP WITH C++

Suppose A, B, C are arrays of integers of size M, N, and M+N respectively. The numbers in array A appear in ascending order while the numbers in array B appear in descending order. Write a user defined function in C++ to produce third array C by merging arrays A and B in ascending order. Use A, B, and C as arguments in the function. You will need to ask user to input M and N, and based on those inputs user needs to fill the arrays A and B in an appropriate order. If user wants to input wrong order of values, your program should not accept it, and should ask for appropriate value.

Explanation / Answer

#include<iostream>

using namespace::std;

void merge(int a[], int sizeA, int b[], int sizeB, int c[]){
   int i=0, j=0, k=0;

   while (i<sizeA && j<sizeB) {
       if (a[i] < b[j])
           c[k++] = a[i++];
       else
           c[k++] = b[j++];
   }

   for( ;i<sizeA; i++)
       c[k++] = a[i];

   for( ;j<sizeA; j++)
       c[k++] = b[j];

}


int main() {
   int *a, *b, *c, m, n, i, j;

   cout<<"Enter first array size: ";
   cin>>m;

   cout<<"Enter second array size: ";
   cin>>n;

   a = new int[m];
   b = new int[n];
   c = new int[m + n];
   cout<<"Enter first array elements in accending order: a[0]= ";
   cin>>a[0];

   for (i=1; i<m; i++) {
       cout<<"a["<<i<<"]= ";
       cin>>a[i];
       if ( a[i] < a[i-1]) {
           cout<<"Please enter elemnts in accending order"<<endl;
           i--;
       }
   }

   cout<<"Enter second array elements in accending order: b[0]= ";
   cin>>b[0];

   for (i=1; i<n; i++) {
       cout<<"b["<<i<<"]= ";
       cin>>b[i];
       if ( b[i] < b[i-1]) {
           cout<<"Please enter elemnts in accending order"<<endl;
           i--;
       }
   }

   merge(a,m,b,n,c);
   cout<<"Content of array c"<<endl;
   for (i=0; i<m+n; i++)
       cout<<"b["<<i<<"]= "<<c[i]<<endl;  
   return 0;  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote