Programming Forums
User Name Password Register
 

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

Reply
 
Thread Tools Display Modes
Old Jul 5th, 2005, 4:38 PM   #1
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4 linuxpimp20 is on a distinguished road
Having trouble with an exercise in the C book.

Im having trouble with an exercise in the C book written by the inventors of C. Normally i don't like people who post code and ask what is wrong with it but i've been working on this since this morning with no success. I don't have much choice because the C book doesn't have the answers in the back of the book. Im not doign this for a class im interesting in learning C and that is why i want to know what im doing wrong. Hoping someone can look at it because the more i look at it the more it just blends together and i can't pick out what is wrong. In a previous exercise i wrote a program that converts fahrneheit to celsius and displays the conversions from 0 to 300 in steps of 20. Now it is asking for me to have it display the output going from 300 to 0. this is what i have:

main()
{
int fahr;

printf("Fahr\t Celsius\n");
for (fahr = 300; fahr >= 0; fahr = fahr - 20);
printf("%3.0f \t%6.1f\n", fahr, (5.0/9.0) * (fahr-32.0));

}

here is the output i get:

Fahr Celsius
0 0.0

i wouldn't have posted if this thing hasn't frustrated me so much because i know it is just something simple im overlooking. thanks for any replies in advance.
linuxpimp20 is offline   Reply With Quote
Old Jul 5th, 2005, 4:52 PM   #2
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
main()
{
    int fahr;

    printf("Fahr\t Celsius\n");
    for (fahr = 300; fahr >= 0; fahr = fahr - 20);
      printf("%3.0f \t%6.1f\n", fahr, (5.0/9.0) * (fahr-32.0));

}

For starters, get rid of semicolumn, since you want to have printf inside the loop.
I would always put {} around loops and other control structures, although in this case it does not matter (since it's just one statement).
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics

Last edited by EverLearning; Jul 5th, 2005 at 5:11 PM.
EverLearning is offline   Reply With Quote
Old Jul 5th, 2005, 5:02 PM   #3
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
how about something like this:

#include <stdio.h>


int main(int argc, char* argv[])
{
     int fahr;

     printf("Fahr\t Celsius\n");
     for (fahr = 300; fahr >= 0; fahr = fahr - 20)
          printf("%i \t %6.1f\n", fahr,  (5.0/9.0) * (fahr-32.0));

     return 0;
}
-EDIT
(double check what is typed in the book! I would not try to cast an int to FLOAT implicitly by passing it to the printf function... if necessary, do an explicit cast with (FLOAT) ) :

printf("%3.0f \t %6.1f\n", (float)fahr,  (5.0/9.0) * (fahr-32.0));
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty

Last edited by stevengs; Jul 5th, 2005 at 5:27 PM.
stevengs is offline   Reply With Quote
Old Jul 5th, 2005, 5:17 PM   #4
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
being picky it would be a float.
EDIT: if in printf() you use %f that is.
Oh, whatever....
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jul 5th, 2005, 5:25 PM   #5
stevengs
Professional Programmer
 
stevengs's Avatar
 
Join Date: May 2005
Location: Bad Nauheim, Germany
Posts: 436
Rep Power: 4 stevengs is on a distinguished road
right... I beg your pardon - %lf must be double then... ( unless you are using a nonconforming M$ , which I have for waaay to long... (4 months, hehe ))
__________________
-Steven
"Is this a piece of your brain?" - Basil Fawlty

Last edited by stevengs; Jul 5th, 2005 at 5:38 PM.
stevengs is offline   Reply With Quote
Old Jul 5th, 2005, 5:32 PM   #6
EverLearning
Hobbyist Programmer
 
EverLearning's Avatar
 
Join Date: May 2005
Location: Indiana
Posts: 130
Rep Power: 4 EverLearning is on a distinguished road
Actually I think you can do it with %f as well, my bet here.
This I found helpful before (...'coz I often forget this stuff!), took several minutes to find it on google: table
So, the only difference between float and double is the size then.
__________________
got math? yumm...

"All men by nature desire to know" -Aristotle, Metaphysics
EverLearning is offline   Reply With Quote
Old Jul 6th, 2005, 7:22 AM   #7
linuxpimp20
Hobbyist Programmer
 
Join Date: May 2005
Location: ma
Posts: 130
Rep Power: 4 linuxpimp20 is on a distinguished road
thank you that was my problem that i needed to cast fahr. that initially made me feel like something was arwy with the int and the printf being %f. I've spent most of my time learning about C++ so i still need to get used to printf function where you have to specific everything. sorry for not posting sooner but the post weren't getting sent to me via email.
linuxpimp20 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




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

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