Simple logic problem: Finding largest and smallest number among 3 numbers
Solution 1
Let's say you've got arbitrary numbers x, y, z
.
Pseudocode:
largest = x
smallest = x
if (y > largest) then largest = y
if (z > largest) then largest = z
if (y < smallest) then smallest = y
if (z < smallest) then smallest = z
This is one way to solve your problem if you're using only variables, assignment, if-else and comparison.
If you have arrays and a sort operation defined over it, you can use this:
array = [x, y, z]
arrays.sort()
largest = array[2]
smallest = array[0]
If you have a max
and min
function that takes an array of numbers as an argument, you can use this:
array = [x, y, z]
largest = max(array)
smallest = min(array)
If you also have positional assignment using sets, you can use this:
array = [x, y, z]
(largest, smallest) = (max(array), min(array))
If you have a data structure that will sort it's contents when inserting elements, you can use this:
array.insert([x, y, z])
smallest = array[0]
largest = array[2]
Solution 2
#include <stdio.h>
#include <algorithm>
using namespace std;
int main ( int argc, char **argv ) {
int a = 1;
int b = 2;
int c = 3;
printf ( "MAX = %d\n", max(a,max(b,c)));
printf ( "MIN = %d\n", min(a,min(b,c)));
return 0;
}
Solution 3
if (x < y) {
minimum = min(x,z)
maximum = max(y,z)
} else {
minimum = min(y,z)
maximum = max(x,z)
}
Solution 4
Something like this would be more general (in Java):
// All available values.
int[] values = new int[] { 1, 2, 3 };
// Initialise smallest and largest to the extremes
int smallest = Integer.MAX_VALUE;
int largest = Integer.MIN_VALUE;
// Compare every value with the previously discovered
// smallest and largest value
for (int value : values) {
// If the current value is smaller/larger than the previous
// smallest/largest value, update the reference
if (value < smallest) smallest = value;
if (value > largest) largest = value;
}
// Here smallest and largest will either hold the initial values
// or the smallest and largest value in the values array
Related videos on Youtube
newbie
I'm a newbie, a novice, an amateur, a beginner in programming. My Total Programming Experience is approximately equal to the no. of months I joined here! So pardon me for my idiotic questions. My dream is to be a great programmer someday. To make the impossible, possible. I want to be the female version of Mr. Jon Skeet and my idol (who always scolds me hehehe) Mr Balusc. But I think, based on my current capabilities, i need a whooping 50 years to be on their level. So help me God! BTW, i really find this site super cool! I could interact and learn from many great programmers around the world. And for a programmer-wannabe like me, every opinion matters. What's important is that I'm learning a lot here which I could not learn by just reading books. So thank you everyone for all the help. I think I am becoming a stackoverflow addict.
Updated on January 19, 2020Comments
-
newbie over 4 years
I am creating a pseudocode in determining the smallest and largest number among 3 numbers:
My code is as follows:
If (x >= y) largest = x Smallest = y Else largest = y Smallest =x If (z >= largest) Largest = z If (z <= smallest) Smallest = z
Do you think this is correct? or are there better ways to solve this?
-
abeln about 13 years@Lukas: I'd say it's O(1) either way :)
-
abeln about 13 years@Lukas: I see only 3 numbers and no "general" solution (if there are n numbers, array[2] won't give you the maximum). I get your point, though.
-
darioo about 13 years@Lukas: I didn't specify what algorithm
sort
uses ;-) -
Lukas Eder about 13 years@darioo, if your algorithm is faster than
O(n log n)
then I congratulate to your nobel prize ;-) -
darioo about 13 years@Lukas: no, it's not; I will leave that discovery to greater minds of our time. If I go and implement a sort algorithm, it will certainly be
O(n log n)
or (more probably) slower than that ;-)