C++ Program – Merge two Arrays

by Nideesh C on May 2, 2011 · 0 comments

in C++




//Downloaded From theonlinetutorials.com
//(C)2011.All rights reserved.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int k=0,i,na,nb,j,a[50],b[50],c[50];
cout<<"Enter A lim"<<endl;
cin>>na;
cout<<"Enter B lim"<<endl;
cin>>nb;
cout<<"Enter A ele";
for(i=0;i<na;i++)
cin>>a[i];
cout<<"Enter B ele"<<endl;
for(j=0;j<nb;j++)
cin>>b[j];
j=0;
i=na-1;
cout<<"Processing";
while((i>=0)&&(j<nb))
{
if((b[j]<a[i]))
{
c[k]=a[i];
i--;
k++;
}
if((b[j]>a[i]))
{
c[k]=b[j];
j++;
k++;
}
if((b[j]==a[i]))
{
c[k]=b[j];
j++;
k++;
c[k]=a[i];
i--;
k++;
}
}

if(i==-1)
{
while(j<nb)
{
c[k]=b[j];
j++;
k++;
}
}

else if(j==nb)
{
while(i>=0)
{
c[k]=a[i];
i--;
k++;
}
}

cout<<"The New Array is"<<endl;
for(k=0;k<na+nb;k++)
cout<<c[k]<<endl;

getch();
}



Not Satisfied ? Just search & get the result

Related Posts Plugin for WordPress, Blogger...
Be Sociable, Share!

Related posts:

  1. C++ Program – Insert & Delete an element from array
  2. C++ Program – BUBBLE SELECTION SORT
  3. C++ rogram to implement merge sort
  4. C++ Program – Insert Delete from 1-D array
  5. C Program to find the perfect numbers below a given number.

Leave a Comment

Previous post:

Next post: