Hello everyone,
I just want to compare multiple numbers and print (or do anything else) biggest difference between them.
EXAMPLE:
numbers: 1, 2, 3, 4
output: biggest difference is 3
Thanks in advance for help...
Hello everyone,
I just want to compare multiple numbers and print (or do anything else) biggest difference between them.
EXAMPLE:
numbers: 1, 2, 3, 4
output: biggest difference is 3
Thanks in advance for help...
Post your best attempt, using code tags as described in the "How to use this forum" post, and explain where you are having difficulty.
I know, i can use if and simply compare the numbers but it's too long
if (number1 > number2) {
compare1 = number1 - number2;
}
if (number2 < number1) {
compare1 = number2 - number1;
}
if (number1 > number3) {
compare2 = number1 - number3;
}
if (number1 < number3) {
compare2 = number3 - number1;
}
Serial.print(max(compare1, compare2));
but i want compare more numbers and the code would be too long, so I ask here, if it can't be easier ...
When you see variable names with numeric suffixes, start thinking arrays
Find the smallest and largest (or min/max) and then subtract. Is there a known maximum? i.e. The numbers are always less than 10, etc?
Are those numbers in an array? If they are in an array they are easy to read in a loop.
For example
int Smallest = 10; // Initialize with the highest possible value, or higher.
if(X < Smallest)
Smallest = X; // Update if smaller (the 1st value tested should always be smaller)