Friday, January 16, 2015

Merging Two Arrays- Learn

Description:  A Program to illustrate Merging of two arrays in ascending order.

Compiler:  Turbo C++ 3.0
Author:  Naman Kumar



#include<iostream.h>
#include<conio.h>

main()
{
   clrscr();

   int i,j,k,o,n,m,a[20],b[20],c[40];
   cout<<"Enter no of elements in 1st array : ";
   cin>>n;
   cout<<"\nEnter ele of 1st array : ";
   for(i=0;i<n;i++)
     cin>>a[i];

   cout<<"Enter no of elements in 2nd array : ";
   cin>>m;
   cout<<"Enter element of 2nd array : ";
   for(j=0;j<m;j++)
     cin>>b[j];

   for(i=0,j=0,k=0;i<n,j<m; )
    {
      if(a[i]<=b[j])
c[k++]=a[i++];
      else
c[k++]=b[j++];
     }
    if(i<n)
     {
       while(i<n)
c[k++]=a[i++];
      }
    else
      {
while(j<m)
 c[k++]=b[j++];
       }

       o=m+n;
       cout<<"Merged Array : ";
       for(int z=0;z<o;z++)
cout<<c[z]<<' ';
 getch();

}

Please LIKE & SHARE US if you find this helpful!

Share

& Comment

0 comments:

Post a Comment

 

Copyright © 2015 Code Fervor™ is a registered trademark.

Designed by Templateism By Naman Kumar