/*---------------------------------------------------*/ /* Program chapter3_7 */ /* */ /* This program generates a summary report from */ /* a data file that does not have a header record */ /* or a trailer record. */ #include #define FILENAME "sensor3.dat" int main() { /* Declare and initialize variables. */ int num_data_pts=0, k; double time, motion, sum=0, max, min; FILE *sensor3; /* Open file. */ sensor3 = fopen(FILENAME,"r"); /* While not at the end of the file, */ /* read and accumulate information. */ while ((fscanf(sensor3,"%lf %lf",&time,&motion)) == 2) { num_data_pts++; if (num_data_pts == 1) max = min = motion; sum += motion; if (motion > max) max = motion; if (motion < min) min = motion; } /* Print summary information. */ printf("Number of sensor readings: %i \n", num_data_pts); printf("Average reading: %.2f \n", sum/num_data_pts); printf("Maximum reading: %.2f \n",max); printf("Minimum reading: %.2f \n",min); /* Close file and exit program. */ fclose(sensor3); return 0; } /*---------------------------------------------------*/