Skip to content Skip to sidebar Skip to footer

Widget HTML #1

How to Find Smallest Value in Array C Arduino Uno

Find Smallest Value in C Arduino

How to Find Smallest Value in Array in C Arduino - In Arduino programming, maybe we will come across a condition where we have to get the smallest value from the existing values.

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.

How to Find Smallest Value in Array in C Arduino
How to Find Smallest Value



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 does the above program 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.

Post a Comment for "How to Find Smallest Value in Array C Arduino Uno"