Thread: easy way to check ordering of numbers?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    46

    easy way to check ordering of numbers?

    I want to check the ordering of numbers in an if statement, for example, in pseudo-code:
    Code:
    if a < b < c < ... < z
    then do something magical
    else quit
    Is there an easy way to do this? Obviously doing all the boolean comparisons is not possible. Maybe sorting the numbers and then doing the respective comparisons is the best way?

  2. #2
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Yes, a sort() will accomplish a sort. But what do you mean by "order"? Like as in a polynomial?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Oh sorry, by 'order' I meant just numerical order. For example
    Code:
    3 < 4 < 5 < 6
    should be true and
    Code:
     3 < 5 < 4 < 6
    should be false

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    I would imagine the easiest way is to place these numbers in a list or vector, and traverse it checking if prev < curr.

    If you make it to the end of the list, the order is correct.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  5. #5
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    sorry I must have had a brain fart ...
    Code:
    a < b && b < c && c < d
    etc...


    my post probably wasn't clear, I was just looking for that

    .... I should probably take a break
    Last edited by Marksman; 02-27-2008 at 10:19 PM.

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by Marksman View Post
    sorry I must have had a brain fart ...
    Code:
    a < b && b < c && c < d
    etc...
    Well sure that will work for just four numbers, but what if you have 50 numbers? Will you write out all those comparison tests?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  7. #7
    Registered User
    Join Date
    Feb 2005
    Posts
    46
    Yeah that's true. In my actual program I only had 4 variables, so it was manageable.
    Otherwise the vector/list approach would definitely be better.

  8. #8
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    How are the numbers stored? In a list/array/vector of some kind? You can potentially keep track if the values are ordered as they are entered and simply have a flag (initialized to true for the empty container). Whenever you add a new number, simply check if it is more than the previous value - if not then set the flag to false meaning the list is not ordered. When you do your if test, then just check this flag (an O(1) operation).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to put a check on an extern variable set as flag
    By rebelsoul in forum C Programming
    Replies: 18
    Last Post: 05-25-2009, 03:13 AM
  2. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  3. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 11:56 PM
  4. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 06:44 PM
  5. Replies: 22
    Last Post: 11-08-2001, 11:01 PM