//------------------------------------------------------- // 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("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("Merged array is :"); for(i=0;i 〈 n1+n2;i++) printf("n %d",c[i]); getch(); }
About Me
- Amit Singh Thakur
- Raipur, Chhattisgarh, India
- Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.
Monday, September 2, 2013
C Program to Merge Two arrays in C Programming
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment