Celsius to Fahrenheit program wont work right.. (ANSI C)

Im writing a program in C that will convert Celsius to Fahrenheit. When I use int to declare my fahrenheit and celsius numbers, my program runs fine.. but when I use floating-point to do it, it gives me some really WEIRD numbers.. heres my program and the log results.. someone help me.. I want to start writing programs in Cocoa and I cant until I master C and OBJ C..

#include <stdio.h>

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

float fahr, celsius;
int lower, upper, step;

lower = -18;
upper = 149;
step = 18;

celsius = lower;
while (celsius <= upper) {
fahr = (9.0/5.0) * (celsius+32.0);
printf ("%d\t%d\n", celsius, fahr);
celsius = celsius + step;
}
return 0;
}

Log:
0 -1070465024
0 0
0 1077018624
0 1078067200
0 1078657024
0 1079115776
0 1079410688
0 1079705600
0 1080000512
0 1080164352

Message was edited by: Fios89

MacBook, Mac OS X (10.4.10)

Posted on Oct 31, 2007 8:20 PM

Reply
16 replies

Nov 1, 2007 3:28 AM in response to Fios89

That's completely normal... When you change the type of a variable, you must change the format of your printf() !

That's the basis of C :
%d : integer with decimal radix, digits are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
%o : integer with octal radix, digits are: 0, 1, 2, 3, 4, 5, 6, 7
%x : integer with hexadecimal radix, digits are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
%u : unsigned integers with decimal radix

Those are really only for INTEGERS so : char, short, int, long

For floats, you have:
%f : float with decimal radix
%e : float with scientific representation

For doubles you add an l (ell) in front of it, like that : %lf or %le, and for long double you add an L : %Lf and %Le (watch out it's case sensitive).

Read the Apple documentation about those modifier.

Nov 1, 2007 5:00 AM in response to PsychoH13

Ok I changd the printf thing.. but its still not working right. It prints everything on the same line now and Im still not getting the values I should be getting.. I already wrote a fahrenheit to celsius program starting with 20 degrees fahrenheit (-17.8 Celsius) ending with 300 degrees fahrenheit (148.9). I dont get the values I should be getting for the Celsius to Fahrenheit program when I change it around. I should be getting 20 for my first value.. 40 for my next.. etc..

Nov 1, 2007 5:11 AM in response to Fios89

You should probably keep the "celsius" variable as int, and format your printf like so:

#include <stdio.h>
int main (int argc, const char * argv[])
{
float fahr;
int celsius;

int lower, upper, step;
lower = -18;
upper = 149;
step = 18;
celsius = lower;
while (celsius <= upper)
{
fahr = (9.0/5.0) * (celsius+32.0);
printf ("%d %f ", celsius, fahr);
celsius = celsius + step;
}
return 0;
}

You don't want to do those "step" calculations on a floating point. That can cause problems. You also don't want to do equality calculations on a floating point. Floating point values are never quite what you think they are. When you do the calculation, all of your literal values are floating point, so the compiler will automatically cast the int celsius to a floating point and store it in fahr. Then, when you print, print out fahr in floating point and celsius as int.

Nov 1, 2007 5:49 AM in response to etresoft

Well... after a few test, your program fios gives the good results... But you just doesn't give the good equation...

There your program working :


#include <stdio.h>
int main (int argc, const char * argv[])
{

float fahr, celsius; // it doesn't matter wether celsius is in float or int
int lower, upper, step;

lower = -18;
upper = 149;
step = 18;

celsius = lower;
while (celsius <= upper)
{
fahr = 9.0 / 5.0 * celsius + 32.0; // there's the good equation
printf ("%f %f ", celsius, fahr);
// the a += b operation is strictly identical to a = a + b
// but += is more explicit
celsius += step;
}

return 0;
}


To convert Celsius into Fahrenheit you have to multiply celsius by 9 then divided by 5 and then you have to add 32. That was your error, you add 32 to celsius and then multiplied by 9 fifths.

By the way, 20 °F = -6.7 °C...

Message was edited by: PsychoH13

Nov 1, 2007 6:13 AM in response to PsychoH13

None of this really makes sense.. because I wrote a Fahrenheit to Celsius program.. in floating-point.. and it works just fine.. heres the code for that.. call me crazy but if this works then why doesnt my reversal program work?

#include <stdio.h>

/* print Fahrenheit-Celsius table
for fahr = 0, 20, ..., 300; floating-point version */

int main (int argc, const char * argv[]) {
{
float fahr, celsius;
int lower, upper, step;

lower = 0; /* lower limit of temperature table */
upper = 300; /* upper limit */
step = 20; /* step size */

fahr = lower;
while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%3.0f %6.1f\n", fahr, celsius);
fahr = fahr + step;
}
}
return 0;
}

and Im sorry.. I meant 0 degrees fahrenheit is -17.8 Celsius.. I remembered my celsius conversion but forgot my initial Fahrenheit. This program converts Fahrenheit to Celsius just fine. Now WHY cant I just switch fahrenheit with celsius and change the equation to be the proper equation to convert celsius to fahrenheit?

Nov 1, 2007 6:19 AM in response to PsychoH13

PsychoH13 wrote:

fahr = 9.0 / 5.0 * celsius + 32.0; // there's the good equation


Interesting. I would have written this with parentheses just because I don't like relying on precedence:

fahr = ( 9.0 / 5.0 * celsius ) + 32.0;


However, I didn't double check the original equation because it already had parentheses. I guess the moral of the story is to always check equations. Relying on precedence (no parentheses) annoys me because then I have to think about it. But in this case, I saw the original parentheses and didn't think about it. I have to remember to check all equations, even those that appear well-formed because of parentheses.

Nov 1, 2007 6:30 AM in response to Fios89

Well I think you should review your 4th grade maths, you can't simply switch them because you don't switch numbers like that in mathematics, look at the modifications :

celsius = (5.0/9.0) * (fahr-32.0)
celsius / (5.0 / 9.0) = --(5.0 / 9.0)-- * (fahr - 32.0) / --(5.0 / 9.0)--
celsius * (9.0 / 5.0) = fahr - 32.0
celsius * (9.0 / 5.0) + 32.0 = fahr

Which gives the equation :
fahr = celsius * (9.0 / 5.0) + 32 which is completely different from fahr = (9.0 / 5.0) * (celsius + 32.0).

Myself, I don't like overusing parentheses, the precedence is standardized so in no way there's any confusion... only for the unsure people or the one that doesn't know what they are writing...

Message was edited by: PsychoH13

Nov 1, 2007 6:53 AM in response to Fios89

Well, I'm sorry if you found that insulting. However, I told you that because it's one of the basis of mathematics and that has no link with programing.
And about programing, there's a lot of good developers who never had any course about it. The fact is that you're here to get some help, and we are here to give it to you, everything freely in no way I want to insult you, but programing demands sternness.

However, if you wish, I'd be glad to help you more...

Nov 1, 2007 7:41 AM in response to Fios89

Fios89 wrote:
You did insult me by telling me to check my 4th grade maths. And you know what, I dont know what im doing with programming ok? Ive never had a programming course. Im teaching myself so I dont have any background information or foundation in programming that would of been given to me by a teacher.


I've been trolling this group for a while and PsychoH13 is quite helpful and nice to people. That being said, most programmers (the ones who post on Internet discusssion groups at least) are usually not helpful or nice.

So, asking you to review 4th grade math would probably be considered insulting in the real world. In the programming world, that's very mild scolding. One of the things you have to learn about programming is that you will probably need a thicker skin. I've been guilty of them myself far too frequently. You get excited about a particular problem you are trying to solve and forget that the code you've been trashing the past half hour was written by your boss who is standing right next to you.

It can be difficult. I used to post to some of the Apple developer mailing lists but stopped because some of those people were decidedly not nice. This forum is about as good as it gets.

Nov 1, 2007 9:31 AM in response to etresoft

I'm just chiming in here to agree with etresoft. The flat written word doesn't convey emotion well and while what PsychoH13 said was perhaps tactless I don't think he meant it with any malice. I think he may have been frustrated that he felt you didn't listen to his earlier explanation so he may have been trying to get your attention but I don't think he meant it to be insulting.

The actions of mathematical precedence and the odd truncing/rounding behavior of floats and ints is an early lesson in how computers work in ways that you don't always expect. You're learning it now and in public forum instead of from a class so your struggle is more public but that doesn't make it any different than the struggle of other beginning programers.

Programming is tough. It's tougher when you don't know what you're doing yet but you need to be willing to take your lumps to gain that knowledge. Eventually you reach the point where you see some old code you wrote and you are embarrassed for yourself. Everyone started out knowing nothing and wrote bad and embarrassing code that did horrible and unexpected things. Everyone.

Please keep that in mind when you ask people for help and advice. They are in a position to give advice because they've made these mistakes and may, many more and learned from them.

My $.02,

=Tod

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Celsius to Fahrenheit program wont work right.. (ANSI C)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.