Home > Software design >  Header function average gives me wrong result in C. If I call the "average" function which
Header function average gives me wrong result in C. If I call the "average" function which

Time:12-08

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).*/

CodePudding user response:

sizeof (list)/sizeof (list[0]); in average doesn't work since list decays into an int* when passed as an argument to the function. You need to send the size of the list into the function as an argument.

Example:

#include <stddef.h>
#include <stdio.h>

float average(int list[], size_t total) { // send total in as an argument
    float sum_elements = 0;               // note: initialize sum_elements

    for (size_t i = 0; i < total; i  ) {
        sum_elements = sum_elements   list[i];
    }

    return sum_elements / total;
}

int main() {
    int list[] = {26, 12, 16, 56, 112, 24};

    // do the size calculation here, where `list` is defined instead:
    float mean = average(list, sizeof list / sizeof *list);
    printf("%f", mean);

    return 0;
}

Demo

CodePudding user response:

The list parameter in your average function isn't an array, it's a pointer, so the sizeof trick doesn't work.

Unless it's the operand of the sizeof or unary & operators, or a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T" will be converted, or "decay", to an expression of type "pointer to T" and its value will be the address of the first element in the array.

When you call average:

float mean=average(list);

the expression list is converted from type "6-element array of int" to "pointer to int" and the value of the expression is the same as &list[0], so what average actually receives is a pointer value, not an array.

In the context of a function parameter declaration, T a[N] and T a[] are both "adjusted" to T *a - all three declare a as a pointer to T, not an array of T.

You will have to pass the array size as a separate parameter:

float average( int *list, size_t list_size ) 
{
  ...
  for ( size_t i = 0; i < list_size; i   ) 
    ...
}

and call it as

mean = average( list, sizeof list / sizeof list[0] );

You also need to explicitly initialize sum_elements to 0 in the average function.

  • Related