Andy - 3 months ago 12

C Question

I have an array with x numbers: sets[ ](long numbers) and a char array operations[ ] with x-1 numbers. For each number from sets[ ], its binary form(in 64bits) would be the same as a set of numbers( these numbers being from 0 to 63 ), 1's and 0's representing whether it is inside a subset or not ( 1 2 4 would be 1 1 0 1, since 3 is missing)

ex: decimal 5 --->000...00101 , meaning that this subset will only have those 2 last numbers inside it(#63 and #61)

now,using the chars i get in operations[], i should work with them and the binaries of these numbers as if they were operations on subsets(i hope subset is the right word), these operations being :

U = reunion ---> 101 U 010 = 111

A = intersection ---> 101 A 001 = 001

\ = A - B ---> 1110 - 0011 = 1100

/ = B-A ---> like the previous one

so basically I'd have to read numbers, make them binary, use them as if they were sets and use operations accordingly, then return the result of all these operations on them.

my code :

`include <iostream>`

using namespace std;

void makeBinaryVector(int vec[64], long xx)

{

// put xx in binary form in array "vec[]"

int k = 63;

long x = xx;

if(xx == 0)

for(int i=0;i<64;i++)

vec[i] = 0;

while(x != 0)

{

vec[k] = x % 2;

x = x / 2;

k--;

}

}

void OperationInA(int A[64], char op, int B[64])

{

int i;

if(op == 'U') //reunion

for(i=0;i<64;i++)

if(B[i] == 1)

A[i] = 1;

if(op == 'A') //intersection

for(i=0;i<64;i++)

{

if((B[i] == 1) && (A[i] == 1))

A[i] = 1;

else

A[i] = 0;

}

if(op == '\\') //A-B

for(i=0;i<64;i++)

{

if( (A[i] == 0 && B[i] == 0) || (A[i] == 0 && B[i] == 1) )

A[i] = 0;

else

if((A[i] == 1) && (B[i] == 1))

A[i] = 0;

else

if((A[i] == 1) && (B[i] == 0))

A[i] = 1;

}

if(op == '/') //B-A

for(i=0;i<64;i++)

{

if(B[i] == 0)

A[i] = 0;

else

if((B[i] == 1) && (A[i] == 0))

A[i] = 1;

else

if((B[i] == 1) && (A[i] == 1))

A[i] = 0;

}

}

unsigned long setOperations(long sets[], char operations[], unsigned int x)

{

unsigned int i = 1; //not 0, since i'll be reading the 1st number separately

unsigned int j = 0;

unsigned int n = x;

int t;

long a = sets[0];

int A[64];

for(t=0;t<64;t++)

A[t] = 0;

makeBinaryVector(A, a); //hold in A the first number, binary, and the results of operations

long b;

int B[64];

for(t=0;t<64;t++) //Hold the next number in B[], in binary form

B[t] = 0;

char op;

while(i < x && j < (x-1) )

{

b = sets[i];

makeBinaryVector(B, b);

op = operations[j];

OperationInA(A, op, B);

i++; j++;

}

//make array A a decimal number

unsigned int base = 1;

long nr = 0;

for(t=63; t>=0; t--)

{

nr = nr + A[t] * base;

base = base * 2;

}

return nr;

}

long sets[100];

char operations[100];

long n,i;

int main()

{

cin>>n;

for(i=0;i<n;i++)

cin>>sets[i];

for(i=0;i<n-1;i++)

cin>>operations[i];

cout<<setOperations(sets,operations,n);

return 0;

}

So everything seems fine, except when im trying this :

sets = {5, 2, 1}

operations = {'U' , '\'}

5 U 2 is 7(111), and 7 \ 1 is 6 (111 - 001 = 110 --> 6)

the result should be 6, however when i Input them like that the result is 4 (??)

however, if i simply input {7,1} and { \ } the result is 6,as it should be. but if i input them like i first mentioned {5,2,1} and {U,} then its gonna output 4.

I can't seem to understand or see what im doing wrong...

Answer

You don't have to "convert to binary numbers". There's no such thing as 'binary numbers'. You can just perform the operations on the variables.

For the *reunion*, you can use the bitwise *OR* operator '|', and for the intersection, you can use the bitwise *AND* operator '&'.

Something like this:

```
if (op == 'A')
result = a & b;
else if (op == 'U')
result = a | b;
else if (op == '\\')
result = a - b;
else if (op == '/')
result = b - a;
```