Complementary task for topic: 8

M Nemeth · 2023-08-29 15:21:04.628218'

Dynamic arrays: Merge Two Sorted Arrays

Dynamic arrays: Merge Two Sorted Arrays

Create a C program that merges two sorted dynamic integer arrays into a new sorted dynamic array.
Expected bahvior:
Enter the size of the first array: 4
Enter the elements of the first array:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Enter the size of the second array: 3
Enter the elements of the second array:
Element 1: 15
Element 2: 25
Element 3: 35
Merged Sorted Array: 10 15 20 25 30 35 40

Hint: Define a function mergeArrays that takes two integer pointers (pointing to the two sorted dynamic arrays), the size of each array, and returns a pointer to the merged sorted dynamic array.
The mergeArrays function should combine the two input arrays into a new sorted array and return a pointer to it.
In the main function, ask the user to input the sizes of the two arrays.
Allocate memory for the two dynamic arrays based on the user input.
Ask the user to input the elements of the two dynamic arrays.
Call the mergeArrays function, passing the two dynamic arrays and their sizes.
Print the merged sorted array.

Solution
#include 
#include 

int* mergeArrays(int* arr1, int size1, int* arr2, int size2) {
    int i = 0, j = 0, k = 0;
    int totalSize = size1 + size2;
    int* mergedArray = (int*)malloc(totalSize * sizeof(int));

    while (i < size1 && j < size2) {
        if (arr1[i] < arr2[j]) {
            mergedArray[k] = arr1[i];
            i++;
        } else {
            mergedArray[k] = arr2[j];
            j++;
        }
        k++;
    }

    while (i < size1) {
        mergedArray[k] = arr1[i];
        i++;
        k++;
    }

    while (j < size2) {
        mergedArray[k] = arr2[j];
        j++;
        k++;
    }

    return mergedArray;
}

int main() {
    int size1, size2;

    printf("Enter the size of the first array: ");
    scanf("%d", &size1);

    int* array1 = (int*)malloc(size1 * sizeof(int));

    printf("Enter the elements of the first array:\n");
    for (int i = 0; i < size1; i++) {
        printf("Element %d: ", i + 1);
        scanf("%d", &array1[i]);
    }

    printf("\nEnter the size of the second array: ");
    scanf("%d", &size2);

    int* array2 = (int*)malloc(size2 * sizeof(int));

    printf("Enter the elements of the second array:\n");
    for (int i = 0; i < size2; i++) {
        printf("Element %d: ", i + 1);
        scanf("%d", &array2[i]);
    }

    int* mergedArray = mergeArrays(array1, size1, array2, size2);

    printf("\nMerged Sorted Array: ");
    for (int i = 0; i < size1 + size2; i++) {
        printf("%d ", mergedArray[i]);
    }

    free(array1);
    free(array2);
    free(mergedArray);

    return 0;
}



Explanation
    In this task, we define a mergeArrays function that takes two integer pointers arr1 and arr2 (pointing to the two sorted dynamic arrays), the size of each array, and returns a pointer to the merged sorted dynamic array.

    Inside the mergeArrays function, we use three variables i, j, and k to traverse the two input arrays (arr1 and arr2) and the merged array (mergedArray) respectively.

    We allocate memory for the mergedArray based on the total size of both arrays.

    We use a while loop to compare elements from both arrays. The smaller element is copied to the mergedArray, and the corresponding index is incremented.

    After the loop, we handle the remaining elements in case one of the arrays has more elements than the other.

    In the main function, we ask the user to input the sizes of the two arrays and allocate memory for them using malloc.

    We ask the user to input the elements of both arrays using two separate for loops.

    We call the mergeArrays function, passing the two dynamic arrays and their sizes.

    Finally, we print the merged sorted array using a for loop and free the dynamically allocated memory using free at the end of the program to avoid memory leaks.
< < previous    next > >