# Beginner C Programming help

Discussion in 'Mac Programming' started by Padraic, Oct 5, 2008.

Joined:
Aug 30, 2007
Location:
Somewhere between here and there...
#1
Okay, so I'm finally getting back into programming after a loooooong hiatus. I'm starting off with the book 'The C Programming Language' by Brian Kernighan and Dennis Ritchie.

Believe it or not I'm stuck on the first chapter, not because of format or the concept of programming (I've taken java classes and done very well), but because of a formula to convert temperatures from Fahrenheit to Celsius and back.

The first exercise has me write a program to print a table converting fahrenheit to celsius. The while loop looks like this:
Code:
```	while (fahr <= upper) {
celsius = (5.0/9.0) * (fahr-32.0);
printf("%3.0f\t%6.1f\n", fahr, celsius);
fahr = fahr + step;
}```
So far so good, the formula works and the conversion is correct.

The next exercise wants me to reverse the table and do a celsius to fahrenheit conversion. My while loop looks like this:
Code:
```	while (celsius <= upper) {
fahr = (9.0/5.0) * (celsius+32.0);
printf("%6.1f\t%3.0f\n", celsius, fahr);
celsius = celsius + step;
}```
At this point I'm not worried about the formatting, so don't worry about the printf statement, I can't get the formula to work. I verified the formula from here, but it doesn't do the conversion correctly. This table says that 0 Celsius is 57.6 Fahrenheit. I can get it to work correctly if I use
Code:
`fahr = (celsius*1.8)+(32.0);`
but I'm confused why the one above doesn't work.

I'm sure I've missed something simple, does anyone have any ideas?

Thanks!

Patrick

2. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#2
Just a parenthesization error. you want to multiply 9/5*celsius first, then add. You can do this by removing all parentheses and letting order of operations take over (the division will occur first), or at least removing the parentheses around celsius + 32.0. What's happening now is that 9/5 is being evaluated first, celsius + 32.0 is being evaluated second, then the result of these two expressions is being evaluated.

-Lee

Joined:
Aug 30, 2007
Location:
Somewhere between here and there...
#3
Lee, thanks for responding so quickly. As soon as I posted I thought maybe it was the order of operations I was getting wrong. You're explanation worked like a champ...

Can you tell me, does C do order of operations differently than Java? Is this website wrong or have I just not had enough coffee and I'm misreading it?

Patrick

4. ### lee1210 macrumors 68040

Joined:
Jan 10, 2005
Location:
Dallas, TX
#4
Operator precedence is, I believe, the same in Java and C. I tried looking at the Java Language Specification, and it discusses the associativity of various operators, but doesn't give, as far as I can tell, and absolute list of operator precedence. If you'd like, you can take a look here:
http://java.sun.com/docs/books/jls/

The most relevant section contains:
This doesn't say what the levels of operator precedence are, but I imagine they are the same as C. If there are differences, it's surely with some more obscure bitwise operators, etc. and not addition and multiplication.

I'm afraid it's the latter =). That site has:
Code:
```Tf = (9/5)*Tc+32; Tc = temperature in degrees Celsius, Tf = temperature in degrees Fahrenheit
```
They have 9/5 parenthesized for clarity, but it isn't necessary as * and / have the same precedence and for those operators, associativity is left to right. I'm sure there's something similar in books, but this page has a list by precedence and associativity within a level of precedence:
http://www.difranco.net/cop2220/op-prec.htm

Note that in the example on the page you posted, Tc+32 is not parenthesized. The page might not say explicitly, but this is assuming that in the language/pseudo-code they are using to express math multiplication has higher precedence than addition. I think this is true in almost every means of expression, so what they are trying to express in RPN is:
9 5 / Tc * 32 +

Not sure if that makes the precedence clearer or not, "fully" parenthesized, in infix:
((9/5) * Tc) + 32

You need the order to be opposite when converting the other direction. That's to say, when converting from fahrenheit to celsius, you need to subtract 32 before multiplying by 5/9. That may have been the source of your confusion.

-Lee

Joined:
Aug 30, 2007
Location:
Somewhere between here and there...
#5
Lee, thanks! After reading your post, I do remember differences in how math notation is written vs coded. I also remember that Java uses the standard order of operations... Of course that was in a class I took almost 4 years ago, to date I haven't had a chance to use any of it.

I think what threw me off was that the first example (fahr - celsius) couldn't just be switched around to make it work in reverse. I did get it to work before I posted with the celsius * 1.8 + 32 formula, so in the end I would have gotten it to work... -- there's always more than one way to skin a cat, I just like to understand why one way is better than the other.