Write three C programs that print out the max, min, and the average values of th
ID: 3676027 • Letter: W
Question
Write three C programs that print out the max, min, and the average values of the
data file included below. Be sure to include the outputs of the programs in your
solutions. Use three data file structures that we have learned in chapter 3 (counter,
sentinel, and neither counter/sentinel). Also, draw three flowcharts for these three
programs.
130.5 120 140
140.4 140.8 150.2
140 -121 160.3
150 160.9 170
160.9 130.4 180
170 190.6 190
180 150.6 -110.5
-150 -110 120.1
150.1 170.7 130
190 180 170
Please include the results along with three flowcharts.
Explanation / Answer
#include <stdio.h>
int main()
{
FILE *sensor;
int num, min, max, sum, count, first;
sensor = fopen("F:data.txt", "r");
if (sensor != NULL)
{
for (sum = count = first = 0;
fscanf(sensor, "%d", &num) == 1; sum += num, ++count)
if (!first)
{
min = max = num; first = 1;
}
else if (num > max) max = num;
else if (num < min) min = num;
fclose(sensor);
printf("count = %d, min = %d, max = %d, sum = %d, avg = %.1lf ",
count, min, max, sum, sum / (double) count);
}
else
{
printf("Unable to read file 'F:data.txt'. " );
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.