Question

In: Computer Science

1. Writing a Random Temperature File Write a function that writes a series of random Fahrenheit...

1. Writing a Random Temperature File
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:

• 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.00 0.00
     99.00 37.22
     35.00 1.67
     etc...

2. Reading a Random Temperature File
Write a function that reads a file produced by Problem 2. Focusing only on the Celsius temperatures in the file, calculate and display to screen the following:

• Mean or Average• Minimum
• Maximum

please include eveery detail you can. plus comments. Thank you !

Solutions

Expert Solution

#include<iostream>

#include<cstdlib>

#include<fstream>

#include<string>

#include<iomanip>

using namespace std;

void write_to_file(string &filename, int count){

               

                ofstream outfile(filename.c_str());

                if(outfile.is_open()){

                               

                                outfile<<"Fahrenheit\tCelsius\n";

                                double fahrenheit,celsius;

                                for(int i=1; i<=count; i++){

                                               

                                                fahrenheit= 32 + rand()%181;

                                                celsius = (fahrenheit-32)/1.8;

                                                outfile<<fixed<<setprecision(2)<<fahrenheit<<"\t"<<fixed<<setprecision(2)<<celsius<<endl;

                                }

                                outfile.close();

                }else{

                                cout<<"Unable to write data to file."<<endl;

                                exit(EXIT_FAILURE);

                }

               

}

void read_from_file(string &filename, double &mean, double &min, double &max){

               

                ifstream infile(filename.c_str());

               

                int count=0;

                double fahrenheit,celsius;

                double total_celsius;

                if(infile.is_open()){

                                string header;

                                getline(infile,header,'\n');

                                while(infile>>fahrenheit>>celsius){

                                                total_celsius+=celsius;

                                                count+=1;

                                                if(count==1){

                                                                min=max=celsius;

                                                }

                                                if(min>celsius)min=celsius;

                                                if(max<celsius) max=celsius;

                                }

                                mean = total_celsius/count;

                                infile.close();

                               

                }else{

                                cout<<"Unable to write data to file."<<endl;

                                exit(EXIT_FAILURE);

                }

}

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;

               

                write_to_file(filename,iterations);

               

               

                double mean=0, min_temp, max_temp;

                read_from_file(filename,mean,min_temp,max_temp);

                cout<<"Mean temperature : "<<mean<<" C"<<endl;

                cout<<"Min temperature : "<<fixed<<setprecision(2)<<min_temp<<" C"<<endl;

                cout<<"Max temperature : "<<fixed<<setprecision(2)<<max_temp<<" C"<<endl;

               

               

               

               

}


Related Solutions

Please use Python 3 3). Write a function that writes a series of random numbers to...
Please use Python 3 3). Write a function that writes a series of random numbers to a text file named ‘random_number.txt’. Each random number should be in the range of 1 through 500. The function should let the user specify how many random numbers the file will hold. Then write another function that reads the random numbers from the ‘random_number.txt’ file, displays the numbers, and then displays the total of the numbers and the number of random numbers read from...
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user...
Write a function in bash that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin
****USING BASH**** Write a function that takes an input temperature (Celsius or Fahrenheit) from the user...
****USING BASH**** Write a function that takes an input temperature (Celsius or Fahrenheit) from the user converts that temperature Celsius to Fahrenheit, Fahrenheit to Celsius, Celsius to Kelvin and Fahrenheit to Kelvin.
Write a Temperature class that will hold a temperature in Fahrenheit, and provide methods to get...
Write a Temperature class that will hold a temperature in Fahrenheit, and provide methods to get the temperature in Fahrenheit, Celsius and Kelvin. The class should have the following field: ftemp – A double that hold a Fahrenheit temperature. The class should have the following methods: Constructor – The constructor accepts a Fahrenheit temperature (as a double) and stores it in the ftemp field. setFahrenheit – The setFahrenheit method accepts a Fahrenheit temperature (as a double) and stores it in...
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a Python program that uses function(s) for writing to and reading from a file: a....
Write a Python program that uses function(s) for writing to and reading from a file: a. Random Number File Writer Function Write a function that writes a series of random numbers to a file called "random.txt". Each random number should be in the range of 1 through 500. The function should take an argument that tells it how many random numbers to write to the file. b. Random Number File Reader Function Write another function that reads the random numbers...
1. Temperature in kelvins (K) is related to the temperature in degrees Fahrenheit (°F) by the...
1. Temperature in kelvins (K) is related to the temperature in degrees Fahrenheit (°F) by the equation Design a MATLAB program that will do the following a. Prompt the user to enter an input temperature in °F. b. Read the input temperature. c. Calculate the temperature in kelvins in a separate function d. Write out the results with two digits to the right of the decimal (Use the fprintf function). The results should go to the command window.
Let x be a random variable that represents the average daily temperature (in degrees Fahrenheit) in...
Let x be a random variable that represents the average daily temperature (in degrees Fahrenheit) in July in a town in Colorado. The x distribution has a mean μ of approximately 75°F and standard deviation σ of approximately 8°F. A 20-year study (620 July days) gave the entries in the rightmost column of the following table. I         II III IV Region under Normal Curve x°F Expected % from Normal Curve Observed Number of Days in 20 Years μ – 3σ...
Function writing: 1). (1 point) Write the prototype of a value-returning function checkIfOdd. This function takes...
Function writing: 1). (1 point) Write the prototype of a value-returning function checkIfOdd. This function takes three integer parameters. It returns true if all three of the parameters are odd, and false otherwise. 2). (1 point) Write a sample call for this function. (Assume that you are calling from main) 3). (3 points) Write the definition (header and body) of the function.
Write a program that displays a temperature conversion table for degrees Celsius and degrees Fahrenheit. The...
Write a program that displays a temperature conversion table for degrees Celsius and degrees Fahrenheit. The table should include rows for all temperatures between 0 and 100 degrees Celsius that are multiples of 10 degrees Celsius. Include appropriate headings on your columns. The formula for converting between degrees Celsius and degrees Fahrenheit can be found on the internet. Python 3
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT