EQuestionAnswers.com Computer/Electronics Questions and Answers
C, C++, VC++, COM/DCOM, DLL and more
#Login #Sign up  Facebook Twitter TGoogle+
 

Write a program to sort two arrays and combine them into one array using mergesort

Source Code

#include<stdio.h>
#include<conio.h>
int main (int argc, char *argv[])
{
  clrscr();
  int ar[100];
  int ar2[100];
  int arm[100];
  int r1,r2,r,i,j,f=0;
10    for(i=0;i<100;i++)
11    {
12      ar[i]=0;
13      ar2[i]=0;
14      arm[i]=0;
15    }
16    printf("\n Enter the range of two arrays:(less than 100) ");
17    scanf("%d %d",&r1,&r2);
18    printf("\n Enter the elements of the first array:");
19    for(i=0;i<r1;i++)
20    {
21         printf("\n Enter element number: %d ",(i+1));
22         scanf("%d",&ar[i]);
23    }
24    printf("\n Enter the elements of the second array:");
25    for(i=0;i<r2;i++)
26    {
27         printf("\n Enter element number: %d ",(i+1));
28         scanf("%d",&ar2[i]);
29    }
30    r=r1+r2;
31    for(i=0;i<r1;i++)
32    {
33      arm[i]=ar[i];
34    }
35    for(i=r1;i<r;i++)
36    {
37           for(j=0;j<i;j++)
38           {
39        if(ar2[i-r1]==arm[j])
40        {
41          f=1;
42          break;
43        }
44           }
45           if(f==0)
46         arm[i]=ar2[i-r1];
47    }
48    printf("\n The elements of merged array is:");
49    for(i=0;i<r;i++)
50    {
51      printf("%d ",arm[i]);
52    }
53    getch();
54  }

Find More from our code collection
Armstrong number, binary number to a decimal number, bubble sort, decimal number to binary number, factorial of the given number factors, fibonacci numbers, HCF and LCM, matrix, mergesort, salary of the employee. palindrome, quadratic equation, star patterns, series etc. and much more...
#Return to Example Source Code