Write a program to generate a table of conversions from Fahrenheit to Kelvin wit
ID: 3672897 • Letter: W
Question
Write a program to generate a table of conversions from Fahrenheit to Kelvin with table output to the screen. Allow the user to specify the starting and ending temperatures and the increment between lines. Your output should have two columns separated by a space and each column should be clearly labeled as Fahrenheit and Kelvin. Use a do/while loop in your program.
Use the endl, setf(), and precision commands so that your temperatures display in two column format and displays only two digits to the right of the decimal for either temperature.
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const static int FAHRENHEIT = 0;
const static int CELCIUS = 1;
const static int KELVIN= 2;
const static float FIVE_NINETHS = 5.0F/9.0F;
const static float NINE_FIFTHS = 9.0F/5.0F;
float fahrenheit_to_celcius( const float fahrenheit )
{
return (FIVE_NINETHS * (fahrenheit - 32.0F));
}
float fahrenheit_to_kelvin( const float fahrenheit )
{
return (273.0F + fahrenheit_to_celcius( fahrenheit ));
} f
loat celcius_to_fahrenheit( const float celcius )
{
return ( (NINE_FIFTHS * celcius) + 32.0F);
}
float celcius_to_kelvin( const float celcius )
{
return (273.0F + celcius);
}
float kelvin_to_celcius( const float kelvin )
{
return (kelvin - 273.0F);
}
float kelvin_to_fahrenheit( const float kelvin )
{
return celcius_to_fahrenheit( kelvin_to_celcius( kelvin ) );
}
void print_temps( float temps[3] )
{
printf( "%.3f degrees Fahrenheit %.3f degrees Celcius %.3f degrees Kelvin ", temps[FAHRENHEIT], temps[CELCIUS], temps[KELVIN] );
}
int main()
{
char value[32] = {0};
float temps[3] = {0};
char choice[2] = {0};
printf( "Enter Temperature Value: " );
fgets( value, 32, stdin );
value[ -1 + strlen( value ) ] = 0;
printf( "Enter Temperature Unit (f/F = Fahrenheit, c/C = Celcius, k/K = Kelvin q/Q = Quit): " );
fgets( choice, 2, stdin );
switch( choice[0] )
{
case 'f':
case 'F':
temps[FAHRENHEIT] = atof( value );
temps[CELCIUS] = fahrenheit_to_celcius( temps[FAHRENHEIT] );
temps[KELVIN] = fahrenheit_to_kelvin( temps[FAHRENHEIT] );
break;
case 'c':
case 'C':
temps[CELCIUS] = atof( value );
temps[FAHRENHEIT] = celcius_to_fahrenheit( temps[CELCIUS] );
temps[KELVIN] = celcius_to_kelvin( temps[CELCIUS] );
break;
case 'k':
case 'K':
temps[KELVIN] = atof( value );
temps[FAHRENHEIT] = kelvin_to_fahrenheit( temps[KELVIN] );
temps[CELCIUS] = kelvin_to_celcius( temps[KELVIN] );
break;
default: printf( "...see ya! " );
exit( 0 );
}
print_temps( temps );
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.