bsc

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

root / semester_1 / practice-c / pc-ip-11.c

pc-ip-11.c (2031B)


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