# Find average in an array (SPECIAL)

**URL:** <https://forum.arduino.cc/t/find-average-in-an-array-special/513597>\
**Category:** Programming\
**Created:** [March 12, 2018, 11:39am UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597 "2018-03-12T11:39:48Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 27, 2018, 1:15pm UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/21 "2018-03-27T13:15:50Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 27, 2018, 1:19pm UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/22 "2018-03-27T13:19:58Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 27, 2018, 1:26pm UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/23 "2018-03-27T13:26:56Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![AdrianCFL](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@AdrianCFL](https://forum.arduino.cc/u/AdrianCFL)\
**Post date:** [March 27, 2018, 2:31pm UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/24 "2018-03-27T14:31:44Z")

</div>

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}; &nbsp; &nbsp; &nbsp;// 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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// String to temporarily store data from UART  
int cueToOutput = 8; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Pin 8 indicates drone has landed  
int altInput = 4; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Pin 4 used for Altitude input from pixhawk  
float altIn; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// altIn stores current altitude of pixhawk  
int sonarInput = 5; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Pin 5 activated when signal received from sonar  
int rows = 0; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Variables for rows/columns of data array  
int columns = 2;  
float maxAlt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // maxAlt stores height of building_

 

_struct measure\_t  
{  
&nbsp; &nbsp;String serialNum;  
&nbsp; &nbsp;float alt;  
};  
//struct measure\_t MEASURES \*\*measures;  
//struct measure\_t measures = new MEASURES[rows][columns];  
struct measure\_t measures[10][2];_

 

_void setup()  
{  
&nbsp;pinMode(cueToOutput, INPUT\_PULLUP);  
&nbsp;pinMode(altInput, INPUT);  
&nbsp;pinMode(sonarInput, INPUT\_PULLUP);  
&nbsp;Serial.begin(57600);  
&nbsp;Serial.println("Start");  
}_

 

_void loop()  
{_

 

_altIn = analogRead(altInput);_

 

_while (Serial.available())  
&nbsp;{ &nbsp;  
&nbsp; &nbsp;char UART;  
&nbsp; &nbsp;UART = Serial.read();  
&nbsp; &nbsp;dataIn += UART;_

 

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

 

_}  
&nbsp; &nbsp;  
&nbsp;}_

 

_if (digitalRead(sonarInput) == 0)  
&nbsp;{  
&nbsp; &nbsp;maxAlt = altIn;  
&nbsp;}_

 

_if (digitalRead(cueToOutput) == 0)  
&nbsp;{  
&nbsp;const uint16\_t nbMeasures = sizeof(measures) / sizeof(measures[0]);_

 

_float &nbsp;measureSum[maxItems] &nbsp;= {0L};  
&nbsp;float &nbsp;measureCount[maxItems] = {0};_

 

_for (uint16\_t i = 0; i \< nbMeasures; i++)  
&nbsp;{  
&nbsp; &nbsp;measureSum[measures[i].serialNum] += measures[i].alt;  
&nbsp; &nbsp;measureCount[measures[i].serialNum]++;  
&nbsp;}_

 

_for (byte i = 0; i \< maxItems; i++)  
&nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp;  
&nbsp; &nbsp;if (measureCount[i] != 0)  
&nbsp; &nbsp;{_

 

_float avgAltPerItem = {measureSum[i] / measureCount[i]};  
&nbsp; &nbsp; &nbsp;float estFloorPerItem = {(avgAltPerItem / maxAlt)\*numOfFloor};_

 

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

 

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

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 27, 2018, 2:37pm UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/25 "2018-03-27T14:37:16Z")

</div>

```cpp
struct measure_t measures[10][2];

```

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

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

```

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

```cpp
    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.

---

<div class="post-metadata">

**Author:** ![AdrianCFL](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@AdrianCFL](https://forum.arduino.cc/u/AdrianCFL)\
**Post date:** [March 28, 2018, 7:37am UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/26 "2018-03-28T07:37:49Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 28, 2018, 7:57am UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/27 "2018-03-28T07:57:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![AdrianCFL](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@AdrianCFL](https://forum.arduino.cc/u/AdrianCFL)\
**Post date:** [March 28, 2018, 8:01am UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/28 "2018-03-28T08:01:05Z")

</div>

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

Thanks UKHeliBob

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 28, 2018, 8:12am UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/29 "2018-03-28T08:12:31Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![AdrianCFL](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@AdrianCFL](https://forum.arduino.cc/u/AdrianCFL)\
**Post date:** [March 28, 2018, 12:54pm UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/30 "2018-03-28T12:54:23Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [March 28, 2018, 1:40pm UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/31 "2018-03-28T13:40:30Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![AdrianCFL](https://avatars.discourse-cdn.com/v4/letter/a/e56c9b/32.png) [@AdrianCFL](https://forum.arduino.cc/u/AdrianCFL)\
**Post date:** [March 29, 2018, 4:06am UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/32 "2018-03-29T04:06:10Z")

</div>

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](https://forum.arduino.cc/index.php?topic=538022)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 11:03pm UTC](https://forum.arduino.cc/t/find-average-in-an-array-special/513597/33 "2021-05-05T23:03:59Z")

</div>

Closed on May 5, 2021, 11:03pm UTC



[Previous page](https://forum.arduino.cc/t/find-average-in-an-array-special/513597.md?page=1)
