Digital जीवन

Free Online Education for India...

Full width home advertisement

Computer Basic

C Programming

Engineering Graphics

Post Page Advertisement [Top]

Dr. A.P.J. Abdul Kalam Technical University, Lucknow

KCS151 / KCS251 Programming for Problem Solving - Using C Language


Lab Exercises


26. Write a program to merge two sorted arrays & no element is repeated during merging.


/*
 File: Prgrm26.c
 Author: Aditya Saini
 Date: Jan 17, 2021
 Description: Program to merge two sorted arrays & no element is repeated during merging.
*/

#include <stdio.h>

int main (void)
{
     int ar_1[10], ar_2[10];
     int merge_ar[20];
     int i, j, k;

     //Input first array.
     printf ("***Input first sorted array***\n");
     i = 0;
     while (i <= 9)
     {
          printf ("Input %d element: ", i + 1);
          scanf ("%d", &ar_1[i]);

          if (i > 0 && ar_1[i] <= ar_1[i - 1])
          {
               printf ("Error! %d is smaller than or equal to previous element. Try again.\n", ar_1[i]);
               continue;
          }
          i++;
     }

     //Input second array.
     printf ("\n***Input second sorted array***\n");
     i = 0;
     while (i <= 9)
     {
          printf ("Input %d element: ", i + 1);
          scanf ("%d", &ar_2[i]);

          if (i > 0 && ar_2[i] <= ar_2[i - 1])
          {
               printf ("Error! %d is smaller than or equal to previous element. Try again.\n", ar_2[i]);
               continue;
          }
          i++;
     }

     //Print first array
     printf ("\nFirst Array: ");
     for (i = 0; i <= 9; i++)
          printf ("%d ", ar_1[i]);
     printf ("\n");

     //Print second array
     printf ("Second Array: ");
     for (i = 0; i <= 9; i++)
          printf ("%d ", ar_2[i]);
     printf ("\n");

     //Merge arrays
     i = 0;
     j = 0;
     k = 0;
     while (i <= 9 && j <= 9)
     {
          if (ar_1[i] <= ar_2[j])
          {
               if (k == 0 || merge_ar[k - 1] < ar_1[i])
               {
                    merge_ar[k] = ar_1[i];
                    k++;
               }
               i++;
          }
          else
          {
               if (k == 0 || merge_ar[k - 1] < ar_2[j])
               {
                    merge_ar[k] = ar_2[j];
                    k++;
               }
               j++;
          }
     }
     if (i < j)
     {
          while (i <= 9)
          {
               merge_ar[k] = ar_1[i];
               i++;
               k++;
          }
     }
     else
     {
          while (j <= 9)
          {
               merge_ar[k] = ar_2[j];
               j++;
               k++;
          }
     }

     //Print Merge array
     printf ("Merge Array: ");
     for (i = 0; i <= k - 1; i++)
          printf ("%d ", merge_ar[i]);

     return 0;
};

Output

***Input first sorted array***
Input 1 element: 5
Input 2 element: 7
Input 3 element: 3
Error! 3 is smaller than or equal to previous element. Try again.
Input 3 element: 9
Input 4 element: 15
Input 5 element: 17
Input 6 element: 19
Input 7 element: 20
Input 8 element: 23
Input 9 element: 25
Input 10 element: 27

***Input second sorted array***
Input 1 element: 3
Input 2 element: 5
Input 3 element: 9
Input 4 element: 12
Input 5 element: 15
Input 6 element: 17
Input 7 element: 18
Input 8 element: 27
Input 9 element: 30
Input 10 element: 35

First Array: 5 7 9 15 17 19 20 23 25 27
Second Array: 3 5 9 12 15 17 18 27 30 35
Merge Array: 3 5 7 9 12 15 17 18 19 20 23 25 27 27 30 35



No comments:

Post a Comment

Please do not post spam links.

Bottom Ad [Post Page]

| Designed by Colorlib