1234
1234

Reputation: 3

How to find Cartesian Product of two sets

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();
}

Upvotes: 0

Views: 2074

Answers (1)

suvojit_007
suvojit_007

Reputation: 1728

Just add printf("{%d, %d}, ", set1[i], set2[j]); to print cartesian product. You should use scanf instead of scanf_s Difference between scanf and scanf_s

printing the result in {} {} {}.. format

 case 2: //for cartesian product 

     /*
      A={1,2}  and B={3,4}
      Cartesian product of A and B is A x B = {{1,3},{1,4},{2,3},{2,4}}


       */

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

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

                        //print cartesian product of two set

                        printf("{%d, %d} ", set1[i], set2[j]); 

                    }
                }

Printing the result in the {{},{},...} format

 case 2: //for cartesian product 

            printf("{");

            for (i = 0; i < n1; i++)
            {
                for (j = 0; j < n2; j++)
                {

                   if(i==n1-1 && j==n2-1)
                            printf("{%d, %d} ", set1[i], set2[j]); 
                    else
                       printf("{%d, %d}, ", set1[i], set2[j]); 

                }
            }

            printf("}");

Upvotes: 1

Related Questions