Challenge

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

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

Solution: The goal of the project is to create a program that takes two lists of integers as input and computes their union and intersection. The union of two sets A and B is the set of all elements that are in A or B or both, while the intersection of two sets A and B is the set of all elements that are in both A and B.

Code Used For the Project

#include

#include

#define MAX_SIZE 100

The stdio.h library is included for input and output operations, while the stdlib.h library is included for dynamic memory allocation. The MAX_SIZE constant is defined to limit the maximum size of the input lists.

int main() {
int list1[MAX_SIZE], list2[MAX_SIZE], union_list[MAX_SIZE], intersection[MAX_SIZE];
int n1, n2, i, j, k, found, count;

Four integer arrays are defined: list1, list2, union_list, and intersection. list1 and list2 will hold the user input, while union_list and intersection will hold the results of the computations. size1 and size2 are integers used to store the number of elements in list1 and list2, respectively. i and j are loop counters, while count is used to keep track of the number of elements in the union_list and intersection arrays. duplicate is a boolean flag used to indicate whether a duplicate element has been found in the intersection computation.

// Read in list1
printf("Enter the number of elements in list1 (up to %d): ", MAX_SIZE);

fflush(stdout);

scanf("%d", &n1);
printf("Enter the elements of list1 (separated by spaces): ");

fflush(stdout);
for (i = 0; i < n1; i++) {
scanf("%d", &list1[i]);
}

// Read in list2
printf("Enter the number of elements in list2 (up to %d): ", MAX_SIZE);

fflush(stdout);
scanf("%d", &n2);
printf("Enter the elements of list2 (separated by spaces): ");

fflush(stdout);
for (i = 0; i < n2; i++) {
scanf("%d", &list2[i]);
}

// Initialize union_list and intersection arrays
for (i = 0; i < MAX_SIZE; i++) {
union_list[i] = -1; // Set all values in union_list to -1
intersection[i] = -1; // Set all values in intersection to -1
}

// Compute the union of list1 and list2
count = n1; // Start with count = number of elements in list1
for (i = 0; i < n2; i++) { // For each element in list2
found = 0; // Initialize found flag to false
for (j = 0; j < n1; j++) { // Check if element exists in list1
if (list2[i] == list1[j]) {
found = 1; // Set found flag to true if element is found in list1
break; // Stop searching list1 once the element is found
}
}
if (!found) { // If element is not found in list1, add it to union_list
union_list[count] = list2[i];
count++; // Increment count to reflect addition of new element
}
}
// Add remaining elements of list1 to union_list
for (i = 0; i < n1; i++) {
union_list[count] = list1[i];
count++; // Increment count to reflect addition of new element
}

// Compute the intersection of list1 and list2
count = 0; // Initialize count to 0
for (i = 0; i < n1; i++) { // For each element in list1
for (j = 0; j < n2; j++) { // Check if element exists in list2
if (list1[i] == list2[j]) {
found = 0; // Initialize found flag to false
for (k = 0; k < count; k++) { // Check if element has already been added to intersection
if (intersection[k] == list1[i]) {
found = 1; // Set found flag to true if element has already been added
break; // Stop searching intersection once the element is found
}
}
if (!found) { // If element is not already in intersection, add it
intersection[count] = list1[i];
count++; // Increment count to reflect addition of new element
}
break; // Stop searching list2 once the element is found

}
}
}

// Print the results
printf("The union of the two lists is: ");

fflush(stdout);
for (i = 0; i < MAX_SIZE; i++) {
if (union_list[i] != -1) {
printf("%d ", union_list[i]);

fflush(stdout);
}
}
printf("\n");

printf("The intersection of the two lists is: ");

fflush(stdout);
for (i = 0; i < MAX_SIZE; i++) {
if (intersection[i] != -1) {
printf("%d ", intersection[i]);

fflush(stdout);
}
}
printf("\n");

fflush(stdout);

return 0;
}

The program works as follows:

  1. Here user is prompted to enter the number of elements in list1 and list2, up to a maximum of MAX_SIZE (which is set to 100 in the code).

  2. User is then prompted to enter the elements of list1 and list2, separated by spaces. The elements are read into two arrays, list1 and list2, respectively.

  3. Two additional arrays, union_list and intersection, are initialized to hold the results of the computations.

  4. The program then computes the union of list1 and list2. To do this, it first sets a count variable to the number of elements in list1. It then loops through each element in list2, checking if it already exists in list1. If it does not, it adds it to the end of the union_list array and increments the count variable to reflect the addition of a new element. Finally, the program adds any remaining elements from list1 to the union_list array.

  5. The program then computes the intersection of list1 and list2. To do this, it loops through each element in list1 and checks if it exists in list2. If it does, the program checks if the element has already been added to the intersection array (to avoid adding duplicates). If it has not, it adds the element to the end of the intersection array and increments the count variable to reflect the addition of a new element.

  6. Finally, the program prints the results of the computations to the console. The union_list and intersection arrays are printed, with any -1 values (which were used to initialize the arrays) ignored.

The logic for computing the union and intersection of two sets is a fundamental concept in mathematics, and can be applied in many different contexts. The program demonstrates how this concept can be implemented in a computer program, using basic loops and array manipulation.

Output For the program

The input for this program consists of two lists of integers: list1 with 3 elements {1, 2, 3} and list2 with 4 elements {2, 3, 4, 5}.

The program prompts the user to enter the number of elements in list1 and list2, and then asks the user to enter the elements of each list separated by spaces.

After receiving the input, the program performs the union and intersection operations on the two lists using a for loop and if statements.

For the union operation, the program first copies all the elements of list1 to the result array. Then it checks each element of list2 against the elements already in the result array. If the element is not already in the result array, it adds it to the end of the result array.

For the intersection operation, the program checks each element of list1 against the elements of list2. If an element of list1 is also in list2 and is not already in the intersection array, it is added to the intersection array.

Finally, the program prints out the result of the union and intersection operations.

In this specific case, the union of list1 and list2 is {4, 5, 1, 2, 3}, and the intersection is {2, 3}.

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.