Thread: Logical expressions

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    14

    Logical expressions

    I have to write logical expressons in C for the following..

    x is a negative integer which is greater than -2000
    x is a positive integer which is not divisble by 10
    x is an even number greater than 1000
    m is a legitimate month number
    The values in a, b and c are equal
    The values in a, b and c are not all equal


    I'm just wondering if i'm on the right track with these:

    (x<0) && (x>-2000)
    (x>0) && (x!%10)
    (x%2==0) && (x>1000)
    1 < m <12
    a==b==c
    ! (a==b==c)

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    x!%10 makes no sense. 1 < m < 12 makes sense, but not the kind of sense you want. Same with a==b==c. Remember that <, >, ==, and all the rest will always and forever evaluate to either 0 or 1.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1 is correct.
    2 is incorrect (bad syntax).
    3 is correct.
    4 is incorrect (will not give you what you want).
    5 is correct, although may be bad practice since it may not give you what you want every time, but works in this scenario. 6 is the same.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    Cheers for the replies guys. I've had another go, hopefully you might be able to have a look at these too:

    (x>0) && ((x%10)!==0)

    (m>1) && (m<12)

    (a==b) && (b==c)

    (a==b) || (b==c) || (a==c)

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The first one still has a syntax error
    (x>0) && ((x&#37;10)!==0)
    But perhaps it was a typo.

    The second one is a logical error. You're saying if m is BIGGER than 1 and m is LESS than 12. Last time I checked, 1 and 12 are valid months.

    3rd is good.

    I'm not sure if the 4th is what you want. You're testing equality instead of inequality.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    There ya go.

    The first one could have been (x>0) && !(x&#37;10) too.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    14
    Cheers for that again lads. I see where i went wrong

    Should have wrote != for 1st one,
    (m>=1) && (m<=12) for 2nd one.

    i'm hoping i'm right here for the last one

    (a!=b) || (b!=c) || (a!=c)

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    I'm not sure how the professor expected you to interpret "The values in a, b and c are not all equal"
    You solved it one way.

    Or he could be wanting
    Code:
    !((a==b) && (b==c))
    ... meaning, some could be equal, but not all at the same time. Basically the previous problem negated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical Operators in C++
    By Flecto in forum C++ Programming
    Replies: 4
    Last Post: 05-15-2009, 07:17 AM
  2. Stack that evaluates postfix expressions
    By killmequick in forum C Programming
    Replies: 7
    Last Post: 10-01-2008, 06:23 PM
  3. Regular expressions [Boost]
    By Desolation in forum C++ Programming
    Replies: 8
    Last Post: 12-30-2006, 10:10 PM
  4. Logical Error
    By gardenair in forum C Programming
    Replies: 2
    Last Post: 04-06-2003, 04:18 PM
  5. Size of 1 pixel
    By ooosawaddee3 in forum C++ Programming
    Replies: 4
    Last Post: 07-26-2002, 08:06 PM