CPPForSchool.com: Array - Single Dimension Set 1 Assignment 8

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



For this assignment, I was very confused on the algorithm for combining the two arrays.  However, it is slowly becoming more apparent as to what is occurring the more I review it.  Nevertheless, I am going to move forward with the assignments.  Below is my source that has been greatly inspired by the sample source code.  From what I understand, the reason why the program was designed to take values in a specified order, either ascending or descending, is to ensure the sequential order of array C when printed.  Still, for this particular task, I will need to practice it a bit more before I can feel comfortable with the process.











---------------------------------------------------------------------------------------------------------------------
Sample Source Code
-------------------------

#include<iostream>
using namespace std;

void Merge(int A[], int B[], int C[], int N, int M, int &K);


int main()
{
 int A[100], B[100], C[200],i,n,m,k;

 cout<<"\nEnter number of elements you want to insert in first array ";
 cin>>n;

 cout<<"Enter element in ascending order\n";
 for(i=0;i<n;i++)
 {
  cout<<"Enter element "<<i+1<<":";
  cin>>A[i];
 }

 cout<<"\nEnter number of elements you want to insert in second array ";
 cin>>m;

 cout<<"Enter element in descending order\n";
 for(i=0;i<m;i++)
 {
  cout<<"Enter element "<<i+1<<":";
  cin>>B[i];
 }

 Merge(A,B,C,n,m,k);

 cout<<"\nThe Merged Array in Ascending Order"<<endl;
 for(i=0;i<k;i++)
 {
  cout<<C[i]<<" ";
 }

 return 0;
}


void Merge(int A[], int B[], int C[], int N, int M, int &K)
{
 int I=0, J=M-1;
 K=0;

 while (I<N && J>=0)
 {
  if (A[I]<B[J])
   C[K++]=A[I++];
  else if (A[I]>B[J])
   C[K++]=B[J--];
  else
  {
   C[K++]=A[I++];
   J--;
  }
 }

 for (int T=I;T<N;T++)
  C[K++]=A[T];
 for (int T=J;T>=0;T--)
  C[K++]=B[T];
}



Comments

Popular posts from this blog

CPPForSchool.com: Variable, Operator & Expression Set 1: Assignment 9

CPPForSchool.com: Array - Single Dimension Set 1 Assignment 6

CPPForSchool.com: Variable, Operator & Expression Set 1: Assignment 8