Challenge

Project 1 - Creation of user defined data type to implement the user interfaces for working with ‘Set’ (Mathematical Set theory) using Linked List

#include <stdio.h>

#define MAX_SIZE 100

// Function to perform union of two sets

void unionSets(int set1[], int size1, int set2[], int size2, int result[]) {

// Combine elements from both sets

int i, j, k;

for (i = 0; i < size1; i++) {

result[i] = set1[i];

}

k = size1;

for (i = 0; i < size2; i++) {

// Check if the element is already in the result set

int found = 0;

for (j = 0; j < size1; j++) {

if (set2[i] == set1[j]) {

found = 1;

break;

}

}

// If not found, add it to the result set

if (!found) {

result[k++] = set2[i];

}

}

}

// Function to perform intersection of two sets

void intersectSets(int set1[], int size1, int set2[], int size2, int result[]) {

int k = 0;

for (int i = 0; i < size1; i++) {

for (int j = 0; j < size2; j++) {

if (set1[i] == set2[j]) {

result[k++] = set1[i];

break;

}

}

}

}

// Function to display elements of a set

void displaySet(int set[], int size) {

printf("{ ");

for (int i = 0; i < size; i++) {

printf("%d ", set[i]);

}

printf("}\n");

}

int main() {

int set1[MAX_SIZE], set2[MAX_SIZE];

int size1, size2;

// Input set1

printf("Enter the size of set1: ");

scanf("%d", &size1);

printf("Enter elements of set1: ");

for (int i = 0; i < size1; i++) {

scanf("%d", &set1[i]);

}

// Input set2

printf("Enter the size of set2: ");

scanf("%d", &size2);

printf("Enter elements of set2: ");

for (int i = 0; i < size2; i++) {

scanf("%d", &set2[i]);

}

// Calculate and display the union

int unionResult[MAX_SIZE];

unionSets(set1, size1, set2, size2, unionResult);

printf("Union of the sets: ");

displaySet(unionResult, size1 + size2);

// Calculate and display the intersection

int intersectResult[MAX_SIZE];

intersectSets(set1, size1, set2, size2, intersectResult);

printf("Intersection of the sets: ");

displaySet(intersectResult, size1);

return 0;

}

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.