C Programming Tutorial

 
 
 
 
#include<stdio.h>
#include<conio.h>

int main()
{
    int a[10],b[10],c[20],n1,n2,i,j,temp,k=0;
    clrscr();

    printf(\" Enter the no. of element for 1st array : \");
    scanf(\"%d\",&n1);

    for(i=0;i<n1;i++,k++)
    {
        printf(\" Enter element [%d] : \",i+1);
        scanf(\"%d\",&a[i]);
        c[k]=a[i];
    }

    for(i=0;i<n1;i++)
    {
        for(j=i+1;j<n1;j++)
        {
            if(a[i]>a[j])
            {
                temp=a[i];
                a[i]=a[j];
                a[j]=temp;
            }
        }
    }

    printf(\"\\n After sorting 1st array : \");
    for(i=0;i<n1;i++)
    {
        printf(\"\\n Element [%d] = %d\",i+1,a[i]);
    }


    printf(\"\\n\\n Enter the no. of element for 2nd array :  \");
    scanf(\"%d\",&n2);

    for(i=0;i<n2;i++,k++)
    {
        printf(\" Enter element [%d] : \",i+1);
        scanf(\"%d\",&b[i]);
        c[k]=b[i];
    }
    for(i=0;i<n2;i++)
    {
        for(j=i+1;j<n2;j++)
        {
            if(b[i]>b[j])
            {
                temp=b[i];
                b[i]=b[j];
                b[j]=temp;
            }
        }
    }

    printf(\"\\n After sorting 2nd array : \");
    for(i=0;i<n2;i++)
    {
        printf(\"\\n Element [%d] = %d\",i+1,b[i]);
    }
    for(i=0;i<n1+n2;i++)
    {
        for(j=i+1;j<n1+n2;j++)
        {
            if(c[i]>c[j])
            {
                temp=c[i];
                c[i]=c[j];
                c[j]=temp;
            }
        }
    }

    printf(\"\\n\\n\\n After combined and sorted both array :- \");
    for(i=0;i<n1+n2;i++)
    {
        printf(\"\\n Element [%d] = %d\",i+1,c[i]);
    }
    getch();

    return 0;
}



Output:

Enter the no. of element for 1st array :  5
Enter element [1] : 20
Enter element [2] : 18
Enter element [3] : 6
Enter element [4] : 12
Enter element [5] : 4

After sorting 1st array :
Element [1] = 4
Element [2] = 6
Element [3] = 12
Element [4] = 18
Element [5] = 20

Enter the no. of element for 2nd array :  3
Enter element [1] : 6
Enter element [2] : 2
Enter element [3] : 3

After sorting 2nd array :
Element [1] = 2
Element [2] = 3
Element [3] = 6


After combined and sorted both array :-
Element [1] = 2
Element [2] = 3
Element [3] = 4
Element [4] = 6
Element [5] = 6
Element [6] = 12
Element [7] = 18
Element [8] = 20

 
 
Didn't find what you were looking for? Find more on Program to merge 2 sorted array in a single array