smallest largest number

This is a discussion on smallest largest number within the C++ Programming forums, part of the General Programming Boards category; how do you find the smallest and largest number of 3 numbers. without using logical operators and else statements, only ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    164

    smallest largest number

    how do you find the smallest and largest number of 3 numbers.

    without using logical operators and else statements, only if and relational operators ? is there any other way to find it out without checking every number against each other using if statements ?

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    There's more than one way. If you have your numbers in something like an array or a list that can be iterated through, you can do this:
    Code:
    current_minimum = numbers[0]
    for(i = 1; i < number count; ++i)
       if(numbers[i] < current_minimum) current_minimum = numbers[i];
    That'll work for any number of numbers. Finding a maximum is very similar. Additionally, if you always have 3 numbers, and you have a function min() that returns the minimum of 2 numbers, this will also work, and may be simpler:
    Code:
    minimum_of_three = min( number1, min(number2, number3 ) )
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    vector<int> numbers;
    ...
    vector<int>::iterator it = min_element( numbers.begin(), numbers.end() );
    cout << "Min # = " << *it << endl;
    it = max_element( numbers.begin(), numbers.end() );
    cout << "Max # = " << *it << endl;
    or you could sort the numbers and just take the first and last numbers...

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Since you're probably not allowed to do any of those methods (as it sounds like this is a contrived test/assignment problem), you can look at them and get a hint for a simpler way to do it.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    or you could sort the numbers and just take the first and last numbers...
    That is inefficient though, so I would go with the min_element and max_element solution instead, unless I wanted to get both of them in a single pass.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    you could sort the numbers and just take the first and last numbers...
    A sort is O(nlogn) at best. In this case, just running through the array (O(n)) will do.

  7. #7
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Quote Originally Posted by cyberfish View Post
    A sort is O(nlogn) at best
    Bucket sort is O(n)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    True. I was thinking about comparison sorts.

    But still, why not just run through the array?

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,629
    The difference between O(n) and O(nlogn) is relatively small when n is 3

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,268
    The difference between O(n) and O(nlogn) is relatively small when n is 3
    That's true... and in retrospect there is probably no array or other container, since Daved is probably right and the 3 numbers really are in three separate variables.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    That is inefficient though, so I would go with the min_element and max_element solution instead, unless I wanted to get both of them in a single pass.
    Yeah, that's why I only mentioned it in passing but gave examples for min_element() & max_element().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. finding the largest number in a binary search
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 07-31-2008, 04:19 AM
  2. scanf oddities
    By robwhit in forum C Programming
    Replies: 5
    Last Post: 09-22-2007, 02:03 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  4. Find the Largest and Smallest Number
    By Nightsky in forum C Programming
    Replies: 27
    Last Post: 09-04-2006, 04:40 PM
  5. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM

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