Write three C programs that print out the max, min, and the average values of th
ID: 3677141 • 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 (counter, sentinel, and neither counter/sentinel).
Also, draw three flowcharts for these three programs.
Program #1:
130.5
140.4
140
150
160.9
170
180
-150
150.1
190
Program #2:
120
140.8
-121
160.9
130.4
190.6
150.6
-110
170.7
180
Program #3:
140
150.2
160.3
170
180
190
-110.5
120.1
130
170
Also write the computer print outs of the C programs, the compiling steps and the results along with three flowcharts.
Explanation / Answer
## Program1.c
#include <stdio.h>
int main(int argc, char* argv[])
{
double sum = 0;
int count = 0;
double min, max;
/* now open it and read it */
FILE *f = fopen("program1.txt", "r");
int flag = 0; // to check wether read data from file is first one [to initialize all min max variable]
int counter = 5; // defining counter value 5
for(counter = 1; counter<=5 && (!feof(f)); counter++)
{
double number;
if (fscanf(f, "%lf", &number) != 1)
break; // file finished or there was an error
if(flag == 0){ // if this is first number read from file, initialize min max
min = number;
max = number;
flag = 1;
}
else{
if(max < number)
max = number;
if(min > number)
min = number;
}
sum = sum+number;
count++;
}
printf("Max: %lf ",max);
printf("Min: %lf ",min);
printf("Average: %lf ",(sum/count));
return 0;
}
/*
Output:
Max: 160.900000
Min: 130.500000
Average: 144.360000
*/
### Program2.c
#include <stdio.h>
int main(int argc, char* argv[])
{
double sum = 0;
int counter = 0;
double min, max;
/* now open it and read it */
FILE *f = fopen("program2.txt", "r");
int flag = 0; // to check wether read data from file is first one [to initialize all min max variable]
double sentinel = -110; // deifining sentinel
while (!feof(f))
{
double number;
if (fscanf(f, "%lf", &number) != 1)
break; // file finished or there was an error
// if sentinel number then stop reading
if(sentinel == number)
break;
if(flag == 0){ // if this is first number read from file, initialize min max
min = number;
max = number;
flag = 1;
}
else{
if(max < number)
max = number;
if(min > number)
min = number;
}
sum = sum+number;
counter++;
}
printf("Max: %lf ",max);
printf("Min: %lf ",min);
printf("Average: %lf ",(sum/counter));
return 0;
}
/*
Output:
Max: 190.600000
Min: -121.000000
Average: 110.328571
*/
###### Program3.c
#include <stdio.h>
int main(int argc, char* argv[])
{
double sum = 0;
int counter = 0;
double min, max;
/* now open it and read it */
FILE *f = fopen("program3.txt", "r");
int flag = 0; // to check wether read data from file is first one [to initialize all min max variable]
while (!feof(f))
{
double number;
if (fscanf(f, "%lf", &number) != 1)
break; // file finished or there was an error
if(flag == 0){ // if this is first number read from file, initialize min max
min = number;
max = number;
flag = 1;
}
else{
if(max < number)
max = number;
if(min > number)
min = number;
}
sum = sum+number;
counter++;
}
printf("Max: %lf ",max);
printf("Min: %lf ",min);
printf("Average: %lf ",(sum/counter));
return 0;
}
/*
Output:
Max: 190.000000
Min: -110.500000
Average: 130.010000
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.