C Programming Energy can be measured in several different scales: calories (c),
ID: 671857 • Letter: C
Question
C Programming
Energy can be measured in several different scales: calories (c), joules (J), ergs (erg) and foot-pound force (f t lbf) among others. To convert between these scales, you can use the following facts:
• 1 erg equals 1.0 10-7J
• 1 ft-lbs equals 1.3558 joules
• 1 calorie is equal to 4.184 joules
For this exercise, you will write a single function to convert a single value to all four at once. Specifically you will implement the following function:
int convertEnergy(double *cals, double *joules, double *ergs, double *ftlbf, Scale scale);
• Create a main function that prompts the user for one of the scales (an integer 1-4 in the order listed above) and a value to convert and outputs the result of all four scales
An example output is:
Enter a scale: (1 = Calorie, 2 = Joule, 3 = Erg, 4 = ft-lbf): 2
Enter the value of energy: 10
-> 2.390057 calories
-> 10.000000 joules
-> 100000000.000000 ergs
-> 7.375719 ft-lbf
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void do_next_op(int op,float num1);
int main()
{
int op;
int num1;
float res;
printf ("Enter the number from list ");
printf ("1.calories ");
printf ("2. Joule ");
printf ("3 Erg ");
printf ("4 Ft-lbf ");
printf ("Enter number: ");
printf ("Enter a number to calculate for Energy: ");
scanf ("%f", &num1);
printf ("%c%1.2f ",op ,num1);
do_next_op(op, num1);
}
void do_next_op(int op,float num1)
{
float res;
switch(op)
{
case('1'):
{
res= num1*4.184;
printf ("The result so far is %1.2f ",res);
break;
}
case('2'):
{
res= num1* 0.239005736 ;
printf ("The result so far is %1.2f ", res);
break;
}
case('3'):
{
res=num1*(1.0E-7) ;
printf ("The result so far is %1.2f ", res);
break;
}
case('4'):
{
res=num1*1.3558;
printf ("The result so far is %1.2f ", res);
break;
}
default:
break;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.