#include #include #include #include #define MAX 128 int takeInput(int** firstArray, int* firstCount, int** secondArray, int* secondCount); void printArray(const char* label, int* arr, int count); void bubbleSortArrays(int* firstArray, int firstCount, int* secondArray, int secondCount); int* combineArrays(int* firstArray, int firstCount, int* secondArray, int secondCount, int* combinedCount); /************************************************** * * @brief - Entry point of the program. * **************************************************/ int main() { int* firstArray = NULL; int* secondArray = NULL; int* combinedArray = NULL; int firstCount = 0; int secondCount = 0; int combinedCount = 0; if (takeInput(&firstArray, &firstCount, &secondArray, &secondCount) == 0) { bubbleSortArrays(firstArray, firstCount, secondArray, secondCount); printArray("First array", firstArray, firstCount); printArray("Second array", secondArray, secondCount); combinedArray = combineArrays(firstArray, firstCount, secondArray, secondCount, &combinedCount); if (combinedArray != NULL) { printArray("Combined array", combinedArray, combinedCount); free(combinedArray); } } free(firstArray); free(secondArray); return 0; } /************************************************** * * @brief - Takes input for the size and elements * of arrays. * **************************************************/ int takeInput(int** firstArray, int* firstCount, int** secondArray, int* secondCount) { char inputStr[MAX]; const int initialCapacity = 8; *firstArray = (int*)malloc(initialCapacity * sizeof(int)); *secondArray = (int*)malloc(initialCapacity * sizeof(int)); if (*firstArray == NULL || *secondArray == NULL) { return -1; } int firstCapacity = initialCapacity; int secondCapacity = initialCapacity; // First Array printf("Enter numbers for the first array: "); if (!fgets(inputStr, sizeof(inputStr), stdin)) { printf("Input error!\n"); return -1; } inputStr[strcspn(inputStr, "\n")] = 0; char* context = NULL; char* token = strtok_s(inputStr, " ", &context); *firstCount = 0; while (token) { if (*firstCount >= firstCapacity) { firstCapacity *= 2; int* temp = (int*)realloc(*firstArray, firstCapacity * sizeof(int)); if (!temp) { return -1; } *firstArray = temp; } (*firstArray)[(*firstCount)++] = atoi(token); token = strtok_s(NULL, " ", &context); } // Second Array printf("Enter numbers for the second array: "); if (!fgets(inputStr, sizeof(inputStr), stdin)) { printf("Input error!\n"); return -1; } inputStr[strcspn(inputStr, "\n")] = 0; context = NULL; token = strtok_s(inputStr, " ", &context); *secondCount = 0; while (token) { if (*secondCount >= secondCapacity) { secondCapacity *= 2; int* temp = (int*)realloc(*secondArray, secondCapacity * sizeof(int)); if (!temp) { return -1; } *secondArray = temp; } (*secondArray)[(*secondCount)++] = atoi(token); token = strtok_s(NULL, " ", &context); } return 0; } /************************************************** * * @brief - Prints the elements of an array. * **************************************************/ void printArray(const char* label, int* arr, int count) { printf("%s: ", label); for (int i = 0; i < count; i++) { printf("%d ", arr[i]); } printf("\n"); } /************************************************** * * @brief - Sorts the two arrays in ascending order * using bubble sort. * **************************************************/ void bubbleSortArrays(int* firstArray, int firstCount, int* secondArray, int secondCount) { int temp = 0; // Sorts first array for (int i = 0; i < (firstCount - 1); ++i) { for (int j = 0; j < firstCount - 1 - i; ++j) { if (firstArray[j] > firstArray[j + 1]) { temp = firstArray[j + 1]; firstArray[j + 1] = firstArray[j]; firstArray[j] = temp; } } } // Sorts second array for (int i = 0; i < (secondCount - 1); ++i) { for (int j = 0; j < secondCount - 1 - i; ++j) { if (secondArray[j] > secondArray[j + 1]) { temp = secondArray[j + 1]; secondArray[j + 1] = secondArray[j]; secondArray[j] = temp; } } } } /************************************************** * * @brief - Combines the two arrays into one. * **************************************************/ int* combineArrays(int* firstArray, int firstCount, int* secondArray, int secondCount, int* combinedCount) { *combinedCount = firstCount + secondCount; int* combined = (int*)malloc((*combinedCount) * sizeof(int)); if (combined == NULL) { return NULL; } for (int i = 0; i < firstCount; i++) { combined[i] = firstArray[i]; } for (int j = 0; j < secondCount; j++) { combined[firstCount + j] = secondArray[j]; } return combined; }