1.

Given two arrays of integers X and Y of sizes m and n respectively. Write a function named MERGE() which will produce a third array named Z, such that the following sequence is followed:(i) All odd numbers of X from left to right are copied into Z from left to right(ii) All even numbers of X from left to right are copied into Z from right to left(iii) All odd numbers of Y from left to right are copied into Z from left to right(iv) All even numbers of Y from left to right are copied into Z from right to left X, Y and Z are passed as argument to MERGE().e.g., X is {3, 2, 1, 7, 6, 3} and Y is {9, 3, 5, 6, 2, 8, 10} the resultant array Z is {3, 1, 7, 3, 9, 3, 5, 10, 8, 2, 6, 6, 2}

Answer»

void MERGE(int X[],int Y[],int n,int m)

{ int Z[20],i=0,j=0,k=0,l=m+n-1;

while(i<n&&k<20)

{ if(X[i]%2!=0)

{ Z[k]=X[i];

k++;

i++;

}

else

{ Z[l]=X[i];

l--;

i++;

}

}

while(j<m&&k<20)

{ if(Y[j]%2!=0)

{ Z[k]=Y[j];

k++;

j++;

}

else

{ Z[l]=Y[j];

l--;

j++;

}

}

cout<<"The elements of an array C is:";

for(i=0;i<n+m;i++)

cout<<"\n"<<Z[i];

}



Discussion

No Comment Found

Related InterviewSolutions