I have created an header file in C and called it statistic.h. I have created a function to calculate the average ( I called the function "average"). But when I use to formula : sizeof (list)/sizeof (list[0]) , the result is wrong.
header file below:
#ifndef STATISTIC_H_INCLUDED
#define STATISTIC_H_INCLUDED
float average(int list[]){
int i;
float sum_elements,mean;
int total =sizeof (list)/sizeof (list[0]);
for (i=0;i<total;i++){
sum_elements=sum_elements+list[i];
}
mean = sum_elements / total;
return mean;
}
#endif // STATISTIC_H_INCLUDED
//see main code below where I'm trying to call the function I have previously created in the header.
#include <stdio.h>
#include "statistic.h"
int main(){
int list[]={26,12,16,56,112,24};
float mean=average(list); // I'm calling the average function I created in my header
printf("%f",mean);
return 0;
/*The average is 41.00 but I'm getting 19.00 instead . If I don't use
the sizeof function and manually declare the variable total=6 (the
number of element in the list), it gives me the correct result
(41.00).*/