 # Define largest, medium, smallest values

What is a better/shorter way to define largest, medium, smallest values?

thank you

int X = 150;
int Y = 100;
int Z = 160;
int exaggeratedX, exaggeratedY, exaggeratedZ;

void setup(){
Serial.begin(9600);
finalise_values();
}

void loop(){}

void finalise_values(){

// check and define X value
if (X<=Y && X<=Z){
exaggeratedX = X/3; // X is the smallest value
Serial.print("X is smallest. X/3 = ");
Serial.println(exaggeratedX);
}
else if (X>Y && X>Z) {
exaggeratedX = X1.5; // X is the largest value
Serial.print("X is largest. X
1.5 = ");
Serial.println(exaggeratedX);
}
else {
exaggeratedX = X; // X is the medium value
Serial.print("X is meduim. X = ");
Serial.println(exaggeratedX);
}

// check and define Y value
if (Y<=X && Y<=Z){
exaggeratedY = Y/3; // Y is the smallest value
Serial.print("Y is smallest. Y/3 = ");
Serial.println(exaggeratedY);
}
else if (Y>X && Y>Z) {
exaggeratedY = Y1.5; // Y is the largest value
Serial.print("Y is largest. Y
1.5 = ");
Serial.println(exaggeratedY);
}
else {
exaggeratedY = Y; // X is the medium value
Serial.print("Y is meduim. Y = ");
Serial.println(exaggeratedY);
}

// check and define Z value
if (Z<=X && Z<=Y){
exaggeratedY = Z/3; // Z is the smallest value
Serial.print("Z is smallest. Z/3 = ");
Serial.println(exaggeratedZ);
}
else if (Z>X && Z>Y) {
exaggeratedZ = Z1.5; // Z is the largest value
Serial.print("Z is largest. Z
1.5 = ");
Serial.println(exaggeratedZ);
}
else{
exaggeratedZ = Z; // Z is the medium value
Serial.print("Z is meduim. Z = ");
Serial.println(exaggeratedZ);
}

}

``````int compare(const void *p1, const void *p2) {
return *(int *)p1 - *(int *)p2;
}

void finalise_values(){
int arr;
arr = X;
arr = Y;
arr = Z;
qsort(arr, 3, sizeof(arr), compare);
Serial.print("Smallest: "); Serial.println(arr);
Serial.print("Middle:"); Serial.println(arr);
Serial.print("Largest: "); Serial.println(arr);
}
``````

-- The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons

Rugged’s code is robust and scalable to any size.

If you just need to order 3 items at max 3 compares will do, however if you need more (than 4) items constructing a correct - if then else tree - becomes error-prone. So unless you’re in a real need for speed I suggest to use Rugged’s solution.

``````if (x < y)
{
if (z < x) Serial.print(" z, x, y");
else if (z < y) Serial.print("x, z ,y");
else Serial.print("x, y, z");
} else {
if (z < y) Serial.print("z, y, x");
else if (z < x) Serial.print("y, z ,x");
else Serial.print("y, x, z");
}
``````