How to resolve the algorithm Temperature conversion step by step in the C++ programming language
How to resolve the algorithm Temperature conversion step by step in the C++ programming language
Table of Contents
Problem Statement
There are quite a number of temperature scales. For this task we will concentrate on four of the perhaps best-known ones: Kelvin, Celsius, Fahrenheit, and Rankine. The Celsius and Kelvin scales have the same magnitude, but different null points. The Fahrenheit and Rankine scales also have the same magnitude, but different null points. The Celsius/Kelvin and Fahrenheit/Rankine scales have a ratio of 5 : 9.
Write code that accepts a value of kelvin, converts it to values of the three other scales, and prints the result.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Temperature conversion step by step in the C++ programming language
This is a program that converts temperatures from Kelvin to other temperature scales, such as Celsius, Fahrenheit, Rankine, and Reaumur.
It uses a class called converter
that has a constructor that initializes the constants used for the conversion.
The convert
method takes a Kelvin temperature as input and converts it to the other scales.
The main
function creates an instance of the converter
class and then prompts the user to enter a Kelvin temperature to convert.
The program continues to run until the user enters a non-numeric value.
The system
function is used to pause the program and clear the screen.
The iomanip
library is used to format the output.
Source code in the cpp programming language
#include <iostream>
#include <iomanip>
//--------------------------------------------------------------------------------------------------
using namespace std;
//--------------------------------------------------------------------------------------------------
class converter
{
public:
converter() : KTC( 273.15f ), KTDel( 3.0f / 2.0f ), KTF( 9.0f / 5.0f ), KTNew( 33.0f / 100.0f ),
KTRank( 9.0f / 5.0f ), KTRe( 4.0f / 5.0f ), KTRom( 21.0f / 40.0f ) {}
void convert( float kelvin )
{
float cel = kelvin - KTC,
del = ( 373.15f - kelvin ) * KTDel,
fah = kelvin * KTF - 459.67f,
net = cel * KTNew,
rnk = kelvin * KTRank,
rea = cel * KTRe,
rom = cel * KTRom + 7.5f;
cout << endl << left
<< "TEMPERATURES:" << endl
<< "===============" << endl << setw( 13 )
<< "CELSIUS:" << cel << endl << setw( 13 )
<< "DELISLE:" << del << endl << setw( 13 )
<< "FAHRENHEIT:" << fah << endl << setw( 13 )
<< "KELVIN:" << kelvin << endl << setw( 13 )
<< "NEWTON:" << net << endl << setw( 13 )
<< "RANKINE:" << rnk << endl << setw( 13 )
<< "REAUMUR:" << rea << endl << setw( 13 )
<< "ROMER:" << rom << endl << endl << endl;
}
private:
const float KTRank, KTC, KTF, KTRe, KTDel, KTNew, KTRom;
};
//--------------------------------------------------------------------------------------------------
int main( int argc, char* argv[] )
{
converter con;
float k;
while( true )
{
cout << "Enter the temperature in Kelvin to convert: ";
cin >> k;
con.convert( k );
system( "pause" );
system( "cls" );
}
return 0;
}
//--------------------------------------------------------------------------------------------------
You may also check:How to resolve the algorithm Pascal's triangle/Puzzle step by step in the Perl programming language
You may also check:How to resolve the algorithm Priority queue step by step in the Factor programming language
You may also check:How to resolve the algorithm Sorting algorithms/Heapsort step by step in the Clojure programming language
You may also check:How to resolve the algorithm Entropy step by step in the Java programming language
You may also check:How to resolve the algorithm Knuth's algorithm S step by step in the jq programming language