I am trying to create a program which enables the user to calculate the difference and cartesian product of two given sets. I have been able to find the difference of sets.
I am having trouble writing the code for calculating the cartesian product of two sets i.e. Set 1 = 1,2
& Set 2 = 3,4
The program I have written so far to calculate the calculations for difference and half for cartesian product is as follows:
#include <stdio.h>
#include <conio.h>
main() {
int i, j, k, p, ch, n1, n2, set1[10], set2[10], set3[20], flag;
int wish;
printf("\n Enter the size of sets1 \n");
scanf_s("%d", &n1);
printf("\n Enter the element of set1 \n");
for (i = 0; i < n1; i++)
scanf_s("%d", &set1[i]);
printf("\n Enter the size of sets2 \n");
scanf_s("%d", &n2);
printf("\n Enter the elements of set2 \n");
for (i = 0; i < n2; i++)
scanf_s("%d", &set2[i]);
do {
printf("\n Menu for set operations");
printf("\n press 1 for DIFFERENCE");
printf("\n press 2 for CARTESIAN PRODUCT");
printf("\n Enter your Choice");
scanf_s("%d", &ch);
switch (ch) {
case 1://for difference
k = 0;
for (i = 0; i < n1; i++) {
flag = 1;
for (j = 0; j < n2; j++) {
if (set1[i] == set2[j]) {
flag = 0;
break;
}
}
if (flag == 1) {
set3[k] = set1[i];
k++;
}
}
p = k;
for (k = 0; k < p; k++) {
printf(" %d", set3[k]);
}
break;
case 2: //for cartesian product
k = 0;
for (i = 0; i < n1; i++) {
flag = 1;
for (j = 0; j < n2; j++) {
}
}
}
printf("\n Do you want to continue(0/1)? ");
scanf_s("%d", &wish);
} while (wish == 0);
getch();
}