Resources » Articles/Knowledge Sharing » Computer & Technology

C program-Program of merging two sorted arrays into a third sorted array


Posted Date: 10-Jun-2008  Last Updated:   Category: Computer & Technology    
Author: Member Level: Gold    Points: 2



/* 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()*/


Did you like this resource? Share it with your friends and show your love!




Responses to "C program-Program of merging two sorted arrays into a third sorted array"
Author: Lenin    10 Jun 2008Member Level: Gold   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



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Awards & Gifts
    Active Members
    TodayLast 7 Daysmore...

    ISC Technologies, Kochi - India. Copyright © All Rights Reserved.