Concept
How do you outline a C program that converts Fahrenheit to Celsius?
ComputerScienceOne / Converting Units
"Let’s write a program that will prompt the user to enter a temperature in degrees Fahrenheit and convert it to degrees Celsius using the formula C = ( F − 32) · 5 9. We begin with the basic program outline which will include preprocessor directives to bring in the standard library and the standard input/output library (we’ll need to prompt for input and print the result as output to the user). Further, we want our program to be executable, so we need to put our code into the main() function. Finally, we’ll document our program to indicate its purpose.\n\n#include <stdlib.h>\n\n#include <stdio.h>\n\n/**\n\n* This program converts Fahrenheit temperatures to\n\n* Celsius\n\n*/\n\nint main(int argc, char **argv) {\n\n//TODO: implement this\n\nreturn 0;\n\n}\n\nIt is common for programmers to use a comment along with a TODO note to themselves as a reminder of things that they still need to implement in a program. Let’s first outline the basic steps that our program will go through: 1. We’ll first prompt the user for input, asking them for a temperature in Fahrenheit. 2. Next we’ll read the user’s input, likely into a floating point number as degrees can be fractional. 3. Once we have the input, we can calculate the degrees Celsius by using the formula above. 4. Lastly, we will want to print the result to the user to inform them of the value.\n\nSometimes its helpful to write an outline of such a program directly in the code using comments to provide a step-by-step process. For example:\n\n#include <stdlib.h>\n\n#include <stdio.h>\n\n/**\n\n* This program converts Fahrenheit temperatures to\n\n* Celsius\n\n*/\n\nint main(int argc, char **argv) {\n\n//TODO: implement this\n\n//1. Prompt the user for input in Fahrenheit\n\n//2. Read the Fahrenheit value from the standard input\n\n//3. Compute the degrees Celsius\n\n//4. Print the result to the user\n\nreturn 0;\n\n}\n\nAs we read each step it becomes apparent that we’ll need a couple of variables: one to hold the Fahrenheit (input) value and one for the Celsius (output) value. It also makes sense that each of these should be double variables as we want to support fractional values. So at the top of our main() function, we’ll add the variable declarations: double fahrenheit, celsius; Each of the steps is now straightforward; we’ll use a printf() statement in the first step to prompt the user for input: printf(\"Please enter degrees in Fahrenheit: \"); In the second step, we’ll use the standard input to read the fahrenheit variable value from the user. Recall that we use the placeholder %lf for reading double values and"
Related Ideas
- Why does integer division make the Fahrenheit-to-Celsius program produce zero?ComputerScienceOne · Computing Quadratic Roots
- How do you design a Java program that converts Fahrenheit to Celsius?ComputerScienceOne · Converting Units
- How does scanf() read integer, character, and floating-point input in C?ComputerScienceOne · Basic I/O
- How can PHP convert a Fahrenheit temperature to Celsius?ComputerScienceOne · Converting Units
- What are preprocessor directives and why are standard libraries included?ComputerScienceOne · Preprocessor Directives
- How do you declare variables in C?ComputerScienceOne · Declaration & Assignment
- Where does execution begin in a C program?ComputerScienceOne · The cmain() Function
- Why must you use floating-point division when converting Fahrenheit to Celsius in Java?ComputerScienceOne · Converting Units