Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 17th, 2008, 5:45 AM   #1
gj15987
Programmer
 
Join Date: Feb 2005
Posts: 47
Rep Power: 0 gj15987 is on a distinguished road
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.

c Syntax (Toggle Plain Text)
  1. int main()
  2. {
  3. float celcius; /* Declare only */
  4. float fahrenheit = 0; /* Declare and initialise */
  5.  
  6. printf("Enter the temperature in degrees celcius: ");
  7.  
  8. /* Read a value from the user and store it in celcius. */
  9. scanf("%f", &celcius);
  10.  
  11. /* Perform the conversion. */
  12. // This one wont work
  13. fahrenheit = ((9 / 5) * celcius) + 32;
  14.  
  15. // But this one does
  16. //fahrenheit = ((9 * celcius) / 5) + 32;
  17.  
  18. printf("Temperature is \t%f 'C\n and \t\t%f 'F\n", celcius, fahrenheit);
  19.  
  20. return 0;
  21. }
gj15987 is offline   Reply With Quote
Old Jun 17th, 2008, 6:31 AM   #2
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 58
Rep Power: 0 Adak is an unknown quantity at this point
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.
Adak is offline   Reply With Quote
Old Jun 17th, 2008, 6:45 AM   #3
gj15987
Programmer
 
Join Date: Feb 2005
Posts: 47
Rep Power: 0 gj15987 is on a distinguished road
Re: Converting Temperatures

Yes! Using 9.0 and 5.0 works!

Thanks a lot!
gj15987 is offline   Reply With Quote
Old Jun 17th, 2008, 8:01 AM   #4
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
Re: Converting Temperatures

Quote:
Originally Posted by Adak View Post
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.
A conversion/cast is not required. Simply use 9.0f/5.0f (or 1.80f) to have literals that are of type float.
grumpy is offline   Reply With Quote
Old Jun 17th, 2008, 8:33 AM   #5
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 205
Rep Power: 0 BstrucT is on a distinguished road
Re: Converting Temperatures

Cool
__________________
"Common sense is the collection of prejudices acquired by age eighteen." - Albert Einstein
BstrucT is offline   Reply With Quote
Old Jun 17th, 2008, 9:19 AM   #6
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 205
Rep Power: 0 BstrucT is on a distinguished road
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 )
{
     float temp_farenheit = ( (temp_celcius * 1.8) + 32 );

     return (0);
}

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
printf("Temperature in Farenheit = %.2f ", convert( temp_celcius );


>BstrucT
__________________
"Common sense is the collection of prejudices acquired by age eighteen." - Albert Einstein
BstrucT is offline   Reply With Quote
Old Jun 18th, 2008, 12:51 AM   #7
BstrucT
Hobbyist Programmer
 
BstrucT's Avatar
 
Join Date: Dec 2007
Location: Durban, South-Africa
Posts: 205
Rep Power: 0 BstrucT is on a distinguished road
Re: Converting Temperatures

Quote:
Originally Posted by BstrucT View Post

Your function could look like this...
float convert( float a )
{
     float temp_farenheit = ( (temp_celcius * 1.8) + 32 );

     return (0);
}
Sorry guys, I was being dumb again, the function shouldn't return 0 , it
should obviously return temp_farenheit in above example.

My apologies.
__________________
"Common sense is the collection of prejudices acquired by age eighteen." - Albert Einstein
BstrucT is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Excell and MySQL - converting data MiKuS Other Web Development Languages 0 May 4th, 2008 12:32 AM
Help in Converting C# to Java bearvsgodzila Java 1 Jan 13th, 2008 12:36 PM
converting double to string dark_omen Java 3 Dec 8th, 2005 4:11 PM
Converting javascript to Visual Basic.NET emdiesse Visual Basic .NET 4 Nov 21st, 2005 9:52 PM
Converting ASM to 86000 Brom02 Assembly 0 Apr 28th, 2005 1:48 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:04 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC