Saturday, March 18, 2023

C Programming. Program to merge two arrays and get an ordered array.

 Program to merge two arrays and get an ordered array.

// Program to merge two array and get an ordered array.

#include<stdio.h>

int main() {
    int n1, n2, i, j, t;
    printf("Enter the size of 1st array : ");
    scanf("%d", &n1);
    printf("Enter the size of 2nd array : ");
    scanf("%d", &n2);
    int a[n1], b[n2], c[n1+n2];
    printf("Enter the elements of 1st array in ordered manner : \n");
    for (i=0; i<n1; i++){
        scanf("%d",&a[i]);
    }
    printf("Enter the elements of 2nd array in ordered manner : \n");
    for (i=0; i<n2; i++){
        scanf("%d",&b[i]);
    }
    i=0;
    while (i<n1){
        c[i]=a[i];
        i++;
    }
    j=0;
    while (j<n2){
        c[i]=b[j];
        i++;
        j++;
    }
    for (i=0; i<n1+n2; i++){
        for (j=i; j<n1+n2; j++){
            if (c[i]>c[j]){
                t=c[i];
                c[i]=c[j];
                c[j]=t;
            }
        }
    }
    printf("After merging the array is :\n");
    for (i=0; i<n1+n2; i++){
        printf("%d ",c[i]);
    }
    return 0;
}

Output :

Enter the size of 1st array : 4 Enter the size of 2nd array : 5 Enter the elements of 1st array in ordered manner : 1 3 5 7 Enter the elements of 2nd array in ordered manner : 2 4 6 8 9 After merging the array is : 1 2 3 4 5 6 7 8 9

No comments:

Post a Comment

Selection Sort Using C programming.

Selection sort is a simple and efficient sorting algorithm that works by repeatedly selecting the smallest (or largest) element from the un...