Write a C++ program that inputs a temperature in Fahrenheit and outputs the temperature in Celsius.
Step 1: Write the basic algorithm
- Allocate storage
- Input data
- Perform calculations
- Output results
Step 2: Look for the things that must be input. List them under "Input data."
- Allocate storage
- Input data
- Temperature in Farhenheit
- Perform calculations
- Output results
Step 3: Look for things that must be output. List them under "Output results."
- Allocate storage
- Input data
- Temperature in Farhenheit
- Perform calculations
- Output results
- Temperature in Celsius
Step 4: Create a list of the conversions from input data to output data
- Allocate storage
- Input data
- Temperature in Farhenheit
- Perform calculations
- Convert Farhenheit to Celsius
- Output results
- Temperature in Celsius
Step 5: Each data listed in input and output must be allocated
- Allocate storage
- Temperature in Fahrenheit
- Temperature in Celsius
- Input data
- Temperature in Farhenheit
- Perform calculations
- Convert Farhenheit to Celsius
- Output results
- Temperature in Celsius
Admittedly, this is a contrived easy example that probably could be written without creating an algorithm, but these easy problems only last for a chapter or two.
Step 6: Research how to perform the calculations
In my web browser, I use google.com and searched for "convert Fahrenheit to Celsius." To convert, subtract 32 from the temperature Farhenheit, divide by 9, then multiply by 5. In pseudo-code, that looks like this:
Temperature in Celsius = (Temperature in Fahrenheit - 32) / 9 * 5
Add that to the appropriate spot under perform calculations.
- Allocate storage
- Temperature in Fahrenheit
- Temperature in Celsius
- Input data
- Temperature in Farhenheit
- Perform calculations
- Convert Farhenheit to Celsius
- Temperature in Celsius = (Temperature in Fahrenheit - 32) / 9 * 5
- Output results
- Temperature in Celsius
Step 7: Starting at the top of the algorithm, translate it into C++
Remember, when naming variables, make sure that
- The names must make sense to you, the programmer
- The names follow the capitalization rules given by your instructor
- The names are valid for C++ (no spaces in the names)
// Allocate storage
double farhenheit;
double celsius;
// Input data
cout << "Enter the temperature in Farhenheit: ";
cin >> farhenheit;
// Perform calculations
celsius = (fahrenheit - 32) / 9.0 * 5.0;
// Output results
cout << "The temperature in celsius is " << celsius << endl;
return 0;