# comparing multiple variables..

• 10-16-2007
myrddin
comparing multiple variables..
I have an array of ints in my program and in main it goes through a huge series of iterations where the numbers in that array are constantly changed. I have a line in main that throws that array into a function and in that function I want to check if any 4 consecutive numbers are the same but the way Im doing it does not seem to be working.. heres the basics..

Code:

```int main(int argc, char *argv[]) { ..... if (consecutive(x)==1) ... true else ...false .... return 0 } int consecutive(int *) {   if (x[0]==x[1]==x[2]==x[3]  ||       x[1]==x[2]==x[3]==x[4]  ||       x[2]==x[3]==x[4]==x[5] )     return 1;   else return 0; }```
Can I not do it this way? Because even when there are 4 consecutive matching numbers it will still return a false.. but it doesn't seem to do so all the time.
• 10-16-2007
MacGyver
The == operator returns 0 or 1.

So (1==1) will return a 1.

2==2==2
1==2
0

Get it?
• 10-16-2007
myrddin
no I don't get it.

If == returns a 0 or 1 isn't it just saying whether the comparison is true or false?
so in a==b==c if they are all same number.. a==b returns a 1, b==c returns a 1 so why would 2 1's (true, true) return a 0 (false) ?
• 10-16-2007
MacGyver
Quote:

Originally Posted by myrddin
If == returns a 0 or 1 isn't it just saying whether the comparison is true or false?

Yes.

Quote:

Originally Posted by myrddin
so in a==b==c if they are all same number.. a==b returns a 1, b==c returns a 1 so why would 2 1's (true, true) return a 0 (false) ?

Because it's not comparing a to b and then b to c.

It's comparing something like a to b and then the result of that to c.

Get it now?

This is why you need to use &&.
• 10-16-2007
ozumsafa
I couldnt get why he or she needs to use &&. Because I would write it like this :

(a==b)==(b==c) ; and I would use recursive function in order to prevent from writing a lot of codes. Would that work , at least as an idea?
• 10-16-2007
King Mir
Quote:

Originally Posted by ozumsafa
I couldnt get why he or she needs to use &&. Because I would write it like this :

(a==b)==(b==c) ; and I would use recursive function in order to prevent from writing a lot of codes. Would that work , at least as an idea?

That would be the operation if A equals B NXOR B equals C.

If NXOR is the function you need, use == (with comments explaining what your doing). If you need AND, use &&.
• 10-16-2007
MacGyver
Quote:

Originally Posted by ozumsafa
I couldnt get why he or she needs to use &&. Because I would write it like this :

(a==b)==(b==c) ; and I would use recursive function in order to prevent from writing a lot of codes. Would that work , at least as an idea?

Uh... That will be considered true, if one of the following two conditions are met

1. a is equal to b and b is equal to c.
2. a is NOT equal to b and b is NOT equal to c.

It's wrong.
• 10-16-2007
ozumsafa
Ahh , forgot What I told . Fatal error :(
• 10-16-2007
ozumsafa
If((a==b)&&(b==c)&&(a==c)) That would do the work I think. And using recursive of course.
• 10-16-2007
King Mir
Quote:

Originally Posted by ozumsafa
If((a==b)&&(b==c)&&(a==c)) That would do the work I think. And using recursive of course.

Yes by by the transitive property (a==b)&&(b==c) implies a == c, so there is no point in that final condition.