Write a C Program to merge two sorted array in descending order

By | 16.11.2016

C Merge two sorted array in descending order


Write a C Program to merge two sorted array in descending order. Here’s simple Program to merge two sorted array in descending order in C Programming Language.


What is an Array ?


Arrays a kind of data structure that can store a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, …, and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and …, numbers[99] to represent individual variables. A specific element in an array is accessed by an index.

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.


Here is source code of the C Program to merge two sorted array in descending order. The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/*  C Program to merge two sorted array in descending order  */

#include <stdio.h>
#define MAX_SIZE 100 //Maximum size of the array

int main()
{
    int arr1[100], arr2[100], arr3[200];
    int size1, size2, size3;
    int i, j, k;

    /*
     * Reads size of first array and element in first array
     */
    printf("Enter the size of first array : ");
    scanf("%d", &size1);
    printf("\nEnter elements in first array :: \n");
    for(i=0; i<size1; i++)
    {
        printf("\nEnter %d element in first array :: ",i+1);
        scanf("%d", &arr1[i]);
    }

    /*
     * Read size of second array and elements in second array
     */
    printf("\nEnter the size of second array : ");
    scanf("%d", &size2);
    printf("\nEnter elements in second array : \n");
    for(i=0; i<size2; i++)
    {
        printf("\nEnter %d element in second array :: ",i+1);
        scanf("%d", &arr2[i]);
    }

    /* size of merged array is size_of_first_array + size_of_second_array */
    size3 = size1 + size2;

    /*
     * Merges both array in descending order
     */
    for(i=0, j=0, k=0; i<size3; i++)
    {
        /*
         * If all elements of one array
         * is merged to final array
         */
        if(j >= size1 || k >= size2)
        {
            break;
        }

        if(arr1[j] > arr2[k])
        {
            arr3[i] = arr1[j];
            j++;
        }
        else
        {
            arr3[i] = arr2[k];
            k++;
        }
    }

    /*
     * Merge the remaining elements of array
     */
    while(j < size1)
    {
        arr3[i] = arr1[j];
        i++;
        j++;
    }
    while(k < size2)
    {
        arr3[i] = arr2[k];
        i++;
        k++;
    }

    /*
     * Prints the merged array
     */
    printf("\nArray merged in descending order : ");
    for(i=0; i<size3; i++)
    {
        printf(" %d ", arr3[i]);
    }

    return 0;
}

OUTPUT : :


Enter the size of first array : 4

Enter elements in first array ::

Enter 1 element in first array :: 1

Enter 2 element in first array :: 2

Enter 3 element in first array :: 3

Enter 4 element in first array :: 4

Enter the size of second array : 4

Enter elements in second array :

Enter 1 element in second array :: 5

Enter 2 element in second array :: 6

Enter 3 element in second array :: 7

Enter 4 element in second array :: 8

Array merged in descending order :  5  6  7  8  1  2  3  4

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

2.5 4 votes
Article Rating
Category: Arrays Programs C Programming Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments