Go Down

Topic: Compare 4 int values and find the lowest value ? (Read 3221 times) previous topic - next topic

damith14

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

HazardsMind

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. 

Robin2

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

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


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

Whandall

Why not use the min function?

Code: [Select]
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() {}
Ah, this is obviously some strange usage of the word 'safe' that I wasn't previously aware of. (D.Adams)

PaulMurrayCbr

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

damith14


Go Up