Function on a double array

Hi,
I really apologize for this basic question :roll_eyes: but didn't managed to understand it yet (despite reading lots of stuff .... but never managed to get it work properly)

I would like to get a function that return the minimum value of a specific column a 2D array in float format (we are talking about temperatures.... )



const byte Arrayl = 24 ; // nombre de row  ...
const byte ArrayC = 2; // nombre de column
float tempHistory[Arrayl][ArrayC] ;

void setup() {


  Serial.begin(9600);

//fill with zero
  for (byte i = 0; i < Arrayl; i++) {
    for (byte j = 0; j < ArrayC; j++) {
      tempHistory[i][j] = 0;

}

void loop() {


// ........acquire data and put it in the array! this part is working  no problem //


//call to the min function:

  MinDataTemp(*tempHistory, Arrayl, ArrayC); 

}


void MinDataTemp( float* arr, byte row, byte col ) {
  
   float minimum= arr[0][1];
  for (float i = 0; i < row; i++)
  {

   if (arr[i][col] < minimum)
   {
  minimum = arr[i][1];
   }
    Serial.println (minimum);
   //   return minimum;
  }
}

I got the following error: error: invalid types 'float*[float]' for array subscript
minimum = arr[i][1];

I know it is trivial but I just don't get it. :hot_face:

Thx for your help

for (float i = 0; i < row; i++)

Array indices must be integers so why are you using a float ?

Hi, thx for the spotting!
I tried so many things that I forgot to change it back.....
Anyway I still have the following error:

: invalid types 'float[int]' for array subscript
minimum = arr[i][1];

Post your latest code. It sounds like you didn't fix the issue reported.

Also, your code as posted is missing several closing braces in setup.

you are reading out of range. array indexing is from 0, last index therefore is col-1

you are right! here you are:


const byte Arrayl = 24 ; // nombre de row  ...
const byte ArrayC = 2; // nombre de column
float tempHistory[Arrayl][ArrayC] ;

void setup() {


  Serial.begin(9600);

//fill the array with zero

  for (byte i = 0; i < Arrayl; i++) {
    for (byte j = 0; j < ArrayC; j++) {
      tempHistory[i][j] = 0;
      //        Serial.print(tempHistory[i][j]);
      //      Serial.print(";");
    }
    //     Serial.println();
  }

}

void loop() {


// ........acquire data and put it in the array! this part is working  no problem //


//call to the min function:

  MinDataTemp(*tempHistory, Arrayl, ArrayC); 

}


void MinDataTemp( float* arr, byte row, byte col ) {
  
   float minimum= arr[0][1];
  for (int i = 0; i < row; i++)
  {

   if (arr[i][col] < minimum)
   {
  minimum = arr[i][1];
   }
    Serial.println (minimum);
   //   return minimum;
  }
}

You're trying to pass a 2D array as a float *. The compiler needs to know the size of the second dimension of the array.

thx for your reply, but it is obscure for me (I m a noob in programming especially in passing array to function :wink:
can you please suggest the modification you have in mind?

void MinDataTemp( float arr[][ArrayC], byte row, byte col )

I still get errors ....



const byte Arrayl = 24 ; // nombre de row  ...
const byte ArrayC = 2; // nombre de column
float tempHistory[Arrayl][ArrayC] ;

void setup() {


  Serial.begin(9600);

//fill the array with zero

  for (byte i = 0; i < Arrayl; i++) {
    for (byte j = 0; j < ArrayC; j++) {
      tempHistory[i][j] = 0;
      //        Serial.print(tempHistory[i][j]);
      //      Serial.print(";");
    }
    //     Serial.println();
  }

}

void loop() {


// ........acquire data and put it in the array! this part is working  no problem //


//call to the min function:

  MinDataTemp(*tempHistory, Arrayl, ArrayC); 

}


void MinDataTemp( float* arr[][ArrayC], byte row, byte col ) {
  
   float minimum= arr[0][1];
  for (int i = 0; i < row; i++)
  {

   if (arr[i][col] < minimum)
   {
  minimum = arr[i][1];
   }
    Serial.println (minimum);
   //   return minimum;
  }
}

EDIT:

I did change MinDataTemp(*tempHistory, Arrayl, ArrayC); to MinDataTemp(tempHistory, Arrayl, ArrayC); `

and it was compiling .... `I still need to check if the function is working properly

Compare what I gave you to what's in your code now.

After you fix that you should have one more issue to figure out.

Yep, that is my EDIT in my post above I guess

Why pass a global array to a function in the first place ?

1 Like

ok it is working, thx for the help!
I still not really understand all those stuff but at least I have something running :+1:

Do you suggest an alternative method? (I m looking for the less consuming one in terme of memory .. running on uno)

Exactly.. If the array is global and your MinDataTemp function is only used with this array, why not use the array directly, like you do in setup() ?

right ...., thx for the spotting!

My thinking was to package it into a function .. more convenient to call, but will have a look it a direct call is less memory consuming.....

But at least I learned how to call a double array in a function! :slight_smile:

You can still put the code in a function, but if you are only ever going to us a single global array in that function then there is no need to pass it to the function with its attendant complications

Question: does a global array not need the init-statement with a '&' in a range based for-loop ?

Flavour One (see also this sketch in Wokwi simulation):

const byte Arrayl = 24 ; // nombre de row  ...
const byte ArrayC = 2;   // nombre de column

float tempHistory[Arrayl][ArrayC] =
{
  { 100.0 , 300000.0 },
  { 200.0 , 500000.0 },
  { 300.0 , 600000.0 },
  { 400.0 , 900000.0 },
  { 500.0 , 300000.0 },
  { 600.0 , 200000.0 },
  { 700.0 , 800000.0 },
  { 800.0 , 900000.0 },
  { 900.0 , 400000.0 },
  { 1000.0, 500000.0 },
  { 1100.0, 123456.7 },   // <--
  { 1200.0, 300000.0 },
  { 1300.0, 800000.0 },
  { 1400.0, 700000.0 },
  { 1500.0, 700000.0 },
  { 1600.0, 900000.0 },
  { 1700.0, 800000.0 },
  {   18.3, 200000.0 },   // <--
  { 1900.0, 400000.0 },
  { 2000.0, 700000.0 },
  { 2100.0, 300000.0 },
  { 2200.0, 600000.0 },
  { 2300.0, 500000.0 },
  { 2400.0, 500000.0 },
};

void setup() 
{
  Serial.begin(9600);
  Serial.println( "The sketch has started");
}

void loop()
{
  float minimumFirst = MinDataTemp(tempHistory, Arrayl, 0);   // the last parameter is the selected column
  Serial.print( minimumFirst);
  float minimumSecond = MinDataTemp(tempHistory, Arrayl, 1);   // the last parameter is the selected column
  Serial.print( ", ");
  Serial.print( minimumSecond);
  Serial.println();

  delay( 3000);
}

float MinDataTemp( float arr[][ArrayC], int rows, int selectedColumn)
{
  float minimum = arr[0][selectedColumn];
  
  for( int i=1; i<rows; i++)           // assuming that row 1 exists
  {
    if( arr[i][selectedColumn] < minimum)
    {
      minimum = arr[i][selectedColumn];
    }
  }
  return( minimum);
}

Flavour Two (see also this sketch in Wokwi simulation):

const byte Arrayl = 24 ; // nombre de row  ...
const byte ArrayC = 2;   // nombre de column

float tempHistory[Arrayl][ArrayC] =
{
  { 100.0 , 300000.0 },
  { 200.0 , 500000.0 },
  { 300.0 , 600000.0 },
  { 400.0 , 900000.0 },
  { 500.0 , 300000.0 },
  { 600.0 , 200000.0 },
  { 700.0 , 800000.0 },
  { 800.0 , 900000.0 },
  { 900.0 , 400000.0 },
  { 1000.0, 500000.0 },
  { 1100.0, 123456.7 },   // <--
  { 1200.0, 300000.0 },
  { 1300.0, 800000.0 },
  { 1400.0, 700000.0 },
  { 1500.0, 700000.0 },
  { 1600.0, 900000.0 },
  { 1700.0, 800000.0 },
  {   18.3, 200000.0 },   // <--
  { 1900.0, 400000.0 },
  { 2000.0, 700000.0 },
  { 2100.0, 300000.0 },
  { 2200.0, 600000.0 },
  { 2300.0, 500000.0 },
  { 2400.0, 500000.0 },
};

void setup() 
{
  Serial.begin(9600);
  Serial.println( "The sketch has started");
}

void loop()
{
  float minimumFirst = MinDataTemp( 0);   // the parameter is the selected column
  Serial.print( minimumFirst, 1);
  float minimumSecond = MinDataTemp( 1);   // the parameter is the selected column
  Serial.print( ", ");
  Serial.print( minimumSecond, 1);
  Serial.println();

  delay( 5000);
}

float MinDataTemp( int selectedColumn)
{
  float minimum = tempHistory[0][selectedColumn];
  
  for( auto &a:tempHistory)
  {
    if( a[selectedColumn] < minimum)
    {
      minimum = a[selectedColumn];
    }
  }
  return( minimum);
}