How to find mean value of Arrays

Hi everyone,

Regarding the above subject, I have done some code to show you how to find the mean value of an array. Alas, the code has an array.

#include <Average.h>

double SUB2_cy1[]= {35.92, 35.73, 35.79, 36.15, 35.73, 35.91, 36.14, 35.68, 35.87, 35.53, 35.16, 36.24, 35.7, 35.65, 35.55, 35.53, 36.03, 35.65, 36.22, 35.47, 35.65, 35.74, 35.31, 35.88, 35.36, 35.08, 35.51};
double SUB2_cy2[]= {35.89, 35.59, 35.51, 35.71, 35.48, 35.89, 36.17, 35.73, 35.43, 35.74, 35.65, 35.78, 35.81, 35.21, 35.18, 34.99, 35.84, 35.46, 35.77, 35.85, 36.12, 35.84, 35.74, 35.98, 35.74, 36.3, 36.39};
double SUB2_cy3[]= {35.86, 35.72, 35.84, 35.84, 35.84, 36.16, 36.02, 35.88, 35.93, 35.83, 35.67, 35.55, 35.84, 35.14, 35.67, 35.62, 35.85, 35.84, 35.55, 35.55, 36.02, 35.86, 35.8};


void setup() {
  // put your setup code here, to run once:
  double arrBBT1 = sizeof( SUB2_cy1 ) / sizeof( double );
  double arrBBT2 = sizeof( SUB2_cy2 ) / sizeof( double );
  double arrBBT3 = sizeof( SUB2_cy3 ) / sizeof( double );
  Serial.begin( 9600 );
  while(!Serial);
  delay(1000);
  Serial.println( arrBBT1 );
  Serial.println( arrBBT2 );
  Serial.println( arrBBT3 );
  
  double mean_BBT1 = SUB2_cy1.mean;
  Serial.print("LIMIT = ");
  Serial.println( mean_BBT1 );
}

float average (int * array, int len)  // assuming array is int.
{
  long sum = 0L ;  // sum will be larger than an item, long for safety.
  for (int i = 0 ; i < len ; i++)
    sum += array [i] ;
  return  ((float) sum) / len ;  // average will be fractional, so float may be appropriate.
}

void loop() {
  // put your main code here, to run repeatedly:

}

Could anyone help me, please…
:frowning:
Thanks.

How do you normally find a mean? Add the samples up and divide by the number of samples.

Oh, sorry! What I mean is, I want to find the mean value for each SUB2_cy[].

double mean_BBT1 = SUB2_cy1[] / arrBBT1;
  Serial.print("LIMIT = ");
  Serial.println( mean_BBT1 );

last code I use it like the above. But the code still has an array.

For each array, add up all the values of the samples and divide by the number of samples in that array.

There's a big clue in the function "average" in your code, but the array type is unsuitable for your arrays

In your opinion what should I do?

SyukriY:
In your opinion what should I do?

See reply #1

  double arrBBT1 = sizeof( SUB2_cy1 ) / sizeof( double );

It does NOT make sense to store the number of elements in the array in a double. You can NOT have 3.14159 elements in the array.

But the code still has an array.

Why is that a problem?

The mean of an array is the middle value (entry) of the array, not the Average which is sum of all entries divided by count of entries.

But I’m guessing you mean average from your code.

mistergreen: The mean of an array is the middle value (entry) of the array, not the Average which is sum of all entries divided by count of entries.

No, that's the median (and the array has to be sorted.) (Arithmetic) mean and average are synonymous

Paul, sorry... Wrong word...

But the code still has an array.

Actually, I want to write, error. Not array...

My mistake. Sorry.

SyukriY: Paul, sorry... Wrong word...

Actually, I want to write, error. Not array...

My mistake. Sorry.

Serial.println (F("error"));

mistergreen,

But I'm guessing you mean average from your code.

yes, you write! What I meant is average. Actually, it supposes to be liked this.

There have been data in each array. At each array I want to take out the number of data in each array. This have been done.

double arrBBT1 = sizeof( SUB2_cy1 ) / sizeof( double );
  double arrBBT2 = sizeof( SUB2_cy2 ) / sizeof( double );
  double arrBBT3 = sizeof( SUB2_cy3 ) / sizeof( double );

Then, I want to find the average of the data in each array.

Then, I want to find the average of the data in each array.

It has been said before, but the seems it needs repeating, add together all the samples in each array, and divide the sum by the number of samples in that array

There is a massive clue in your original post, but the array type is incorrect.

AWOL,
erm…

Add the samples up and divide by the number of samples.

Yes, I can do that. But, it will not be automatically.
Because each array has a different number of samples.
I want it to be automatic by using the sizeof code.

float average (int * array, int len) You need to make one tiny change to that line.

About this,

float average (int * array, int len)  // assuming array is int.
{
  long sum = 0L ;  // sum will be larger than an item, long for safety.
  for (int i = 0 ; i < len ; i++)
    sum += array [i] ;
  return  ((float) sum) / len ;  // average will be fractional, so float may be appropriate.
}

I’m not going to use it. I will use this.

#include <Average.h>

double SUB2_cy1[]= {35.92, 35.73, 35.79, 36.15, 35.73, 35.91, 36.14, 35.68, 35.87, 35.53, 35.16, 36.24, 35.7, 35.65, 35.55, 35.53, 36.03, 35.65, 36.22, 35.47, 35.65, 35.74, 35.31, 35.88, 35.36, 35.08, 35.51};
double SUB2_cy2[]= {35.89, 35.59, 35.51, 35.71, 35.48, 35.89, 36.17, 35.73, 35.43, 35.74, 35.65, 35.78, 35.81, 35.21, 35.18, 34.99, 35.84, 35.46, 35.77, 35.85, 36.12, 35.84, 35.74, 35.98, 35.74, 36.3, 36.39};
double SUB2_cy3[]= {35.86, 35.72, 35.84, 35.84, 35.84, 36.16, 36.02, 35.88, 35.93, 35.83, 35.67, 35.55, 35.84, 35.14, 35.67, 35.62, 35.85, 35.84, 35.55, 35.55, 36.02, 35.86, 35.8};


void setup() {
  // put your setup code here, to run once:
  double arrBBT1 = sizeof( SUB2_cy1 ) / sizeof( double );
  double arrBBT2 = sizeof( SUB2_cy2 ) / sizeof( double );
  double arrBBT3 = sizeof( SUB2_cy3 ) / sizeof( double );
  Serial.begin( 9600 );
  while(!Serial);
  delay(1000);
  Serial.println( arrBBT1 );
  Serial.println( arrBBT2 );
  Serial.println( arrBBT3 );
  
  double mean_BBT1 = SUB2_cy1.mean;
  Serial.print("LIMIT = ");
  Serial.println( mean_BBT1 );
}

I will use this

Ok, let us know how it goes

An apology, when I wrote you had to changefloat average (int * array, int len), I forgot that there's a very similar mod to the first line of the function too.

, I forgot that there's a very similar mod to the first line of the function too.

What is it?

I think I already mentioned that the type of the array (pointer) was incorrect. So is the sum.

  double arrBBT1 = sizeof( SUB2_cy1 ) / sizeof( double );
  double arrBBT2 = sizeof( SUB2_cy2 ) / sizeof( double );
  double arrBBT3 = sizeof( SUB2_cy3 ) / sizeof( double );

The number of elements in the array is an integral value, guaranteed.

You REALLY need to learn to use the correct types everywhere.