Monday, November 4, 2013

Merging Arrays In C

Merging Arrays In C


//-------------------------------------------------------
//  Title : Merging of the Two arrays
//  a  - First Array
//  b  - Second Array
//  c  - Resultant Array after merging
//  i  - Subscript variable for array a
//  j  - Subscript variable for array b
//  k  - Subscript variable for array c
//  n1 - Total number of elements in array1
//  n2 - Total number of elements in array2
//-------------------------------------------------------

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[30],b[30],c[30],i,j,k,n1,n2;

 printf("n Enter no of elements in 1'st array :");
 scanf("%d",&n1);

 for(i=0;i 〈 n1;i++)
     scanf("%d",&a[i]);

 printf("n Enter no of elements in 2'nd array :");
 scanf("%d",&n2);

 for(i=0;i 〈 n2;i++)
  scanf("%d",&b[i]);

 i=0;j=0;k=0;  /* merging starts */

 while(i 〈 n1 && j 〈 n2)
 {
  if(a[i] 〈= b[j])
   {
   c[k]=a[i];
   i++;k++;
   }
  else
   {
   c[k]=b[j];
   k++;j++;
   }
 }

 /* Some elements in array 'a' are still remaining
    where as the array 'b' is exhausted */

 while(i 〈 n1)
  {
  c[k]=a[i];
  i++;k++;
  }

 /* some elements in array b are still remaining
    whereas the array 'a' is exhausted */

 while(j 〈 n2)
  {
  c[k]=b[j];
  k++;j++;
  }

 /* Displaying elements of array 'c' */

        printf("nMerged array is :");

        for(i=0;i 〈 n1+n2;i++)
           printf("n %d",c[i]);
getch();

}

No comments: