Find Smallest Value in Array C Arduino Uno

Posted on

Smallest Number in C

How to Find Smallest Value in Array Arduino? – In Arduino programming, maybe we will come across a condition where we have to get the smallest number from the existing number.

In this article we will learn how to get the smallest value from the value stored in an array.

For example, there are values 34, 32, 4, 67, 87 and 12. The Arduino IDE will display the smallest value which is 4.

Find Smallest Value in Array C Arduino Uno

Basic Code

int values[] = {34, 32, 4, 67, 87, 12};
int lowest = 0;
int a;

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

  lowest = values[0];


  for (a = 0; a < 6;  a++)
  {
    if (values[a] < lowest) 
    {
      lowest = values[a];
    }
  }

  Serial.println(lowest);
}

void loop() {

}

Output: 4

How it work?

The code above has 6 known arrays. The array is read one by one starting from the 0 to the 5th address.

Initially, the lowest value is set to equal the 0th array value, then compared to the 1st array value.

If the lowest value is smaller than 1st array, then continue compare it with 2nd array.

If lowest value is greater than 2nd array, 2nd array value is entered as lowest value. etc.

I hope this Find Smallest Value in Array Arduino article is useful.

May be you like:
If you wanto to find largest number, please use basic code here.