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

Assume that, for a given town, mid-day daily temperature in Fahrenheit was measu

ID: 3865450 • Letter: A

Question

Assume that, for a given town, mid-day daily temperature in Fahrenheit was measured for a month and stored in a file called temprature.txt. Write a complete C Language program that reads the stored temperatures, one at a time, from the file temprature.txt and converts the read temperatures to its equivalent in Celsius. Your program should store each converted temperature in an output file called results.txt. Your program should also find the average, the minimum, and the maximum temperatures in Celsius and saves the results in the output file. Use data type double for input and output temperatures.

Explanation / Answer

#include <stdio.h>

#include <stdlib.h>

int main()

{

double tempInFahrenheit = 0.0, tempInCelsius = 0.0, maximum = -1, minimum = -1, average = 0.0;

int count = 0;

char *t, line[100];

FILE *fin, *fout;

fin = fopen("temprature.txt", "r");

fout = fopen("results.txt", "w");

// fopen returns a FILE pointer.

// Otherwise, NULL is returned if error occurred while opening file

if (fin && fout)

{

//feof returns 0 when end of file is found

while (feof(fin) == 0)

{

//Reading a line and storing it in string variable line

t = fgets(line, 100, fin);

// t contains

// the read string if fgets is successful

// or null pointer if end of file is found or error occurs

if (t)

{

count++;

tempInFahrenheit = atof(line); //atof converts string to double

tempInCelsius = (tempInFahrenheit - 32) / 1.8; //Converting fahrenheit to celsius

fprintf(fout, "%f ", tempInCelsius);

if (minimum == -1 || tempInCelsius < minimum)

{

minimum = tempInCelsius;

}

if (maximum == -1 || tempInCelsius > maximum)

{

maximum = tempInCelsius;

}

average += tempInCelsius;

}

}

fprintf(fout, "Minimum: %f ", minimum);

fprintf(fout, "Maximum: %f ", maximum);

average = average / count;

fprintf(fout, "Average: %f ", average);

//Cleanup

fclose(fin);

fclose(fout);

}

else

printf("Unable to open file temprature.txt or results.txt ");

return 0;

}

*******************temprature.txt***********************

25
32
56
72
9
15
68.0
55
42
27
102
320
526
254
365
30
20
8
89
45
74
63
19
154
58
67
100
200
300
400

*******************results.txt****************************

-3.888889
0.000000
13.333333
22.222222
-12.777778
-9.444444
20.000000
12.777778
5.555556
-2.777778
38.888889
160.000000
274.444444
123.333333
185.000000
-1.111111
-6.666667
-13.333333
31.666667
7.222222
23.333333
17.222222
-7.222222
67.777778
14.444444
19.444444
37.777778
93.333333
148.888889
204.444444
Minimum: -13.333333
Maximum: 274.444444
Average: 48.796296