Advertisement
dmkozyrev

avg

Jan 22nd, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.46 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. double avg(double * a, int N, int (*f)(double *)){
  4.     int i, count = 0;
  5.     double sum = 0;
  6.     for(i = 0; i < N; i++)
  7.         if ((*f)(&a[i])){
  8.             sum += a[i];
  9.             count++;
  10.         }
  11.    
  12.     if (count == 0)
  13.         return 0;
  14.     else
  15.         return sum/count;
  16. }
  17.  
  18. int f(double * d){
  19.     if (d >= 0)
  20.         return 0;
  21.     else
  22.         return 1;
  23. }
  24.  
  25. int main(){
  26.     double a[] = {1,2,3,4,5,6,7};
  27.     int N = sizeof(a)/sizeof(a[0]);
  28.    
  29.     double r = avg(a, N, &f);
  30.     printf("avg = %f", r);
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement