Tuesday, February 15, 2011

Temperature Conversion - The C Way

#include <stdio.h>

#define LOWER 0 /* lower limit of table */
#define UPPER 300 /* upper limit */
#define STEP 20 /* step size */

/* print Fahrenheit-Celsius table */

main()
{
        int fahr;

        for (fahr = LOWER; fahr <= UPPER; fahr = fahr + STEP)
              printf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}

Using Symbolic Constraints we are not throwing magic numbers into the program. This method makes it a lot easier to hand this code off to another programmer he can jump right into editing it even without a synopsis of what it does. Not nearly as complicated as the encryption program but this is where I am at so its cool. I am almost on chapter two of my C Programming book!

44 comments:

  1. Hey! Nice stuff, never managed to learn C++, good job.

    ReplyDelete
  2. You should learn fortran. Its seriously the most useful programming out there I think. I don't get why more people don't use it.

    ReplyDelete
  3. That is a very basic program... you should add it conversion to Kelvin.

    ReplyDelete
  4. How do I go about making a simple program out of this in DOS or something. I'm constantly googling temperature conversion rates and it's pathetic lol

    ReplyDelete
  5. how many chapters you got left in your C programming book?

    ReplyDelete
  6. i certainly remember doing something similar to this in first year classes, fun times :)

    ReplyDelete
  7. Good info, I gave up on programming haha.

    ReplyDelete
  8. Delicious, it's always a pain to try and convert it yourself.

    ReplyDelete
  9. I have always wanted to learn a programming language. I hear C/C++ are quite popular. I use a lot of applications but to truly appreciate the effort put into these I would need to learn how to code.

    ReplyDelete
  10. I tried to teach myself C one time. I kinda lost interest after 3 days, I kind of burned myself out though tbh, I didn't do anything else.

    ReplyDelete
  11. Ah the wonders of CS. I was forced to learn this weird language called netlogo back in high school lol

    ReplyDelete
  12. This could actually be very useful, thanks for sharing.

    ReplyDelete
  13. ohh this gave me a headache at uni last year... nice blog ;)

    ReplyDelete
  14. hmmm....all I know is some highschool PASCAL, but it still makes some sense to me.

    ReplyDelete
  15. Gaahh, I love how the pre-processor does any math it can before compiling

    ReplyDelete
  16. Good place to start, once you get a hand of arrays and such you'll be creating all sorts of cool stuff.

    ReplyDelete
  17. Hear anything about C#? It's fairly new, but I don't know much about it.

    ReplyDelete
  18. nice job dude!! im going to save the code..thanks

    ReplyDelete
  19. keep it up, u'll catch up in no time, C is the very base of a programmer

    ReplyDelete
  20. awesome stuff thanks for sharing!

    ReplyDelete
  21. Thanks for sharing, need to brush up on my coding.

    ReplyDelete
  22. I'll show this to my brother. It will benefit him more than myself. This goes straight over my head hah.

    ReplyDelete
  23. Nice share, I think I'm gonna start learning how to program more

    ReplyDelete
  24. nice job.

    Haven't compiled any code in a while so I'll have to try this out.

    ReplyDelete
  25. Im learning C# at the moment, would love to see any kind of post about it

    ReplyDelete
  26. Damn americans making shit overly complex. Use degrees!

    ReplyDelete
  27. :D I'm learning C programming @ school, Followed!

    ReplyDelete
  28. It seems so simple, but this would take me quite awhile to figure out.

    ReplyDelete
  29. Yesss a blog with C on it.

    Following,
    U Laugh U Lose
    http://ulaughulose.blogspot.com/

    ReplyDelete
  30. I don't completely understand this, but I'm still learning!

    ReplyDelete
  31. great stuff, need this on a phone app!

    ReplyDelete
  32. I wish I could get computer programming easier

    ReplyDelete
  33. Cool stuff! I'm learning really basic programming in class right now. Hope to learn C someday. Good luck! New Follower!

    Follow me at
    http://finallytwenty-one.blogspot.com/

    ReplyDelete
  34. Bro, your shit is hot and your code is Tight!! I'm following you in the hopes of absorbing some of that skillful goodness.

    ReplyDelete