Header-C
Header-C

 

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:

  1. Function to add elements to the hash table.
  2. Function to compute the union of the two arrays.
  3. Function to compute the intersection of the two arrays.
  4. 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:

  1. Copy the code into a text file and save it with a .c extension, e.g., union_intersection.c.
  2. Open a terminal and navigate to the directory containing the file.
  3. Compile the program using the command: gcc union_intersection.c -o union_intersection.
  4. Run the program using the command: ./union_intersection.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)