Interview Questions   Tutorials   Discussions   Programs   Videos   

C++ - Write the program to merge the following arrays Array: A 1 18 22 43 Array: B 3 4 6 20 34 46 55 ?




375
views
asked marvit August 19, 2014 08:52 AM  

Write the program to merge the following arrays Array: A 1 18 22 43 Array: B 3 4 6 20 34 46 55 ?


           

1 Answers



 
answered By bhjahnavi   0  
/* program that merges two arrays*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();

int a[100],b[100],c[100],n,m,i,j,k,s;

cout<<"Merging Two Array's ";
cout<<"\n\nEnter No. of Elements in First Array : ";
cin>>n;

cout<<"\nEnter Elements in Sorted Order:\n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}

cout<<"\nEnter No. of Elements in Second Array : ";
cin>>m;

cout<<"\nEnter Elements in Sorted Order:\n";
for(i=1;i<=m;i++)
{
cin>>b[i];
}

i=1,j=1;

for(k=1;k<=n+m;k++)   
{
if(a[i]<b[j])      
{
c[k]=a[i];
i++;

if(i>n)
goto b;
}

else
{
c[k]=b[j];
j++;

if(j>m)
goto a;
}
}


a:       
for(s=i;s<=n;s++)     
{
k++;
c[k]=a[s];
}


b:
for(s=j;s<=m;s++)    
{
k++;
c[k]=b[s];
}

cout<<"\n\nAfter Merging Two Arrays:\n";

for(k=1;k<=n+m;k++)
{
cout<<c[k]<<endl;
}

getch();
}
flag   
   add comment

Your answer

Join with account you already have

FF

Preview


Ready to start your tutorial with us? That's great! Send us an email and we will get back to you as soon as possible!

Alert