Function that returns the average of all the array elements except for the final element

/* (Version 1) The array version of the function */
 
float average(int m[]){
    int total=0,i;
    float avg;
    for(i=0;m[i];i++)
    total+=m[i];
    avg=(float)(total)/i;
    return avg;
}
/* (Version 2) The pointer version of the function */
 
float average(int *pm){
    int total=0,*pt=pm;
    float avg;
    while(*pt){
          total+=*pt;
          pt++;
    }
    avg=(float)(total)/(pt-pm);
    return avg;
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.