Question

Exercise 5.4 The following program is supposed to convert a temperature in degree Fahrenheit to degree...

Exercise 5.4

The following program is supposed to convert a temperature in degree Fahrenheit to degree Celsius, but it will not produce the correct result. Use type casting to fix the problem and run the program for the test values. Call your new program ex54.cpp.

Test cases:

32 F is 0 C
212 F is 100 C

PROGRAM:

#include
using namespace std;
int main( )
{
      int t_in_fah, t_in_cel;  //Notice that we declared these two as integers, not the best choice
      cout << "Enter a temperature in Fahrenheit \n";
      cin >> t_in_fah;

      t_in_cel = 5/9*(t_in_fah - 32);
      cout << "The temperature in Celsius is: " << t_in_cel << endl;

     return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ERROR: In cpp, *,/ have equal precedence and have left to right associativity so in given expression 5/9*(t_in_fah - 32). 5/9 is calculated to 0 first and everything multiplies by 0 results to 0. To avoid this first do multiplication and then division.

//CODE

#include<iostream>
using namespace std;
int main( )
{
int t_in_fah, t_in_cel; //Notice that we declared these two as integers, not the best choice
cout << "Enter a temperature in Fahrenheit \n";
cin >> t_in_fah;

t_in_cel = 5*(t_in_fah - 32)/9;
cout << "The temperature in Celsius is: " << t_in_cel << endl;

return 0;
}

//OUTPUT

Add a comment
Know the answer?
Add Answer to:
Exercise 5.4 The following program is supposed to convert a temperature in degree Fahrenheit to degree...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Modify this code to display the message "That's hot!" if the temperature entered is greater than...

    Modify this code to display the message "That's hot!" if the temperature entered is greater than 90 degrees Farenheit or equivalent Celsius, or the message "That's cold!" if the temperature entered is less than -10 degrees Farenheit or equivalent Celsius. And to show one in between where it doesn't show any message. #include <iostream> #include <iomanip> using namespace std; // a temperature conversion program int main() { char tempType; double temp, fahren, celsius; cout << "Enter the temperature to be...

  • Convert Celsius to Fahrenheit. (Programming Exercise 2.1) or Convert Fahrenheit to Celsius. Write a program with...

    Convert Celsius to Fahrenheit. (Programming Exercise 2.1) or Convert Fahrenheit to Celsius. Write a program with reads a Celsius degree value from the console or a Fahrenheit degree value from the console , then converts it to Fahrenheit or Celsius and displays the result. The formulas for the conversion are as follows: Fahrenheit = (9 / 5) * Celsius + 32 Celsius = (5/9) *(Fahrenheit -32) Write this program in Java please

  • Temperature Converter Create a temperature conversion program that will convert the following to Fahrenheit: Celsius Kelvin...

    Temperature Converter Create a temperature conversion program that will convert the following to Fahrenheit: Celsius Kelvin Newton Your program should take a character which represents the temperature to convert from and a value to be converted to using the following specification: C - Celsius K - Kelvin N - Newton In addition your program should take in the following character to exit the program: X - eXit the program The numeric input for your program should be of type double....

  • Question 1 The following program asks the user for the current temperature (in Fahrenheit). Provide a...

    Question 1 The following program asks the user for the current temperature (in Fahrenheit). Provide a total of 7 valid test cases (one for each message). Do not provide invalid test cases. Add a series of if statements (if with multiple alternatives i.e. if/else if/else) so that one of the following messages is printed based on the temperature value: Temperature (F) Message >90 “Go swimming.” <=90, >80 “Turn on air conditioning.” <=80, >70 “Do nothing.” <=70, >55 “Turn on heat.”...

  • C++ optional exercise. You are going to convert temperatures in this program. You will give the...

    C++ optional exercise. You are going to convert temperatures in this program. You will give the user the choice of converting Fahrenheit to Celsius or Celsius to Fahrenheit. With the correct answer you will also display the number entered. The user should have the option to convert as many temperatures as they want. Use functions and a menu (also a function) and, assume they will convert at least one temperature. When finished upload and submit the zipped project. the formulas...

  • Fahrenheit scale is a temperate scale and it uses the degree Fahrenheit (symbol °F) as the...

    Fahrenheit scale is a temperate scale and it uses the degree Fahrenheit (symbol °F) as the unit. Celsius scale is another temperature scale and its symbol is °C. Fahrenheit is used by United States whereas all other countries in the world use Celsius. The formulas of conversion between Fahrenheit and Celsius are as follows: From Fahrenheit: [°C] = ([°F] − 32) × 5⁄9 From Celsius: [°F] = [°C] × 9⁄5 + 32 Write a function named convertTemp( degree, ForC )...

  • Design a VI to convert temperature from Degree Fahrenheit (F) to Degree Celsius (C). The VI...

    Design a VI to convert temperature from Degree Fahrenheit (F) to Degree Celsius (C). The VI accepts Fahrenheit from the user and displays the corresponding Celsius. Use the following equation: F = (9/5)*C + 32 Design a VI that computes f = 5x2 + 9y – z, where x, y, and z are real numbers. Provide two solution methods. For the first solution use only arithmetic functions (such as Square, Add, Multiply, and Subtract). For the second solution use the...

  • the formula C=5/9(F-32) is used to convert Fahrenheit temperature,F, to Celsius temperature,C

    the formula C=5/9(F-32) is used to convert Fahrenheit temperature,F, to Celsius temperature,C. What temperature in Fahrenheit is equivalent to a temperature of 100 Celsius.I know that in Fahrenheit its 212 degrees, but i used it by using guess and check. i need help with how to use the formula.

  • FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice...

    FOR PYTHON Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom functionc_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a...

  • Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The...

    Write a Python program that can convert a Fahrenheit temperature to Celsius, or vice versa. The program should use two custom functions, f_to_c and c_to_f, to perform the conversions. Both of these functions should be defined in a custom module named temps. Custom function c_to_f should be a void function defined to take a Celsius temperature as a parameter. It should calculate and print the equivalent Fahrenheit temperature accurate to three decimal places. Custom function f_to_c should be a value-returning...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT