In: Computer Science
/ File: temperature.cxx
// This program prints a table to convert numbers from one unit to
another.
// The program illustrases some implementation techniques.
#include // Provides cout
#include // Provides setw function for setting output width
#include // Provides EXIT_SUCCESS
#include // Provides assert function
using namespace std; // Allows all standard library items to be
used
double celsius_to_fahrenheit(double c)
// Precondition: c is a Celsius temperature no less than
absolute
// zero (-273.16).
// Postcondition: The return value is the temperature c converted
to Fahrenheit
// degrees.
{
const double MINIMUM_CELSIUS = -273.16; // Absolute zero in Celsius
degrees
assert(c >= MINIMUM_CELSIUS);
return (9.0 / 5.0) * c + 32;
}
void setup_cout_fractions(int fraction_digits)
// Precondition: fraction_digits is not negative.
// Postcondition: All double or float numbers printed to cout will
now be
// rounded to the specified digits on the right of the
decimal.
{
assert(fraction_digits > 0);
cout.precision(fraction_digits);
cout.setf(ios::fixed, ios::floatfield);
if (fraction_digits == 0)
cout.unsetf(ios::showpoint);
else
cout.setf(ios::showpoint);
}
int main( )
{
const char HEADING1[] = " Celsius"; // Heading for table's 1st
column
const char HEADING2[] = "Fahrenheit"; // Heading for table's 2nd
column
const char LABEL1 = 'C'; // Label for numbers in 1st column
const char LABEL2 = 'F'; // Label for numbers in 2nd column
const double TABLE_BEGIN = -50.0; // The table's first Celsius
temp.
const double TABLE_END = 50.0; // The table's final Celsius
temp.
const double TABLE_STEP = 10.0; // Increment between
temperatures
const int WIDTH = 9; // Number chars in output numbers
const int DIGITS = 1; // Number digits right of decimal pt
double value1; // A value from the table's first column
double value2; // A value from the table's second column
// Set up the output for fractions and print the table
headings.
setup_cout_fractions(DIGITS);
cout << "CONVERSIONS from " << TABLE_BEGIN << "
to " << TABLE_END << endl;
cout << HEADING1 << " " << HEADING2 <<
endl;
// Each iteration of the loop prints one line of the table.
for (value1 = TABLE_BEGIN; value1 <= TABLE_END; value1 +=
TABLE_STEP)
{
value2 = celsius_to_fahrenheit(value1);
cout << setw(WIDTH) << value1 << LABEL1 <<
" ";
cout << setw(WIDTH) << value2 << LABEL2 <<
endl;
}
return EXIT_SUCCESS;
}
1) The source code must be well structured like include relevant comments like on the top include the version like 1.0 and 1.1, date, and a brief description of what the program does.
2) . As you make changes you
can add one line description of the changes and change the version
# or add a version #
like version 1.0 and 1.1
3) Write a short report (not to exceed 100 words) that describes the work done, the input, and output when executing the program.
2.
Screenshot of the code:
Sample Output:
Code To Copy:
//The error-free code is provided below:
// This program prints a table to convert numbers from one unit to another.
// The program illustrates some implementation techniques.
//Include the header file ostream for including all stream.
#include <iostream>// Provides cout
//Include iomanip that is used for set precision.
#include <iomanip> // Provides setw function for setting output width
//Header file for EXIT_SUCCESS.
#include <stdlib.h>// Provides EXIT_SUCCESS
//Header file for assert.
#include <assert.h>// Provides assert function
using namespace std; // Allows all standard library items to be used
double celsius_to_fahrenheit(double c)
// Precondition: c is a Celsius temperature no less than absolute
// zero (-273.16).
// Postcondition: The return value is the temperature c converted to Fahrenheit
// degrees.
{
const double MINIMUM_CELSIUS = -273.16; // Absolute zero in Celsius degrees
assert(c >= MINIMUM_CELSIUS);
return (9.0 / 5.0) * c + 32;
}
void setup_cout_fractions(int fraction_digits)
// Precondition: fraction_digits is not negative.
// Postcondition: All double or float numbers printed to cout will now be
// rounded to the specified digits on the right of the decimal.
{
assert(fraction_digits > 0);
cout.precision(fraction_digits);
cout.setf(ios::fixed, ios::floatfield);
if (fraction_digits == 0)
cout.unsetf(ios::showpoint);
else
cout.setf(ios::showpoint);
}
int main( )
{
const char HEADING1[] = " Celsius"; // Heading for table's 1st column
const char HEADING2[] = "Fahrenheit"; // Heading for table's 2nd column
const char LABEL1 = 'C'; // Label for numbers in 1st column
const char LABEL2 = 'F'; // Label for numbers in 2nd column
const double TABLE_BEGIN = -50.0; // The table's first Celsius temp.
const double TABLE_END = 50.0; // The table's final Celsius temp.
const double TABLE_STEP = 10.0; // Increment between temperatures
const int WIDTH = 9; // Number chars in output numbers
const int DIGITS = 1; // Number digits right of decimal pt
double value1; // A value from the table's first column
double value2; // A value from the table's second column
// Set up the output for fractions and print the table headings.
setup_cout_fractions(DIGITS);
cout << "CONVERSIONS from " << TABLE_BEGIN << " to " << TABLE_END << endl;
//Provide the correct style of heading before printing the
//headers Celcius and Fehrenhit.
cout << " " << HEADING1 << "\t " << HEADING2 << endl;
// Each iteration of the loop prints one line of the table.
for (value1 = TABLE_BEGIN; value1 <= TABLE_END; value1 += TABLE_STEP)
{
value2 = celsius_to_fahrenheit(value1);
cout << setw(WIDTH) << value1 << LABEL1 << " ";
cout << setw(WIDTH) << value2 << LABEL2 << endl;
}
return EXIT_SUCCESS;
}
Description of the program: