1. ## 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. 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 ) )`

3. 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. 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. 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.

6. 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. Originally Posted by cyberfish
A sort is O(nlogn) at best
Bucket sort is O(n)

8. True. I was thinking about comparison sorts.

But still, why not just run through the array?

9. The difference between O(n) and O(nlogn) is relatively small when n is 3

10. 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.

11. Originally Posted by laserlight
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().