Example:
#include <stdio.h>
#include <math.h>
#define NUM_STU 5 /* 5 students in the class */
/* Two arrays, one for student id, and one for
student grade */
int student_id[NUM_STU] = {123, 223, 326, 117, 326};
int student_gr[NUM_STU] = {84, 74, 90, 100, 70};
int main() {
int i, hi, lo, sum;
double avg;
hi = student_gr[0];
lo = student_gr[0];
sum = 0;
/* Let's print the grades to the screen */
printf("id\tgrade\n");
for (i = 0; i < NUM_STU; i++) {
printf("%d\t%d\n",student_id[i], student_gr[i]);
if(hi < student_gr[i])
hi = student_gr[i];
if(lo > student_gr[i])
lo = student_gr[i];
sum += student_gr[i];
}
printf("\nThe highest grade: %d", hi);
printf("\nThe lowest grade: %d", lo);
printf("\nThe sum of the grades: %d", sum);
avg = sum / NUM_STU;
printf("\nThe average grade: %f\n\n", avg);
return 0;
}
|