(Do this in C++ please and make sure no compile/run
errors):
I. Write a function that writes a series of random Fahrenheit
temperatures and their correspond-
ing Celsius temperatures to a tab-delimited file. Use 32 to 212 as
your temperature range.
From the user, obtain the following:
1. The number of temperatures to randomly generate.
2. 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.00 0.00
99.00 37.22
35.00 1.67
etc...
II. 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:
1. Mean or Average
2. Minimum
3. Maximum
Note: Please provide answers of I and II on separate source codes.
Answer: I
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
//Creates an object of ofstream class
ofstream fw;
//To store fahrenheit, celsius, and number of temperature
int fahrenheit, celsius, no;
//For file name to store
char filename[30];
cout<<"\n The number of temperatures to randomly generate:
";
cin>>no;
cout<<"\n The name of the output file: ";
cin>>filename;
//Open the specified file for writing
fw.open (filename);
fw<< "Fahrenheit \tCelsius";
//Iterates no times entered by the user
for(int x = 0; x < no; x++)
{
//Generates random number
fahrenheit = (rand()% 180) + 32;
//Calculates the celsius
celsius = ((fahrenheit - 32) * 5) / 9;
//Write the fahrenheit and celsius in the specied file
fw<<endl<<fahrenheit<<"\t\t"<<celsius;
}
//close the file
fw.close();
}
Output:
The number of temperatures to randomly generate: 20
The name of the output file: mydata.txt
mydata.txt file contains
Fahrenheit Celsius
73 22
139 59
66 18
72 22
121 49
96 35
170 76
50 10
174 78
196 91
157 69
97 36
93 33
119 48
93 33
163 72
147 63
94 34
179 81
68 20
Answer II:
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
//Creates an object of ifstream class
ifstream fr;
//To store fahrenheit, celsius, and number of temperature
int fahrenheit[20], celsius[20], no, tot = 0, min, max;
//To store average, total and minimum and maximum celsius
float avg;
char da[10];
//Open the mydata.txt file for reading
fr.open("mydata.txt");
int c = 0;
//Reads the heading
fr>>da; fr>>da;
//Loops till end of file
while(!fr.eof())
{
//Reads data
fr>>fahrenheit[c];
fr>>celsius[c];
//Increase the counter
c++;
}
//Initially minimum and maximum is starting position
min = celsius[0];
max = celsius[0];
//Loops till end of celsius
for(int t = 0; t < c; t++)
{
//Calculates the total
tot += celsius[t];
//Checks for maximum celsius
if(celsius[t] > max)
max = celsius[t];
//Checks for minimum celsius
if(celsius[t] < min)
min = celsius[t];
}
//Calculates average
avg = tot / c;
//Close file
fr.close();
//Displays information
cout<<"\n Average Celsius = "<<avg;
cout<<"\n Maximum Celsius = "<<max;
cout<<"\n Minimum Celsius = "<<min;
}
Output:
Average Celsius = 47
Maximum Celsius = 91
Minimum Celsius = 10
Get Answers For Free
Most questions answered within 1 hours.