1.

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 ad B in Ascending order. Use A, B, and C as arguments in the function.

Answer»

#include<iostream.h>

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

int mai()

{ int A[50],B[50],C[50],MN=0,M,N;

cout<<"How many elements do U want to create first array with? ";

cin>>M;

cout<<"Enter First Array's elements [ascending]...";

for(int i=0;i<M;i++)

cin>>A[i];

cout<<"How many elements do U want to create second array with? ";

cin>>N;

MN=M+N;

cout<<"Enter Second Array's elements [descending]...";

for(int i=0;i<N;i++)

cin>>B[i];

Merge(A,M,B,N,C);

cout<<"The merged array is....";

for(i=0;i<MN;i++)

cout<<C[i]<<" ";

cout<<endl;

return 0;

}

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

 { int a,b,c;

for(a=0,b=-1,c=0;a=0;)

{

if(A[a]<=B[b]) C[c++]=A[a++]

else C[c++]=B[b--];

}

if(a<M)

{ while(a<M)

C[c++]=A[a++];

}

else

{  while(b>=0)

 C[c++]=B[b--];

}

}



Discussion

No Comment Found

Related InterviewSolutions