Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C program that opens a text file called numbers.txt for reading. The fil

ID: 3759398 • Letter: W

Question

Write a C program that opens a text file called numbers.txt for reading. The file contains a sequence of integers all separated by new lines. Your program should read all of the numbers in the file and output the following information: A count of the number of integers read. The sum of all the integers in the file. The smallest integer in the file. The largest integer in the file. The average of all the numbers.

I ran the program with the following input file.

7

4

3

5

-20

1

9

87

32

41

9001

File location: To test your program, you can use an absolute path (full path) for the text file like what I had in the class. When you submit your program, just use “numbers.txt” (remove any path information) in fopen function. If you know the location of your executable program, you can copy the numbers.txt file to that directory. Your program output should look something like the following:

COMP 91.101 (201, 20)·704) here were e sum of ll the integers in the file is 9170. 11 integers in the file. Th The smallest integer in the fie is -20 The argest integr the fiie is01 The average of all the numbers is 833. 636364 Press any key to cont inue ..

Explanation / Answer

#include <stdio.h>
int main()
{
FILE *fp;
int num,i,sum,min,max;

double avg,sumd;
fp = fopen("numbers.txt", "r");

i=0;
sum=0;
while(!feof(fp))
{
fscanf(fp, "%i", &num);
sum = sum+num;
if (i==0){
min=num;
max=num;
}
else{
if (num < min) min=num;
if (num > max) max=num;
}
i=i+1;
}
sumd=sum*1.0;
avg=sumd/i;
printf("There were %i integers in the file. ",i);
printf("The sum of all the integers in the file is %i. ",sum);
printf("The smallest integer in the file is %i. ",min);
printf("The largest integer in the file is %i. ",max);
printf("The average of all the numbers is %f. ",avg);
printf("Press any key to continue . . .");
getchar();
  
fclose(fp);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote