![]() |
Converting Temperatures
I'm using the following code to convert from Celcius to Fahrenheit but for some reason the actual conversion wont work and I can't work out why. The key lines are lines 13 and 16.
Basically, the first line tries fahrenheit = ((9 / 5) * celcius) + 32; however it's not performing the multiplication, it just adds 32 to celcius. I've tried changing it to 1.8 * celcius too but it still doesn't perform the multiplication. If anyone can figure out why then I'd be grateful. Could it just be a problem with my machine? I'm using an Intel Mac running Leopard. Thanks. :
|
Re: Converting Temperatures
Your program is working, but integer 9 divided by integer 5 = 1, (the remainder is truncated), so it's not what you want.
Try (9.0 / 5.0) and see if your compiler won't accept them as floats, that way. Otherwise you may need to explicitly cast them to floats first, and then divide. The second line of code works, because when 9 is multiplied by celcius, the 9 is being automatically re-cast to a float, since celcius is a float, already. |
Re: Converting Temperatures
Yes! Using 9.0 and 5.0 works!
Thanks a lot! |
Re: Converting Temperatures
Quote:
|
Re: Converting Temperatures
Cool
|
Re: Converting Temperatures
A bit off topic, but you could use a function to do the work
in the program if you want. Identify the formula, in this case it could be: Farenheit = Celcius X 1.8 + 32. (I think) Your function could look like this... :
float convert( float a )Let the user enter the temp in Celcius, then pass that float value as an argument to the function above, then display the 'converted' result on - screen...( %.2f will display to two decimal places, %.3f to three, etc.);):
printf("Temperature in Celcius = %.2f ", temp_celcius>BstrucT |
Re: Converting Temperatures
Quote:
should obviously return temp_farenheit in above example.My apologies. |
| All times are GMT -5. The time now is 2:14 PM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC