Right Click to Search

Tuesday, December 29, 2024

Merging of Two arrays in C Programming


//-------------------------------------------------------
//  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
#include
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();
}


Tags / Keywords : |

Stumble
Delicious
Technorati
Twitter
Facebook

0 Comments:

Post a Comment

Your Feedback :This is Growing Site and Your Feedback is important for Us to improve the Site performance & Quality of Content.Feel Free to contact and Please Provide Name & Contact Email

 

Learn C Programming Copyright © 2010 LKart Theme is Designed by Lasantha, Free Blogger Templates