The purpose of this assignment is to introduce you to basic C programming, inclu
ID: 3749137 • Letter: T
Question
The purpose of this assignment is to introduce you to basic C programming, including the use of functions, selections, and loops. For this program, you will communicate with the user asking whether to convert Fahrenheit into Celsius, or vice versa. Then, you call the corresponding function to do the conversion. Continue to do this until the user has decided not to use the system any more. You should write a sketch and implement the steps in detail one statement after another.
Instructions:
You may design your program with minor variations, but the following is a sample with key requirements (constants, functions, selections, and loops):
1. Your program starts with a while or do-while loop with the condition that your input character is F or C;
1) Before and within the while loop, it prints "your name > Fahrenheit/Celsius Converter. Please enter F or C:" For example, if it is my program, it will print: Jim Chen > Fahrenheit/Celsius Converter. Please enter F or C:
2) If you have any other input (other than F, f, C, or c), you inform the user that "The input is unknown!" and your program will quit the loop.
2. Within the loop, your program will use F, f, C, or c as selections in a switch statement. Actually, you can put "The input is unkown!" in the switch statement's default selection.
3. Within the corresponding switch selection, your program
1) prints "your name > Please enter a Fahrenheit degree number:" if you entered F or f; prints "your name > Please enter a Celsius degree number:" if you entered C or c.
2) reads the input number, which should be a "double" and should be passed as a parameter into the following corresponding function.
3) in case of F or f, call a function "double fc_converter(double);" with the input value; in case of C or c, call a function "double cf_converter(double);" otherwise, you inform the user "The input is unknown!" in the default selection.
4. In "double fc_converter(double);" you should use "if" to do the following.
1) if you enter a number in the range of -200 to +200, for example, "32", it will calculate according to the equation: C = FCR*(F - 32.00), where FCR is a pre-defined constant 0.556. Your variable should be in "double". After that, it will print the following before returning the Celsius degree:
32.00 F ==> 0.00 C
2) otherwise, it will print "Invalid Celsius temperature." and return a value according to the equation: C = sqrt(F).
5. Similarly, in "double cf_converter(double);" you should use "if" to do the following.
1) if you enter a number in the range of -200 to + 200, for example, "0", it will calculate according to the equation: F = CFR*C + 32.00, where CFR is a defined constant 1.8. After that, it will print the following before returning the Fahrenheit degree:
0.00 C ==> 32.00 F
2) otherwise, it will print "Invalid Fahrenheit temperature." and return a value according to the equation F = pow(C, 3).
6. After the switch, if your input is F, f, C, or c at the beginning,
1) print out the return value as: "The calculated value from the converter function is: <the return value from the converter function with precision to two digits after the decimal point>"
2) Loop back to Step 1.
7. For any other input at the beginning, your program exits the loop, and the program finishes.
Explanation / Answer
#include <math.h>
const double FCR = 0.556;
const double CFR = 1.8;
double cf_converter(double c) {
double ret = 0.0;
if (c <= 200 && c >= -200) {
ret = CFR * c + 32.00;
printf("0.00 C ==> 32.00 F ");
return ret;
} else {
printf("Invalid Fahrenheit temperature.");
return pow(c,3);
}
}
double fc_converter(double f) {
double ret = 0.0;
if (f <= 200 && f >= -200) {
ret = FCR *( f - 32.00);
printf("32.00 F ==> 0.00 C ");
return ret;
} else {
printf("Invalid Celsius temperature.");
return sqrt(f);
}
}
void convertor() {
char type;
while (1) {
printf("your_name > Fahrenheit/Celsius Converter. Please enter F or C:");
scanf("%c", &type);
switch(type) {
case 'c' || 'C': printf("your_name > Please enter a Celsius degree number:");
double c = 0.0;
scanf("%f",&c);
printf("The calculated value from the converter function is: %.2f", cf_converter(c));
case 'f' || 'F':
printf("your_name > Please enter a Fahrenheit degree number:");
double f = 0.0;
scanf("%f",&f);
printf("The calculated value from the converter function is: %.2f", fc_converter(f));
case default: printf("The input is unknown!"); break;
}
}
return ;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.