In: Computer Science
The MarsX Space Vehicles Company has been very successful. Due to an increase in demand for its STS’s, the company had to hire thousands of scientists, engineers and staff from all over the world. As we all know, not all countries use the same system of measurements. The U.S., Liberia and Burma use the English system; while the rest of the world uses the metric system. In addition scientists use specific scales for some of their applications. To avoid confusion among team members from different countries, the company has commissioned the development and deployment of conversion applications. Your job is to write a program that interchangeably converts between different temperature scales (Celsius, Fahrenheit and Kelvin).
Your job depends on the success of this application. Therefore, make sure you write clean code and test it thoroughly.
Your program must do the following:
Celsius |
Fahrenheit |
Kelvin |
-10.00 |
14.00 |
263.15 |
0.00 |
32.00 |
273.15 |
100.00 |
212.00 |
373.15 |
Fahrenheit |
Celsius |
Kelvin |
-10.00 |
-23.33 |
249.82 |
0.00 |
-17.78 |
255.37 |
100.00 |
37.78 |
310.93 |
Kelvin |
Fahrenheit |
Celsius |
0.00 |
-459.67 |
-273.15 |
100.00 |
-279.67 |
-173.15 |
1000.00 |
1340.33 |
726.85 |
Needed conversion formulas:
From |
To |
Formula |
Celsius |
Fahrenheit |
F = C * (9.0/5.0) + 32 |
Celsius |
Kelvin |
K = C + 273.15 |
Fahrenheit |
Celsius |
C = (F – 32) * (5.0/9.0) |
Fahrenheit |
Kelvin |
K = (F + 459.67) * (5.0 /9.0) |
Kelvin |
Fahrenheit |
F = K * (9.0/5.0) – 459.67 |
Kelvin |
Celsius |
C = K – 273.15 |
Your program must comply with the following constraints:
int getMenuSelection(); /*Displays menu and gets user selection*/
void convertFromCelsius(); /*From Celsius to the other scales*/
void convertFromFahrenheit(); /*From Fahrenheit to the other scales*/
void convertFromKelvin(); /*From Kelvin to the other scales*/
int main()
{
int menuSelection = 0;
do
{
system(“cls”);
menuSelection = getMenuSelection();
switch (menuSelection)
{
case 1: convertFromCelsius();
break;
case 2: convertFromFahrenheit();
break;
case 3: convertFromKelvin ();
break;
case 4: break; /*Do nothing. Exit Condition*/
default: printf(“Please enter a number between 1 and 4 \n”);
system(“pause”);
}
} while (menuSelection != 4);
system(“pause”);
return 0;
}
Code:
#include <stdio.h>
#include<stdlib.h>
int getMenuSelection(){
int r;
printf("Enter 1 for
convert from Celcius ");
printf("Enter 2 for
convert from Farenheit ");
printf("Enter 3 for
convert from Kelvin ");
printf("Enter 4 for Quit
");
scanf("%d",&r);
printf(" ");
return r;
}
void convertFromCelsius(){
float c1,c2,c3,f1,f2,f3,k1,k2,k3;
//displaying message and taking input 3 celcius
value c1,c2,c3 from user
printf("Enter the first number that needs to be
converted from Celcius: ");
scanf("%f",&c1);
printf("Enter the second number that needs to be
converted from Celcius: ");
scanf("%f",&c2);
printf("Enter the thire number that needs to be
converted from Celcius: ");
scanf("%f",&c3);
//calculating three farenheit f1,f2,f3 from
c1,c2,c3
f1= c1 * (9.0/5.0) + 32;
f2= c2 * (9.0/5.0) + 32;
f3= c3 * (9.0/5.0) + 32;
//calculating three kelvin k1,k2,k3 from
c1,c2,c3
k1=c1 + 273.15;
k2=c2 + 273.15;
k3=c3 + 273.15;
//displaying results in atabular form
printf(" |-------------------------------|
");
printf("| Celcius | Farenheit | Kelvin |
");
printf("|---------|-----------|---------|
");
printf("|%-9.2f|%-11.2f|%-9.2f| ",c1,f1,k1);
//-9.f : - for left justification, 9 for 9 space , .2 for 2 decimal
precision
printf("|---------|-----------|---------|
");
printf("|%-9.2f|%-11.2f|%-9.2f|
",c2,f2,k2);
printf("|---------|-----------|---------|
");
printf("|%-9.2f|%-11.2f|%-9.2f|
",c3,f3,k3);
printf("|---------|-----------|---------|
");
}
void convertFromFahrenheit(){
float c1,c2,c3,f1,f2,f3,k1,k2,k3;
printf("Enter the first number that needs to be
converted from Farenheit: ");
scanf("%f",&f1);
printf("Enter the second number that needs to be
converted from Farenheit: ");
scanf("%f",&f2);
printf("Enter the thire number that needs to be
converted from Farenheit: ");
scanf("%f",&f3);
c1=(f1-32)*(5.0/9.0);
c2=(f2-32)*(5.0/9.0);
c3=(f3-32)*(5.0/9.0);
k1=(f1 + 459.67) * (5.0 /9.0);
k2=(f2 + 459.67) * (5.0 /9.0);
k3=(f3 + 459.67) * (5.0 /9.0);
printf(" |-------------------------------| ");
printf("| Farenheit | Celcius | Kelvin |
");
printf("|-----------|---------|---------|
");
printf("|%-11.2f|%-9.2f|%-9.2f|
",f1,c1,k1);
printf("|-----------|---------|---------|
");
printf("|%-11.2f|%-9.2f|%-9.2f|
",f2,c2,k2);
printf("|-----------|---------|---------|
");
printf("|%-11.2f|%-9.2f|%-9.2f|
",f3,c3,k3);
printf("|-----------|---------|---------|
");
}
void convertFromKelvin(){
float c1,c2,c3,f1,f2,f3,k1=-1,k2=-1,k3=-1;
while(k1<0 || k2<0 || k3<0){
printf("Kelvin cannot be
negative ");
printf("Enter the first
number that needs to be converted from Kelvin: ");
scanf("%f",&k1);
printf("Enter the second
number that needs to be converted from Kelvin: ");
scanf("%f",&k2);
printf("Enter the thire
number that needs to be converted from Kelvin: ");
scanf("%f",&k3);
}
c1=k1-273.15;
c2=k2-273.15;
c3=k3-273.15;
f1= k1 * (9.0/5.0) - 459.67;
f2= k2 * (9.0/5.0) - 459.67;
f3= k3 * (9.0/5.0) - 459.67;
printf(" |-------------------------------|
");
printf("| Kelvin | Farenheit | Celcius |
");
printf("|---------|-----------|---------|
");
printf("|%-9.2f|%-11.2f|%-9.2f|
",k1,f1,c1);
printf("|---------|-----------|---------|
");
printf("|%-9.2f|%-11.2f|%-9.2f|
",k2,f2,c2);
printf("|---------|-----------|---------|
");
printf("|%-9.2f|%-11.2f|%-9.2f|
",k3,f3,c3);
printf("|---------|-----------|---------|
");
}
int main(){
int menuSelection = 0;
do{
system("cls");
menuSelection = getMenuSelection();
switch (menuSelection)
{
case 1:
convertFromCelsius();
break;
case 2:
convertFromFahrenheit();
break;
case 3:
convertFromKelvin ();
break;
case 4: break; /*Do
nothing. Exit Condition*/
default: printf("Please
enter a number between 1 and 4 ");
system("pause");
}
} while (menuSelection != 4);
system("pause");
return 0;
}
output: