Find average in an array (SPECIAL)

For what I am trying to do, I would not know the size of the array at compilation time.

Fair enough.

That's why I'm trying to make it dynamic

There are only two ways to do that. Use new or malloc(). You can NOT just invent new syntax.

Keep in mind that, if you've dynamically defined an array that can hold 25 (pairs of) values, and you need a 26th, you'll need room in memory for both arrays, while you copy the data from one to the other, and then free() or delete the other one.

Pissing away memory using Strings isn't going to make dynamic allocation easy, given the way that String makes swiss-cheese of memory.

I would not know the size of the array at compilation time

If at some time during the running of the program the array may have n elements in it then you are going to need enough memory to hold n element. You might as well declare the array with n elements from the beginning.

UKHeliBob:
If at some time during the running of the program the array may have n elements in it then you are going to need enough memory to hold n element. You might as well declare the array with n elements from the beginning.

With dynamic allocation, if the array needs to hold n elements, you need already have an array holding n-1 elements. So, dynamic allocation requires enough memory to hold almost twice as many rows as static allocation.

If you have the room to handle dynamic allocation, you have way more than you need with static allocation. If you don't have room to handle dynamic allocation, you might still have room for static allocation, since you need just over half as much.

Alright, I think I'll use static array size as you advised.

However, now I get the error (request for member 'serialNum' in 'measures', which is of non-class type 'measure_t [2]') at line 80 - measureCount[measures*.serialNum]++;
_
```*_
*uint8_t numOfFloor = {12};      // Input number of floors
// Declare item1, item2, item3, etc. (Do Not Remove "maxItems")
enum {bikeObike1 , bikeObike2, bikeObike3, bikeObike4, maxItems};

// Please fill in information for above
// Code under this line is automated

String dataIn;                  // String to temporarily store data from UART
int cueToOutput = 8;            // Pin 8 indicates drone has landed
int altInput = 4;               // Pin 4 used for Altitude input from pixhawk
float altIn;                    // altIn stores current altitude of pixhawk
int sonarInput = 5;             // Pin 5 activated when signal received from sonar
int rows = 0;                   // Variables for rows/columns of data array
int columns = 2;
float maxAlt;                   // maxAlt stores height of building

struct measure_t
{
   String serialNum;
   float alt;
};
//struct measure_t MEASURES **measures;
//struct measure_t measures = new MEASURES[rows][columns];
struct measure_t measures[10][2];

void setup()
{
 pinMode(cueToOutput, INPUT_PULLUP);
 pinMode(altInput, INPUT);
 pinMode(sonarInput, INPUT_PULLUP);
 Serial.begin(57600);
 Serial.println("Start");
}

void loop()
{

altIn = analogRead(altInput);

while (Serial.available())
 {  
   char UART;
   UART = Serial.read();
   dataIn += UART;

//Process message only when new line character is received
   if (UART == '\n')
   {
//      Serial.print(dataIn);
//      measures = new MEASURES* [rows];
//      for(int i=0; i<rows; i++)
//        {measures[i] = new MEASURES [columns];}
     rows ++;
     measures[rows][1].serialNum = dataIn;
     measures[rows][2].alt = altIn;
     
     dataIn = "";            // Clear received buffer

}
   
 }

if (digitalRead(sonarInput) == 0)
 {
   maxAlt = altIn;
 }

if (digitalRead(cueToOutput) == 0)
 {
 const uint16_t nbMeasures = sizeof(measures) / sizeof(measures[0]);

float  measureSum[maxItems]  = {0L};
 float  measureCount[maxItems] = {0};

for (uint16_t i = 0; i < nbMeasures; i++)
 {
   measureSum[measures[i].serialNum] += measures[i].alt;
   measureCount[measures[i].serialNum]++;
 }

for (byte i = 0; i < maxItems; i++)
 {        
   if (measureCount[i] != 0)
   {

float avgAltPerItem = {measureSum[i] / measureCount[i]};
     float estFloorPerItem = {(avgAltPerItem / maxAlt)*numOfFloor};

Serial.print(F("Average Alt for Item"));
     Serial.print(i+1);
     Serial.print(F(" = "));
     Serial.println(avgAltPerItem);
     Serial.print(F("Estimated floor for Item"));
     Serial.print(i+1);
     Serial.print(F(" is "));
     Serial.println(estFloorPerItem);
     Serial.println();
   }
   else
   {
     Serial.print(F("No measure exists for Item"));
     Serial.println(i+1);
     Serial.println();
   }
 }
 Serial.print(F("Total height of building is "));
 Serial.print(maxAlt);
 Serial.println(F(" meters."));

while(1){}
}
}
_
*_ _*When I change this part to:*_ _*_
  for (uint16_t i = 0; i < nbMeasures; i++)
  {
    measureSum[measures[i][1].serialNum] += measures[i][2].alt;
    measureCount[measures[i][1].serialNum]++;
  }
_
```*_
I get this error instead: no match for 'operator[]' (operand types are 'float [4]' and 'String')

struct measure_t measures[10][2];

You have an array called measures. That array is a 2D array of structs.

      measures[rows][1].serialNum = dataIn;
      measures[rows][2].alt = altIn;

The second set of indices are 0 and 1, NOT 1 and 2.

    measureSum[measures[i].serialNum] += measures[i].alt;

You appear to have forgotten than measures is a 2D array. The serialNum field is a String. You can NOT use a String as an array index.

If I change it to use a float, this error occurs: invalid types 'float [4][float]' for array subscript
Both are float, I don't understand why is it invalid type

  for (uint16_t i = 0; i < nbMeasures; i++) 
  {
    measureSum[measures[i][1].alt] += measures[i][1].alt;
    measureCount[measures[i][1].alt]++;
  }

You can't use a float as an array index.

UKHeliBob:
You can't use a float as an array index.

Thanks UKHeliBob

AdrianCFL:
If I change it to use a float, this error occurs: invalid types 'float [4][float]' for array subscript
Both are float, I don't understand why is it invalid type

Because you can't address the πth element of an array.

TolpuddleSartre:
Because you can't address the πth element of an array.

Wow, thanks TolpuddleSartre! That's a solid answer to this! I've searched in so many forums, nobody gave this answer.

I've searched in so many forums, nobody gave this answer.

I am not surprised as it must be blindingly obvious that the array indices must be integers as the computer cannot store data in between memory locations.

If it helps, think of arrays as numbered pigeonholes each of which can hold a certain piece of data from which it can be read. The data in the array can be of any type, even user defined types, and the compiler will make the "pigeonhole" large enough to hold the data type specified but the pigeonhole numbers must be integers.

I see i see.. Thanks too UKHeliBob!

Anyway, I was supposed to update the forum on the final working code but realised I have encountered another problem, possibly regarding the array.
I have it in this thread:
https://forum.arduino.cc/index.php?topic=538022