Compare 4 int values and find the lowest value ?

i have 4 int values and they can be different to each other or they can be equal,i want to write a programme to find what are the lowest values and how much of them...

Eg:(val1=5,val2=3,val3=10,val4=3)

lowest value=3,
lowest variables are val2 and val4

any help ..

thanks in advance

Do you know how for loops work? Are you familiar with the function min().

If yes to both, then you should be able to solve your problem.
I would make two functions, one to find the minimum value and the other to look for how many of that value.

I'll give you a Karma point if you can combine both into one function that returns both values.

The simplest thing would be to put the values into an array and use a FOR loop to iterate over the array. Something like

lowVal = myArray[0];  // just to start it off
for (byte n = 0; n < 4; n++) {
   if (myArray[n] < lowVal) {
        lowVal = myArray[n];
   }
}

...R

Why not use the min function?

void setup() {
  int val1 = 5, val2 = 3, val3 = 10, val4 = 3;
  int minimum = min(min(val1, val2), min(val3, val4));
  Serial.begin(115200);
  Serial.print("Minimum is ");
  Serial.println(minimum);
}
void loop() {}
1 Like

Sweep through the list once to find the lowest value, sweep though a second time to find which values equal it. O(n).

THANK YOU GUYS
problem solved :smiley: