this is our question we have: Write a program that reads a sequence of integers,
ID: 3623881 • Letter: T
Question
this is our question we have:
Write a program that reads a sequence of integers, one per line, representing high daily temperatures (in Fahrenheit) from a file. Let the user specify what file to analyze. Compute and display the number of temperatures read (days), the average (mean) temperature, and the maximum and minimum temperature. The file temps.txt contains 30 daily temperature readings and can be used as a test data file.
this is what i have so far:
#include
#include
int main()
{
int temp;
int tempcount = 0;
FILE *Temp;
Temp = fopen("c:/temp/temps.txt", "r");
if( Temp == NULL ) {
printf("File could not be opened ... exiting. ");
system("pause");
exit(EXIT_FAILURE);
}
while(fscanf(Temp, "%d", &temp) == 1) {
printf("%d ", temp); ++tempcount;
}
system("pause");
return (0);}
the thing im lost on is how am i supposed to take the values out of my text file(which is 30 tempature readings) and be able to find the averages, and max and min im completely lost in how to do that.
Explanation / Answer
please rate - thanks
#include <stdio.h>
#include <stdlib.h>
int main()
{
int temp,max=-10000,min=10000,sum=0;
int tempcount = 0;
double average;
FILE *Temp;
//Temp = fopen("c:/temp/temps.txt", "r");
Temp = fopen("temps.txt", "r");
if( Temp == NULL ) {
printf("File could not be opened ... exiting. ");
system("pause");
exit(EXIT_FAILURE);
}
while(fscanf(Temp, "%d", &temp) == 1) {
printf("%d ", temp); ++tempcount;
if(temp>max)
max=temp;
if(temp<min)
min=temp;
sum+=temp;
}
average=(double)sum/tempcount;
printf(" Number of temperatures read: %d ",tempcount);
printf("Average temperature: %.2f ",average);
printf("Minimum temperature %d ",min);
printf("Maximum temperature %d ",max);
fclose(Temp);
system("pause");
return (0);}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.