1. The problem with the code is that 5 / 9 will give you the integer 0 in the C language, because 5 and 9 are both integers.
2. The way to fix it is to cast 5 as a double, like this: (double) 5 / 9.
Bryjaun R.
asked 10/31/21Consider this excerpt of code that converts a temperature from Fahrenheit to Celsius:
double fTemp = 75.2;
double cTemp;
cTemp = (5 / 9) * (fTemp - 32);
1. What is the problem with this code that will cause cTemp to be incorrect?
2. How can you fix the issue using type-casting?
1. The problem with the code is that 5 / 9 will give you the integer 0 in the C language, because 5 and 9 are both integers.
2. The way to fix it is to cast 5 as a double, like this: (double) 5 / 9.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.