# Maximum of Floating point array

Hey, wonder if someone could help me with a programming question on performing a max function on a list of floating point numbers.

I'm receiving a list of 32 floating point numbers over serial and then parsing the list into its 32 components. Next I am grouping the lists on a log scale and would like to return the maximum number in each category (a category can contain up to 17 floating point numbers).

I was looking at tutorials for reading the maximum value in an array (eg Find the max or min value in an array (C++ programming tutorial) - YouTube) however this method only works with integers and I'm struggling to find something comparable for handling lists of floats.

I've attached the code I'm working with. At the bottom commented out is the section of the code I'm trying to write to find the maximum floating point number in each of LRA categories 3 to 5.

Best
Jess

What make you say that ?

Greater than and less than operators also work on floating point datatypes.

you should use an array instead of

``````
float floatFromPC1 = 0.0;
float floatFromPC2 = 0.0;
float floatFromPC3 = 0.0;
float floatFromPC4 = 0.0;
float floatFromPC5 = 0.0;
float floatFromPC6 = 0.0;
float floatFromPC7 = 0.0;
float floatFromPC8 = 0.0;
float floatFromPC9 = 0.0;
float floatFromPC10 = 0.0;
float floatFromPC11 = 0.0;
float floatFromPC12 = 0.0;
float floatFromPC13 = 0.0;
float floatFromPC14 = 0.0;
float floatFromPC15 = 0.0;
float floatFromPC16 = 0.0;
float floatFromPC17 = 0.0;
float floatFromPC18 = 0.0;
float floatFromPC19 = 0.0;
float floatFromPC20 = 0.0;
float floatFromPC21 = 0.0;
float floatFromPC22 = 0.0;
float floatFromPC23 = 0.0;
float floatFromPC24 = 0.0;
float floatFromPC25 = 0.0;
float floatFromPC26 = 0.0;
float floatFromPC27 = 0.0;
float floatFromPC28 = 0.0;
float floatFromPC29 = 0.0;
float floatFromPC30 = 0.0;
float floatFromPC31 = 0.0;
float floatFromPC32 = 0.0;
``````

The code gets much smaller when you use an array instead of 32 variables. Note: C++ arrays start with index 0 so everything is one off from your numbering that started at 1.

``````const byte numChars = 255;
char tempChars[numChars];        // temporary array for use when parsing

float floatsFromPC;

float LRA;
const int LRAStartRange = {0, 1, 3, 7, 15};
const int LRAEndRange   = {0, 2, 6, 14, 31};

boolean newData = false;

//============

void setup()
{
Serial.begin(115200);
Serial.println();
}

//============

void loop()
{
recvWithStartEndMarkers();
if (newData == true)
{
// this temporary copy is necessary to protect the original data
//   because strtok() used in parseData() replaces the commas with \0
parseData();
showParsedData();
newData = false;
}
}

//============

void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '<';
char endMarker = '>';
char rc;

while (Serial.available() > 0 && newData == false)
{

if (recvInProgress == true)
{
if (rc != endMarker)
{
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
recvInProgress = false;
ndx = 0;
newData = true;
}
}

else if (rc == startMarker)
{
recvInProgress = true;
}
}
}

//============

void parseData()        // split the data into its parts
{
char * strtokIndx; // this is used by strtok() as an index
strtokIndx = strtok(tempChars, " ");     // get the first part - the string
floatsFromPC = atof(strtokIndx);     // convert this part to a float

for (int i = 1; i < 32; i++)
{
strtokIndx = strtok(NULL, " "); // this continues where the previous call left off
floatsFromPC[i] = atof(strtokIndx);     // convert this part to a float
}
}

float maxInRange(int start, int end)
{
float max = floatsFromPC[start];
for (int i = start + 1; i <= end; i++)
{
if (floatsFromPC[i] > max)
max = floatsFromPC[i];
}
return max;
}

void showParsedData()
{
for (int i = 0; i < 32; i++)
{
//  Serial.print("Bin ");
//  Serial.print(i+1);
//  Serial.print(": "
Serial.print(floatsFromPC[i]);
}
Serial.println();

for (int i = 0; i < 5; i++)
{
LRA[i] = maxInRange(LRAStartRange[i], LRAEndRange[i]);
Serial.print("LRA");
Serial.print(i + 1);
Serial.print(' ');
Serial.println(LRA[i]);
}
}``````

Using an object that already stores floats and can tell you the max of its list..

``````#include <runningAvg.h>

runningAvg  floatList(17);    // An object that can store 17 floats.
char        readBuff;     // A buffer for our typings.
int         index;            // Where the enxt char goes.

void setup(void) {

Serial.begin(57600); // Fire up the serial stuff.
index = 0;           // Init the index.
}

void loop(void) {

if (Serial.available()) {                    // We got a char coming in?
readBuff[index+1] = '\0';                 // Stuff in an end of string marked after it.
if (readBuff[index]=='\n') {              // The last char was a newline?
Serial.print("Max value : ");          // Lets show the user-
Serial.println(floatList.getMax());    // the max value of the list so far.
index = 0;                             // And we reset the index to zero for the next string.
} else {                                  // Oh, we DIDN"T see an end of line?
index++;                               // Bump up the index for the next char.
}
}
}
``````

You will need to grab LC_baseTools from the IDE library manager to get this to compile.

Good luck!

-jim lee

great, thanks so much for your replies, really helpful. John I ran your code and it works perfectly and yes thanks all, much simpler to put the floats into an array

thanks again
Jess

The odd thing is that you said that you wanted to find the maximum of values in an array right from the start but they were never in an array at all

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.