I write the code for this problem but it doesn't work. Can anyone provide me some idea?
Write a program that finds the temperature, as an integer, that is the same in both Celsius and Fahrenheit. The formula to convert from Celsius to Fahrenheir is:
Fahrenheit = 9/5 Celsius + 32
Your program should create two integers variables for the temperature in Celsius and Fahrenheit. Initialize the temperature to 100 degrees Celsius. In a loop, decrement the Celsius value and compute the corresponding temperature in Fahrenheit until the two values are the same.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
usingnamespace std;
int main()
{
double C=100, F;
while (C--<101)
{
F=(9/5)*C + 32;
if (F==C)
break;
}
cout<<"The same temperature is "<<C;
return 0;
}
It should loop forever, as C starts less than 101 and never increases. You may as well put while (true) which explicitly says you want to loop forever.
This line always gives problems. I suspect it is one of the main points of this exercise.
F=(9/5)*C + 32;
9 is an integer. 5 is an integer. 9/5 does integer division resulting in 1. The effect is F = C + 32;. And that means the following condition, if (F==C) will never be true. Well, comparing floating-point types is a separate topic. I won't go too deep into that here.
This line should be
F = (9.0/5.0)*C + 32;
which will cause the numbers to be type double, giving 9.0/5.0 == 1.8.
edit: original instructions state:
Your program should create two integers variables for the temperature in Celsius and Fahrenheit.
Hence F and C should be int, not double.
But you still need 9.0/5.0 as type double.
# include <iostream>
auto CelToFah(constint tempCel)
{
return (1.8 * tempCel) + 32;//see Chervil's comments above
}
int main()
{
int tempCel = 100;
while (static_cast<int>(CelToFah(tempCel)) != tempCel)
{
--tempCel;
}
std::cout << "C == F @ " << tempCel << "\n";
}
edit: the problem pre-supposes that there is indeed a temp at which C == F but a robust program should also cover the possibility that there might not be one and stop checking at a predifined temp (in C), say -10'000 etc and report that no matches were found over given range etc
There is no a priori reason for a solution in integers.
F = 1.8 C + 32 necessitates truncation 80% of the time and I'm not sure whether cast'ing (of any form) will necessarily produce the nearest integer for both positive and negative values of the RHS.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include <iostream>
#include <cmath>
usingnamespace std;
int main()
{
int C = 100, F;
do
{
C--;
F = round( 1.8 * C + 32 );
cout << C << '\t' << F << '\n';
} while ( F > C ); // initial values and gradient ensure this must eventually end
cout << "The same temperature is " << C << '\n';
}
> It's a poor question.
> There is no a priori reason for a solution in integers.
> F = 1.8 C + 32 necessitates truncation 80% of the time ...
There is nothing wrong with the original question which stated (using integer arithmetic):
"The formula to convert from Celsius to Fahrenheit is: Fahrenheit = 9/5 Celsius + 32"
This equation ( x*5 = x*9 + 160 ) is guaranteed to have an integer solution (less than zero).
What are poor are the programs which attempted to compare the result of floating point calculations.
#include <iostream>
int main()
{
// const int SMALLEST_INT_CELSIUS = -273 ;
int celsius = 100 ; // Initialize the temperature to 100 degrees Celsius
do // in a loop
{
--celsius ; // decrement the Celsius value
/*
if( celsius < SMALLEST_INT_CELSIUS )
{
// report error: solution to the equation is below absolute zero
}
*/
// and compute the corresponding temperature in Fahrenheit
constdouble fahrenheit = celsius * 9.0 / 5 + 32 ;
// presumably print out the values
std::cout << "Celsius " << celsius << " == " << fahrenheit << " Fahrenheit\n" ;
} while( celsius != ( celsius * 9 / 5 + 32 ) ) ; // until the two values are the same
}
> The only reason that we know it has an integer solution
> is because we have already solved it by simple algebra and found it thus.
Repeat:
For any equation a.x = b.x + c, where x is the variable and a, b, and c are (edit: finite) non-zero integers,
we do not have to solve the equation to know that it has an integer solution.
> The original question states "Your program should create two integers variables for the
> temperatures in Celsius and Fahrenheit ... "
To determine the solution to such an equation in the program, it is required that we use some kind of integer variables for x and y. To ask the programmer to use non-integer variables for x and y would be downright silly.
The equation a.x = b.x + c
has solution x = c/(a-b)
This is an integer only if a-b is a factor of c. This is the case here as -4 divides a whole number of times into 160. It is not true for arbitrary a, b, c.
If the equation had been F = (9/5) C + 31
then there would have been no integer solution. Try it.
Thank you guys. I figured out the problem. This is my code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
int C=100, F;
F=(9*C)/5 + 32;
while (F!=C)
{
C--;
F=(9*C)/5 + 32;
}
cout<<"The same temperature is "<<F;
return 0;
}