Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- double avg(double * a, int N, int (*f)(double *)){
- int i, count = 0;
- double sum = 0;
- for(i = 0; i < N; i++)
- if ((*f)(&a[i])){
- sum += a[i];
- count++;
- }
- if (count == 0)
- return 0;
- else
- return sum/count;
- }
- int f(double * d){
- if (d >= 0)
- return 0;
- else
- return 1;
- }
- int main(){
- double a[] = {1,2,3,4,5,6,7};
- int N = sizeof(a)/sizeof(a[0]);
- double r = avg(a, N, &f);
- printf("avg = %f", r);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement