bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

pc-ip-11.c (2222B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 05 Jan 2026
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /*
      9  * Question 11:
     10  * Write a program to merge two sorted integer arrays to form a single sorted array.
     11  */
     12 
     13 #include <stdio.h>
     14 #include <stdlib.h>
     15 
     16 void merge(int[], int, int[], int);
     17 void inputArray(int[], int);
     18 void printArray(int[], int);
     19 
     20 int main()
     21 {
     22     int a, b, *arra = NULL, *arrb = NULL;
     23     printf("Enter the number of element for array A and B: ");
     24     scanf("%d %d", &a, &b);
     25     arra = (int *)malloc(a * sizeof(int));
     26     if (arra == NULL)
     27     {
     28         printf("\nMemory Allocation failed.");
     29         return 1;
     30     }
     31     arrb = (int *)malloc(b * sizeof(int));
     32     if (arrb == NULL)
     33     {
     34         printf("\nMemory Allocation failed.");
     35         return 1;
     36     }
     37     printf("\nEnter element for array A:\n");
     38     inputArray(arra, a);
     39     printf("\nEnter element for array B:\n");
     40     inputArray(arrb, b);
     41     printf("\nGiven array A: ");
     42     printArray(arra, a);
     43     printf("\nGiven array b: ");
     44     printArray(arrb, b);
     45     merge(arra, a, arrb, b);
     46     free(arra);
     47     free(arrb);
     48     return 0;
     49 }
     50 
     51 void inputArray(int arr[], int n)
     52 {
     53     int i;
     54     for (i = 0; i < n; i++)
     55     {
     56         printf("Enter element %d: ", i + 1);
     57         scanf("%d", &arr[i]);
     58     }
     59 }
     60 
     61 void printArray(int arr[], int n)
     62 {
     63     int i;
     64     printf("[");
     65     for (i = 0; i < n; i++)
     66     {
     67         printf("%d", arr[i]);
     68         if (i < n - 1)
     69         {
     70             printf(", ");
     71         }
     72     }
     73     printf("]");
     74 }
     75 
     76 void merge(int arra[], int a, int arrb[], int b)
     77 {
     78     int i, j, k, *arr = NULL, n = a + b;
     79     arr = (int *)malloc(n * sizeof(int));
     80     if (arr == NULL)
     81     {
     82         printf("\nMemory Allocation failed.");
     83         return;
     84     }
     85     i = j = k = 0;
     86     while (i < a && j < b)
     87     {
     88         if (arra[i] < arrb[j])
     89         {
     90             arr[k++] = arra[i++];
     91         }
     92         else
     93         {
     94             arr[k++] = arrb[j++];
     95         }
     96     }
     97     while (i < a)
     98     {
     99         arr[k++] = arra[i++];
    100     }
    101     while (j < b)
    102     {
    103         arr[k++] = arrb[j++];
    104     }
    105     printf("\nMerged Array: ");
    106     printArray(arr, n);
    107     free(arr);
    108 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror