Question

(Do this in C++ please and make sure no compile/run errors): I. Write a function that...

(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.

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
can you please do this lab? use lunix or C program its a continuation of a...
can you please do this lab? use lunix or C program its a continuation of a previous lab. the previous lab: Unix lab 4: compile and link multiple c or c++ files Please do the following tasks step by step: create a new directory named by inlab4 enter directory inlab4 create a new file named by reverse.c with the following contents and then close the file: /*reverse.c */ #include <stdio.h> reverse(char *before, char *after); main() {       char str[100];    /*Buffer...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter...
Please do the following in python: Write a program (twitter_sort.py) that merges and sorts two twitter feeds. At a high level, your program is going to perform the following: Read in two files containing twitter feeds. Merge the twitter feeds in reverse chronological order (most recent first). Write the merged feeds to an output file. Provide some basic summary information about the files. The names of the files will be passed in to your program via command line arguments. Use...
PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may...
PYTHON : Create a Email Address Parser (* Please do make comments*) Often times, you may be given a list of raw email addresses and be asked to generate meaningful information from such a list. This project involves parsing such a list and generating names and summary information from that list. The script, eparser.py, should: Open the file specified as the first argument to the script (see below) Read the file one line at a time (i.e., for line in...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import...
please write the code in java so it can run on jGRASP import java.util.Scanner; 2 import java.io.*; //This imports input and output (io) classes that we use 3 //to read and write to files. The * is the wildcard that will 4 //make all of the io classes available if I need them 5 //It saves me from having to import each io class separately. 6 /** 7 This program reads numbers from a file, calculates the 8 mean (average)...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in...
Getting the following errors: Error 1 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 565 Error 2 error C2436: '{ctor}' : member function or nested class in constructor initializer list on line 761 I need this code to COMPILE and RUN, but I cannot get rid of this error. Please Help!! #include #include #include #include using namespace std; enum contactGroupType {// used in extPersonType FAMILY, FRIEND, BUSINESS, UNFILLED }; class addressType { private:...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...