0

Im having trouble coding the complement of a set. For example if Set A (x)= {1, 2, 3, 4} and Set B (y)= {5, 2, 3, 8} The complement should be x-y={1,4} or y-x={5,8}. What should I change in this code to achieve this?

for (i = 0; i < x; i++) {
    for (int k = 0; k < y; k++) {
        if (a[i] == b[k]) {
            flag = 1;
            break;
        } else {
            flag = 0;
        }
    }

    if (flag == 0) {
        c[z++] = a[i];
    }
}

System.out.print("Complement:");
System.out.print("[ ");
for (i = 0; i < x; i++) {

    System.out.print(a[i] + ",");
}
System.out.print(" ]");
azro
  • 53,056
  • 7
  • 34
  • 70
Dez Lo
  • 13
  • 2
  • You should indent your wode well. This time I'll do for you, but next time think about it, it's not readable like this – azro Dec 09 '18 at 21:09
  • Give definition of : `a, b, c, i, x, y, z, flag` please – azro Dec 09 '18 at 21:09

1 Answers1

0

You should print the result c[i] instead of a[i].

for (int i = 0; i < z; i++) {
  System.out.print(a[i] + ",");
}
Donat
  • 4,157
  • 3
  • 11
  • 26