Title: **Compare 4 int values and find the lowest value ?**

Post by:**damith14** on **Nov 16, 2015, 08:32 am**

Post by:

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

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

lowest value=3,

lowest variables are val2 and val4

any help ..

thanks in advance

Title: **Re: Compare 4 int values and find the lowest value ?**

Post by:**HazardsMind** on **Nov 16, 2015, 08:58 am**

Post by:

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.

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.

Title: **Re: Compare 4 int values and find the lowest value ?**

Post by:**Robin2** on **Nov 16, 2015, 08:59 am**

Post by:

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

...R

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

Title: **Re: Compare 4 int values and find the lowest value ?**

Post by:**Whandall** on **Nov 16, 2015, 09:21 am**

Post by:

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() {}

Title: **Re: Compare 4 int values and find the lowest value ?**

Post by:**PaulMurrayCbr** on **Nov 16, 2015, 02:29 pm**

Post by:

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

Title: **Re: Compare 4 int values and find the lowest value ?**

Post by:**damith14** on **Nov 16, 2015, 02:56 pm**

Post by:

THANK YOU GUYS

problem solved :D

problem solved :D