(NEEDS TO FOLLOW ALL REQUESTS)Thanks! You will create a file named average.c whi
ID: 671846 • Letter: #
Question
(NEEDS TO FOLLOW ALL REQUESTS)Thanks!
You will create a file named average.c which will open a file containing exactly 10 real numbers and compute their average. The file name will be provided through the command line.
Suppose you have a file called lab4-easy-data1.in has the following contents:
Here is a run of the program using the file lab4-easy-data1.in:
You will receive no credit if:
You do not name your files correctly
Your program does not compile
Your program crashes during testing
You will receive a deduction if:
You do not perform all the operations or handle all cases
Explanation / Answer
#include <stdio.h>
#include <math.h>
int main(){
double i = 0.00, sum = 0.00, n = 0.00,avg = 0.00;
FILE *fin;
fin = fopen("input.txt", "r");
while(fscanf(fin, "%lf", &n) != EOF){
sum += n;
i++;
}
avg = (sum / i);
printf("The average is %.2lf . ", avg);
fclose(fin);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.