Question: To convert temperatures written in Fahrenheit to Celsius (Centigrade),
ID: 1715227 • Letter: Q
Question
Question:
To convert temperatures written in Fahrenheit to Celsius (Centigrade), you subtract 32, multiply by 5 and then divide by 9. Absolute temperature values are calculated as Celsius plus 273.15.
Write a program that prompts the user for the lowest and highest Fahrenheit temperature wanted in the table, and also prompts the user for the desired step size between rows of the table (the step size is 20 in the example table below). The program should begin by explaining to the user what it does, and should appropriately echo the user's input before printing the table.
The program should display the temperature conversion chart on the screen as follows:
Fahrenheit Celsius Absolute Value
0 -17.78 255.37
20 -6.67 266.48
40 4.44 277.5
... ...... ......
... ...... ......
300 148.89 422.04
Explanation / Answer
/* This program prints out a conversion table of temperatures. */
#include <iostream>
using namespace std;
const int LOWER = 0;
const int UPPER = 300;
const int STEP = 20;
int main()
{
int fahr = LOWER;
double celsius = 0;
/* Print table heading */
cout.width(15);
cout << "Fahrenheit";
cout.width(17);
cout << "Celsius";
cout.width(20);
cout << "Absolute Value" << endl << endl;
/* set format of individual values */
cout.precision(2);
cout.setf(ios::fixed);
/* print table from LOWER to UPPER */
for (fahr = LOWER ; fahr <= UPPER ; fahr += STEP) {
cout << " ";
cout.width(15);
cout << fahr;
celsius = (static_cast<double>(5)/9) * (fahr - 32);
cout.width(15);
cout << celsius;
cout.width(15);
cout << celsius + 273.15 << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.