Program Structure
This C program uses a hash table (implemented as an array) to store the elements of two input arrays. By leveraging the hash table, we can efficiently determine the union and intersection of the two arrays. The program consists of the following main parts:
- Function to add elements to the hash table.
- Function to compute the union of the two arrays.
- Function to compute the intersection of the two arrays.
- Main function to execute the program.
Documentation
/** * @brief Adds an element to the hash table. * * This function inserts an element into the hash table. * It marks the presence of the element using its value as the index. * * @param hashTable The hash table array. * @param element The element to be added. */ void addToHashTable(int hashTable[], int element); /** * @brief Computes the union of two arrays. * * This function calculates the union of two input arrays * by adding their elements to a hash table. * * @param arr1 First input array. * @param size1 Size of the first array. * @param arr2 Second input array. * @param size2 Size of the second array. */ void findUnion(int arr1[], int size1, int arr2[], int size2); /** * @brief Computes the intersection of two arrays. * * This function calculates the intersection of two input arrays * by checking which elements are present in both. * * @param arr1 First input array. * @param size1 Size of the first array. * @param arr2 Second input array. * @param size2 Size of the second array. */ void findIntersection(int arr1[], int size1, int arr2[], int size2);
Complete C Program
#include #define MAX_SIZE 1000 // Maximum size for the hash table // Function to add an element to the hash table void addToHashTable(int hashTable[], int element) { hashTable[element] = 1; // Mark element as present } // Function to compute the union of two arrays void findUnion(int arr1[], int size1, int arr2[], int size2) { int hashTable[MAX_SIZE] = {0}; // Hash table initialization // Add elements of the first array to the hash table for (int i = 0; i < size1; i++) { addToHashTable(hashTable, arr1[i]); } // Add elements of the second array to the hash table for (int i = 0; i < size2; i++) { addToHashTable(hashTable, arr2[i]); } // Displaying the union printf("Union: "); for (int i = 0; i < MAX_SIZE; i++) { if (hashTable[i] == 1) { printf("%d ", i); } } printf("\n"); } // Function to compute the intersection of two arrays void findIntersection(int arr1[], int size1, int arr2[], int size2) { int hashTable[MAX_SIZE] = {0}; // Hash table initialization // Add elements of the first array to the hash table for (int i = 0; i < size1; i++) { addToHashTable(hashTable, arr1[i]); } // Check for intersection with the second array printf("Intersection: "); for (int i = 0; i < size2; i++) { if (hashTable[arr2[i]] == 1) { printf("%d ", arr2[i]); hashTable[arr2[i]] = 0; // Avoid duplicate printing } } printf("\n"); } // Main function to execute the program int main() { int arr1[] = {1, 2, 3, 4, 5}; int arr2[] = {4, 5, 6, 7, 8}; int size1 = sizeof(arr1) / sizeof(arr1[0]); int size2 = sizeof(arr2) / sizeof(arr2[0]); findUnion(arr1, size1, arr2, size2); findIntersection(arr1, size1, arr2, size2); return 0; }
How to Run the Program
To run this program, follow these steps:
- Copy the code into a text file and save it with a .c extension, e.g., union_intersection.c.
- Open a terminal and navigate to the directory containing the file.
- Compile the program using the command: gcc union_intersection.c -o union_intersection.
- Run the program using the command: ./union_intersection.