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 program that asks the user for a Fahrenheit temperature and calls a function name...
Write a program that asks the user for a Fahrenheit temperature and calls a function name Celsius that returns the temperature in Celsius of the Fahrenheit temperature sent to it. Your function will look similar to this: double celsius(double f) { double c; : : return c; }
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...
Write a php program that writes numbers to a file. The file type should be .txt...
Write a php program that writes numbers to a file. The file type should be .txt and the file name should be numbers.tx. The numbers.txt should contain 10 random integers between 1 to 100 after the file is written.
1. Write a C++ code segment to read 50 temperature values in Fahrenheit and to convert...
1. Write a C++ code segment to read 50 temperature values in Fahrenheit and to convert them to Celsius. You convert a Fahrenheit temperature to Celsius by using the following formula: Celsius = 5.0 / 9 * (Fahrenheit - 32). 2.A client has purchased 20 products in a store. Write a C++ code segment to read the unit price and the number of items of each product and to compute and print the total price of all these products.
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT