Go Down

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

#### damith14

##### Nov 16, 2015, 08:32 am
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 ..

#### HazardsMind

#1
##### Nov 16, 2015, 08:58 am
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

#2
##### Nov 16, 2015, 08:59 am
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 offfor (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

#3
##### Nov 16, 2015, 09:21 am
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

#4
##### Nov 16, 2015, 02:29 pm
Sweep through the list once to find the lowest value, sweep though a second time to find which values equal it. O(n).

#5
THANK YOU GUYS
problem solved

Go Up