Result is not what it should be

Hi, I'm trying to find average of an array using function. But the average should be 5.5, instead it shows 5.00. I don't know if anything wrong with my code. Thank you.

int Array[10] = { 8, 7, 6, 3, 2, 1, 5, 4, 9, 10 };
double Result;
double avg;

  double ArrayAverage(int sum) {
    Result = sum/10;
    return Result;
  }

void setup() {
  Serial.begin(9600); 
  int sum=0;
  for (int index=0; index<=9; index++) {
    sum = sum + Array[index];
  }
  Result = ArrayAverage(sum);
  Serial.print("Result: ");
  Serial.print(Result);
}

void loop() {

}

you are doing integer maths, so sum/10 is an integer

do sum/10.0

(your function is not really useful as the number of entries is fixed)

1 Like

(double)sum/10

1 Like

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