I really need help with the following problem. Assume that, for a given town, mi
ID: 666456 • Letter: I
Question
I really need help with the following problem.
Assume that, for a given town, mid day daily temperature in Fahrenheit was measured for a month and stored in a file called temperature.txt. Write a complete C language program that reads the stored temperature, one at a time, from the file temperature.txt and converts the read temperature to its equivalent in Celsius. The program should store each converted temperature to an output file called results.txt. The program should also find the average, the minimum, and the maximum temperatures in Celsius and save the results in the output file.
Explanation / Answer
//Program:
#include <stdio.h>
#include <stdlib.h>
float minTemp(float[]);
float maxTemp(float[]);
float avgTemp(float[]);
int main()
{
float fh[30],cl[30];
float f,c;
float min, max, avg;
int i=0,j=0;
FILE *fptr,*fptr1;
fptr=(fopen("temperature.txt","r"));
if(fptr==NULL){
printf("Error!");
exit(0);
}
else
{
int t;
while(i<=30)
{
fscanf(fptr,"%f",&fh[i++]);
}
fclose(fptr);
fptr1=(fopen("celsius.txt","w"));
fprintf(fptr1,"Temperature in Celsius: ");
for(i=0;i<30;i++)
{
cl[i]=(fh[i]-32) /1.8;
fprintf(fptr1,"%f ",cl[i]);
}
min=minTemp(cl);
max=maxTemp(cl);
avg=avgTemp(cl);
fprintf(fptr1,"Minimum Temperature:%f ",min);
fprintf(fptr1,"Maximum Temperature:%f ",max);
fprintf(fptr1,"Average Temperature:%f ",avg);
}
//getch();
return 0;
}
float minTemp(float f[])
{
float min=f[0];
int i;
for(i=1;i<30;i++)
{
if(f[i]<min)
min=f[i];
}
return min;
}
float maxTemp(float f[])
{
float max=f[0];
int i;
for(i=1;i<30;i++)
{
if(f[i]>max)
max=f[i];
}
return max;
}
float avgTemp(float f[])
{
float t=0;
int i;
for(i=0;i<30;i++)
{
t=t+f[i];
}
return t/30;
}
// input file: temperature.txt
34
67
100
89
34
23
78
98
12
34
23
12
70
54
76
29
90
80
53
92
22
26
67
71
30
20
69
33
77
88
// Output file : celsius.txt
Temperature in Celsius:
1.111111
19.444445
37.777779
31.666666
1.111111
-5.000000
25.555555
36.666668
-11.111111
1.111111
-5.000000
-11.111111
21.111111
12.222222
24.444445
-1.666667
32.222221
26.666666
11.666667
33.333332
-5.555555
-3.333333
19.444445
21.666666
-1.111111
-6.666667
20.555555
0.555556
25.000000
31.111111
Minimum Temperature:-11.111111
Maximum Temperature:37.777779
Average Temperature:12.796297