comparing multiple variables..

This is a discussion on comparing multiple variables.. within the C Programming forums, part of the General Programming Boards category; I have an array of ints in my program and in main it goes through a huge series of iterations ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    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.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    The == operator returns 0 or 1.

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

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

    Get it?

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    4
    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) ?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by myrddin View Post
    If == returns a 0 or 1 isn't it just saying whether the comparison is true or false?
    Yes.

    Quote Originally Posted by myrddin View Post
    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 &&.

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    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?

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    Quote Originally Posted by ozumsafa View Post
    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 &&.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by ozumsafa View Post
    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.

  8. #8
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    Ahh , forgot What I told . Fatal error

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    If((a==b)&&(b==c)&&(a==c)) That would do the work I think. And using recursive of course.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,046
    Quote Originally Posted by ozumsafa View Post
    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.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why Multiple define error ...
    By nilathinesh in forum C Programming
    Replies: 2
    Last Post: 10-19-2006, 06:31 AM
  2. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  3. Remotely Creating Variables
    By Rajin in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2005, 11:20 PM
  4. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 03:46 PM
  5. Replies: 1
    Last Post: 05-01-2003, 02:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21