Question

In: Computer Science

/ File: temperature.cxx // This program prints a table to convert numbers from one unit to...

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

Solutions

Expert Solution

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:

  • The program is computing the temperature from Celsius to Fahrenheit.
  • Along with computing the temperature values, the preconditions are also being taken care of.
  • For formatting the output in the tabular form, some heading labels such as Celsius, Fahrenheit, C and F are defined.
  • Also, the width, height and the table setups are defined in the main function.
  • The temperature values are computed from -50 Celsius to 50 Celsius.
  • For each predefined functions to be used, the required header files such as iostream, iomanip, assert and stdlib are included in the function.
  • A function named as celsius_to_fahrenheit(double c) is defined that taken the temperature in Celsius as its parameter.
  • In that function, the absolute zero value of Celsius is defined and the temperature from Celsius to Fahrenheit is computed and return the values in the main function.
  • Also, a function setup_cout_fractions(int fraction_digits) is defined to set up the decimal point with the help of set precision.
  • These functions are being called in the main and the values returned by these function along with proper heading are displayed in the output screen.
  • At last the exit success is being returned by the main function.

Related Solutions

Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
Write a program that computes and prints the average of numbers in a text file. I...
Write a program that computes and prints the average of numbers in a text file. I created a text file integers.txt that has the numbers 5,4,3,2,1. I need to define the average function Define the main function which will include the following things 1. prompt user for input of text file name 2. open and read input file, can be done before or inside high order functions 3. use two high order functions 4.calculate and display averages and original ist...
This program will read a bunch of numbers from an external file (numbers.txt) and calculate their...
This program will read a bunch of numbers from an external file (numbers.txt) and calculate their total and average. In addition, the total will be multiplied by an integer that is supplied by the user. ** IMPORTANT: The numbers are included in your starter code as a separate file (numbers.txt). Don't touch that file! All of your code should be placed in code.cpp, as usual. ** Input: After all numbers are read, what number would you like to multiply the...
(write a program that get the numbers from user and search the file numbers.text for that...
(write a program that get the numbers from user and search the file numbers.text for that value. in C++) numbers.txt: 10 23 43 5 12 23 9 8 10 1 16 9 you must to have the exact output: Enter a number: 10 10 last appears in the file at position 9 Enter a number: 29 29 does not appear in the file Enter a number: 9 9 last appears in the file at position 12 Enter a number:
Convert these numbers from Decimal to Binary 111: 66: 252 11 20 Convert these numbers from...
Convert these numbers from Decimal to Binary 111: 66: 252 11 20 Convert these numbers from Binary to Decimal 00110110 11111000 00000111 10101010 What is the Default Subnet mask of Class A IPV4 What is the Default Subnet mask of Class B IPV4 What is the Default Subnet mask of Class C IPV4 What is the CIDR notation or / short handwriting of Subnet masks: Class A: /?. Explain the reason Class B: /? Explain the reason Class C: /?...
5. Write a program that prints all numbers between 27 and 78, one number per line....
5. Write a program that prints all numbers between 27 and 78, one number per line. 6. In questions 6,7 the following list is used: [1,2,5,6,3,77,9,0,3,23,0.4,-12.4,-3.12] 7. Using “for” loop, write program that finds the smallest number in the list. 8. Using “for” loops, calculate arithmetic mean of all numbers in the list. Do not use built-in function in Python. 9. For this question envision you are creating a dummy alert to help the doctor determine if patient needs a...
Create a program that parses a CSV file of product data and prints the items with...
Create a program that parses a CSV file of product data and prints the items with a price that is less than or equal to that input by the user. • Your program should take two arguments: an input file to process and a price limit. • Print only the names of each item to stdout that have a price less than or equal to the given limit. • If the given file does not exist or cannot be opened,...
Assume s is a string of numbers. Write a program that prints the longest substring of...
Assume s is a string of numbers. Write a program that prints the longest substring of s in which the numbers occur in ascending order and compute the average of the numbers found. For example, if s = '561984235272145785310', then your program should print Longest substring in numeric ascending order is: 14578 Average: 5 In the case of ties, print the first substring. For example, if s = '147279', then your program should print Longest substring in numeric ascending order...
C++ Write a program that reads candidate names and numbers of votes in from a file....
C++ Write a program that reads candidate names and numbers of votes in from a file. You may assume that each candidate has a single word first name and a single word last name (although you do not have to make this assumption). Your program should read the candidates and the number of votes received into one or more dynamically allocated arrays. In order to allocate the arrays you will need to know the number of records in the file....
Write a program that processes numbers, corresponding to student records read in from a file, and...
Write a program that processes numbers, corresponding to student records read in from a file, and writes the required results to an output file (see main ( )). Your program should define the following functions: double read_double (FILE *infile) — Reads one double precision number from the input file. Note: You may assume that the file only contains real numbers. int read_integer (FILE *infile) - Reads one integer number from the input file. double calculate_sum (double number1, double number2, double...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT