|
|
|
/* Program of merging two sorted arrays into a third sorted array*/ #include
main() { int arr1[20],arr2[20],arr3[40]; int i,j,k; int max1,max2;
printf("Enter the number of elements in list1 : "); scanf("%d",&max1); printf("Take the elements in sorted order :\n"); for(i=0;i { printf("Enter element %d : ",i+1); scanf("%d",&arr1[i]); } printf("Enter the number of elements in list2 : "); scanf("%d",&max2); printf("Take the elements in sorted order :\n"); for(i=0;i { printf("Enter element %d : ",i+1); scanf("%d",&arr2[i]); } /* Merging */ i=0; /*Index for first array*/ j=0; /*Index for second array*/ k=0; /*Index for merged array*/
while( (i < max1) && (j < max2) ) { if( arr1[i] < arr2[j] ) arr3[k++]=arr1[i++]; else arr3[k++]=arr2[j++]; }/*End of while*/ /*Put remaining elements of arr1 into arr3*/ while( i < max1 ) arr3[k++]=arr1[i++]; /*Put remaining elements of arr2 into arr3*/ while( j < max2 ) arr3[k++]=arr2[j++];
/*Merging completed*/ printf("List 1 : "); for(i=0;i printf("%d ",arr1[i]); printf("\nList 2 : "); for(i=0;i printf("%d ",arr2[i]); printf("\nMerged list : "); for(i=0;i printf("%d ",arr3[i]); printf("\n"); }/*End of main()*/
|
| Author: Lenin 10 Jun 2008 | Member Level: Diamond Points : 2 |
hi giri are you a computer student , i know only small programs in C.But i know C is a SEA.Am i right ? I remember the days when i used to mugup the codings for Lab exams.still c fear is there. Also still C is in flying colours
|