3) Write a C++ program that outputs a table having four columns of temperature v
ID: 3602905 • Letter: 3
Question
3) Write a C++ program that outputs a table having four columns of temperature values, ordered as follow: Celsius, Fahrenheit, Kelvin and Rankine. Use a while KELVIN CELSIUS FAHRENHEIT RANKINE too wu zrt loop. Let the Celsius temperature vary from 0.0 to 100.0 in steps of 5 °C. Convert each of these values to the other scales using the formulas given in Lab-1 (reproduced here for your convenience): 2 492 11 Fahrenheit (F): where F= 1.8C+32 Kelvin (K): where K-C+273.15 Rankine (R): where R=1.8K a. b. 4594 c. 4) Using files-Writing a table to a file: Modify the program written in Question-3, such that it outputs the table to a file (temperatures.txt) using an output file stream ofstream.Explanation / Answer
//both 3 and 4 program in same file
#include<iostream>
#include<fstream>
//for printing in format
#include<iomanip>
using namespace std;
int main()
{
//declare ofstream for output to a file
ofstream out;
//open output file
out.open("temperatures.txt");
//chk file is open
if (!out)
{
cout << "Not able to open output file" << endl;
return -1;
}
//now print table
cout.setf(ios::fixed, ios::floatfield);
cout.precision(2);
//set floating point for output file
out.setf(ios::fixed, ios::floatfield);
out.precision(2);
double f, k, r;
cout << "Centigrade " << "Fahreinheit " << "Kelvin " << "Rankine" << endl;
//write to output file
out << "Centigrade " << "Fahreinheit " << "Kelvin " << "Rankine" << endl;
for (float c = 0; c <= 100; c = c + 5 )
{
f = 1.8*c + 32;
k = c + 273.15;
r = 1.8*k;
cout << right << c<< " " << right <<f << " " << right << k << " " << right << r << endl;
//write to output file
out << right << c << " " <<right <<f << " " << right << k << " " << right << r << endl;
}
}
----------------------------------------------------
//output for problem 3,,stdout output
Centigrade Fahreinheit Kelvin Rankine
0.00 32.00 273.15 491.67
5.00 41.00 278.15 500.67
10.00 50.00 283.15 509.67
15.00 59.00 288.15 518.67
20.00 68.00 293.15 527.67
25.00 77.00 298.15 536.67
30.00 86.00 303.15 545.67
35.00 95.00 308.15 554.67
40.00 104.00 313.15 563.67
45.00 113.00 318.15 572.67
50.00 122.00 323.15 581.67
55.00 131.00 328.15 590.67
60.00 140.00 333.15 599.67
65.00 149.00 338.15 608.67
70.00 158.00 343.15 617.67
75.00 167.00 348.15 626.67
80.00 176.00 353.15 635.67
85.00 185.00 358.15 644.67
90.00 194.00 363.15 653.67
95.00 203.00 368.15 662.67
100.00 212.00 373.15 671.67
//output for problem 4 ,, content of file temperatures.txt file
Centigrade Fahreinheit Kelvin Rankine
0.00 32.00 273.15 491.67
5.00 41.00 278.15 500.67
10.00 50.00 283.15 509.67
15.00 59.00 288.15 518.67
20.00 68.00 293.15 527.67
25.00 77.00 298.15 536.67
30.00 86.00 303.15 545.67
35.00 95.00 308.15 554.67
40.00 104.00 313.15 563.67
45.00 113.00 318.15 572.67
50.00 122.00 323.15 581.67
55.00 131.00 328.15 590.67
60.00 140.00 333.15 599.67
65.00 149.00 338.15 608.67
70.00 158.00 343.15 617.67
75.00 167.00 348.15 626.67
80.00 176.00 353.15 635.67
85.00 185.00 358.15 644.67
90.00 194.00 363.15 653.67
95.00 203.00 368.15 662.67
100.00 212.00 373.15 671.67
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.