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>

// Function prototypes

void computeUnion(int set1[], int set2[], int n1, int n2);

void computeIntersection(int set1[], int set2[], int n1, int n2);

int main() {

// Example sets (arrays)

int set1[] = {1, 2, 3, 4, 5};

int set2[] = {3, 4, 5, 6, 7};

int n1 = sizeof(set1) / sizeof(set1[0]);

int n2 = sizeof(set2) / sizeof(set2[0]);

printf("Set 1: ");

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

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

}

printf("\n");

printf("Set 2: ");

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

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

}

printf("\n");

printf("Union of Set 1 and Set 2: ");

computeUnion(set1, set2, n1, n2);

printf("\nIntersection of Set 1 and Set 2: ");

computeIntersection(set1, set2, n1, n2);

printf("\n");

return 0;

}

// Function to compute the union of two sets

void computeUnion(int set1[], int set2[], int n1, int n2) {

int unionSet[n1 + n2];

int i = 0, j = 0, k = 0;

while (i < n1 && j < n2) {

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

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

} else if (set2[j] < set1[i]) {

unionSet[k++] = set2[j++];

} else {

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

j++;

}

}

while (i < n1) {

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

}

while (j < n2) {

unionSet[k++] = set2[j++];

}

for (int m = 0; m < k; m++) {

printf("%d ", unionSet[m]);

}

}

// Function to compute the intersection of two sets

void computeIntersection(int set1[], int set2[], int n1, int n2) {

int intersectionSet[n1 + n2];

int i = 0, j = 0, k = 0;

while (i < n1 && j < n2) {

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

i++;

} else if (set2[j] < set1[i]) {

j++;

} else {

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

j++;

}

}

for (int m = 0; m < k; m++) {

printf("%d ", intersectionSet[m]);

}

}

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.