Question

General overview This program will convert a set of temperatures from Fahrenheit to Celsius and Kelvin....

General overview

This program will convert a set of temperatures from Fahrenheit to Celsius and Kelvin.

Your program will be reading in three double values. The first values are starting and ending temperatures. The third value is the increment value. There is no prompt for the input. There is an error message that can be displayed. This is discussed later.

You need to display output for all of the values between the starting and ending values. First two values are temperatures in Fahrenheit. You need to display all of the values from the first temperature to the last temperature. You increment from one temperature to the next by the increment value (the third value you read in). You need to convert these temperatures to Celsius and Kelvin. You need to output the temperatures as Fahrenheit, Celsius, and Kelvin. The numbers should be 18 characters wide with 4 digits of precision and need to be in fixed format. Do not use tab characters (\t) to output the values.

For part 2 you MUST have at least 4 functions, including main. The main function will be the driver of the program. It needs to either do the processing or delegate the work to other functions.

Obviously the main function will have the same function that you have been using for all of your labs. In addition to need to have two functions with the following signatures:

double toCelsius(double fahrenheit)
double toKelvin(double celsius)

Note that the toCelsius function take a parameter that is in fahrenheit. The toKelvin function takes a parameter that is in celsius.

Other functions that you may want to have are:

  • A function to read in the values
  • A display function
  • A display heading function

The headings are also required (see the sample output below).

The conversion from Fahrenheit to Celsius is:

celsius = (fahrenheit - 32) / 1.8

The conversion from Celsius to Kelvin is:

kelvin = celsius + 273.15

Here is a sample run with valid input:

-30 100 20

The output would be:

        Fahrenheit           Celsius            Kelvin
          -30.0000          -34.4444          238.7056
          -10.0000          -23.3333          249.8167
           10.0000          -12.2222          260.9278
           30.0000           -1.1111          272.0389
           50.0000           10.0000          283.1500
           70.0000           21.1111          294.2611
           90.0000           32.2222          305.3722

For data validation you need to make sure the first number read in is less than or equal to the second number. The third number read in must be greater than 0. If this is not the case you need to output the following message and read in three new values:

Starting temperature must be <= ending temperature and increment must be > 0.0

The above message is all one line of output.

You need to keep reading in values and outputting messages until the values are all valid.

Using the following input :

40 30 5
30 40 -5
30 40 5

We get the output:

Starting temperature must be <= ending temperature and increment must be > 0.0
Starting temperature must be <= ending temperature and increment must be > 0.0
        Fahrenheit           Celsius            Kelvin
           30.0000           -1.1111          272.0389
           35.0000            1.6667          274.8167
           40.0000            4.4444          277.5944

Depending on the value for the increment the ending temperature may not be reached. You need to display the temperatures where the value of the Fahrenheit temperature is less than or equal to the ending temperature.

Consider this input:

100.5 110.4 5

The valid output will be:

        Fahrenheit           Celsius            Kelvin
          100.5000           38.0556          311.2056
          105.5000           40.8333          313.9833

The last Fahrenheit temperature output is 105.5. The next one would have been 110.5, but this is more than the ending temperature of 100.4. This is not an error condition. The output would be as above.

Think about what other tests cases are needed? Do we cover all possible input problems or valid types of input values?

Expected output

There are eight tests. Each test will have a new set of input data. You must match, exactly, the expected output.

You will get yellow highlighted text when you run the tests if your output is not what is expected. This can be because you are not getting the correct result. It could also be because your formatting does not match what is required. The checking that zyBooks does is very exacting and you must match it exactly. More information about what the yellow highlighting means can be found in course "How to use zyBooks" - especially section "1.4 zyLab basics".

Finally, do not include a system("pause"); statement in your program. This will cause your verification steps to fail.

Note: that the system("pause"); command runs the pause command on the computer where the program is running. The pause command is a Windows command. Your program will be run on a server in the cloud. The cloud server may be running a different operating system (such as Linux).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>
#include<iomanip>
using namespace std;

double toCelsius(double fahrenheit);
double toKelvin(double celsius);
void input_values(double &lower, double &upper, double &increment);
void print_header();
void display(double fahrenheit, double celsius, double kelvin);

int main()
{
double lower, upper, increment;
// Call function to take input values
input_values(lower, upper, increment);

double fahrenheit = lower;
double celsius, kelvin;

// Print header
print_header();

// Iterate loop till Fahrenheit <= upper
while(fahrenheit<=upper)
{
// Calculate Celsius and Kelvin by calling function
celsius = toCelsius(fahrenheit);
kelvin = toKelvin(celsius);

// Print display function
display(fahrenheit,celsius,kelvin);

// Increment Fahrenheit
fahrenheit+=increment;
}
return 0;
}


// Convert Fahrenheit to Celsius and return
double toCelsius(double fahrenheit)
{
double celsius = (fahrenheit - 32) / 1.8;
return celsius;
}

// Convert Celsius to Kelvin and return
double toKelvin(double celsius)
{
double kelvin = celsius + 273.15;
return kelvin;
}

// Read input values
void input_values(double &lower, double &upper, double &increment)
{
cin>>lower;
cin>>upper;
cin>>increment;

// Validate input values
// If lower>upper or increment<=0, ask user to enter input values again
while(lower>upper || increment<=0)
{
cout<<"Starting temperature must be <= ending temperature and increment must be > 0.0"<<endl;
cin>>lower;
cin>>upper;
cin>>increment;
}
}

void print_header()
{
// Print header
cout<<left<<setw(18)<<"Fahrenheit"<<left<<setw(18)<<"Celsius"<<left<<setw(18)<<"Kelvin"<<endl;
}

void display(double fahrenheit, double celsius, double kelvin)
{
// Print Fahrenheit, Celsius and Kelvin
// with width 18 and fixed 4 precision
cout<<left<<setw(18)<<fixed<<setprecision(4)<<fahrenheit<<left<<setw(18)<<fixed<<setprecision(4)<<celsius<<left<<setw(18)<<fixed<<setprecision(4)<<kelvin<<endl;
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
General overview This program will convert a set of temperatures from Fahrenheit to Celsius and Kelvin....
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT