runner% cat score_arr0.c /* Read scores from stdin into an array. * Find their average. */ #include #include #define MAXS 10 /* maximum number of scores */ void main(void) { int s[MAXS]; /* array holding score values */ int n; /* number of scores read */ int i; /* counter for for loops */ int sum; /* running sum, used for average */ double ave; /* average of scores */ /* fetch scores from stdin. */ n = 0; while (scanf("%i", &s[n]) != EOF) { n++; if (n == MAXS) { printf("Problem reading scores\n"); exit(1); } } /* print scores on stdout */ for (i = 0; i < n; i++) printf("Score number %i = %i\n", i, s[i]); /* calculate the average */ sum = 0; for (i = 0; i < n; i++) sum = sum + s[i]; ave = (double)sum / (double)i; printf("Average: %.2f\n", ave); exit(0); } runner% lint -m -u score_arr0.c function returns value which is always ignored printf runner% cc -o score_arr0 score_arr0.c runner% score_arr0 #include #define MAXS 10 /* maximum number of scores */ int get_scores(int s[], int *n); void put_scores(int s[], int n); double calc_ave(int s[], int n); void main(void) { int s[MAXS]; /* array holding score values */ int n; /* number of scores read */ double ave; /* average of scores */ if (get_scores(s, &n) == -1) { printf("Problem reading scores\n"); exit(1); } put_scores(s, n); ave = calc_ave(s, n); printf("Average: %.2f\n", ave); exit(0); } /* get_scores: fetch scores from stdin. Return -1 if too many */ int get_scores(int s[], int *n) { *n = 0; while (scanf("%i", &s[*n]) != EOF) { (*n)++; if (*n == MAXS) return -1; } return 0; } /* put_scores: print scores on stdout */ void put_scores(int s[], int n) { int i; for (i = 0; i < n; i++) printf("Score number %i = %i\n", i, s[i]); } /* calc_ave: calculate and return the average */ double calc_ave(int s[], int n) { int i; int sum = 0; for (i = 0; i < n; i++) runner$ score_arr sum = sum + s[i]; 80 return (double)sum / (double)i; 95 } 75 82 runner$ score_arr