Write a function that writes a series of random Fahrenheit temperatures and thei
ID: 3781229 • Letter: W
Question
Write a function that writes a series of random Fahrenheit temperatures and their corresponding Celsius temperatures to a tab-delimited file. Use 32 to 212 as your temperature range. From the user, obtain the following: The number of temperatures to randomly generate. The name of the output file. A sample run is included below (you must follow the format provided below): Please enter the name of your file: Example.txt Please enter the number of iterations: 100 Example txt: Fahrenheit Celsius 152.00 66.67 97.00 36.11 32.000.00 99.00 37.22 35.00 1.67 etc.. write a function that reads a file produced by Problem I. Focusing only on the Celsius temperatures in the file, calculate and display to screen the following: Mean or Average Minimum MaximumExplanation / Answer
// 1. C++ code output to file the calculated celsius values
#include <iostream>
#include <cstdio>
#include <cmath>
#include <fstream>
#include <cstring>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <utility>
#include <ctime>
#include <limits.h>
using namespace std;
void celsiusTofile(string filename, int iterations)
{
ofstream outFile (filename.c_str());
if (outFile.is_open())
{
outFile << "Fahrenheit Celsius ";
for (int i = 0; i < iterations; ++i)
{
float random = ((float) rand()) / (float) RAND_MAX;
float diff = 212 - 32;
float fahrenheit = random * diff;
float celsius = 5*(fahrenheit-32)/9;
outFile << fahrenheit << " " << celsius << endl;
}
outFile.close();
}
else cout << "Unable to open file";
}
int main ()
{
string filename;
int iterations;
cout << "Please enter the name of your file: ";
cin >> filename;
cout << "Please enter the number of iterations: ";
cin >> iterations;
celsiusTofile(filename,iterations);
return 0;
}
/*
output:
Please enter the name of your file: examaple.txt
Please enter the number of iterations: 20
example.txt
Fahrenheit Celsius
151.234 66.241
70.9889 21.6605
140.958 60.5321
143.719 62.0662
164.097 73.387
35.5592 1.97736
60.3401 15.7445
138.281 59.0452
49.9995 9.99969
99.7146 37.6192
85.9315 29.9619
113.197 45.1093
65.6612 18.7007
92.4122 33.5623
171.401 77.4452
164.915 73.8417
114.428 45.7934
129.113 53.9519
25.4885 -3.61752
109.254 42.9191
*/
// 2. C++ code read from file and determine min max and avg celsius value
#include <iostream>
#include <cstdio>
#include <cmath>
#include <fstream>
#include <cstring>
#include <set>
#include <vector>
#include <map>
#include <algorithm>
#include <utility>
#include <ctime>
#include <limits.h>
using namespace std;
int main ()
{
string filename, line;
cout << "Please enter the name of your file: ";
cin >> filename;
float Fahrenheit, celsius;
float min = INT_MAX;
float max = INT_MIN;
float average = 0.0;
int size = 0;
ifstream inFile (filename.c_str());
if (inFile.is_open())
{
// skip first line
getline(inFile,line);
while (true)
{
inFile >> Fahrenheit >> celsius;
if(celsius < min)
min = celsius;
else if(celsius > max)
max = celsius;
average = average + celsius;
size++;
if(inFile.eof())
break;
}
inFile.close();
}
else cout << "Unable to open file";
average = average/size;
cout << "Average: " << average << endl;
cout << "Minimum: " << min << endl;
cout << "Maximum: " << max << endl;
return 0;
}
/*
output:
Please enter the name of your file: examaple.txt
Average: 41.3743
Minimum: -3.61752
Maximum: 77.4452
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.