Sunday, May 19, 2013

K&R

So I cracked open K&R.





Holy crap, they launch right into things!! They go from "hello, world" right into LET'S DO FAHRENHEIT TO CELSIUS CONVERSIONS!! HERE, HAVE SOME MATHEMATICAL EXPRESSIONS AND INTEGERS AND VARIABLES!

(My fiance just told me to be careful or I'd grow a neckbeard, doing all this coding...)

But seriously, it's really interesting how quickly they leap into these subjects. I'm not even to section three, and they're talking about floating points and everything.

The program they give as an example is pretty neat though.

#include <stdio.h>

/* print Fahrenheit-Celsius table
   for  fahr = 0, 20, ..., 300 */
main()
{
   int 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 * (fahr-32) / 9;
         printf("%d\t%d\n", fahr, celsius);
         fahr = fahr + step;
   }
}

They go into basic data types here, too, such as int, float, char, short, long, and double. I think I understand the difference between float and int... int numbers seem to be non-decimal values, while float numbers have at least six significant digits (they say something about magnitude between 10^-38 and 10^+38... I looked it up and it's a mathematical phrase in this context, so float numbers can be calculated as decimals).

There's a loop present in this program, at the line with while... Essentially, that section will continue testing whether or not the value set to fahr is less than or equal to the value set to upper. If the test returns true (yes, fahr is less than or equal to upper), the program continues with the rest of the computation. If the test returns false (no, fahr is greater than upper), the program stops.

K&R also impress the significance of indentation in programming, to assist in organization and readability. I've also found that indentation helps to show where your beginning and ending statements are. I still haven't tried that IDE, but that seems like it would help even moreso.

So the bulk of the work in this program is done by one line:

celsius = 5 * (fahr-32) / 9;

Right here, the value of celsius is calculated each time the program loops. The value of fahr is set to 0 initially, but the value of step is 20, and at the end of the loop, fahr is added to step. Also worth noting is how the equation is blown out slightly in the line above... If you don't know the formula to find Celsius from Fahrenheit by heart anymore, it is actually C = (5/9) * (F - 32). Well, back in College Algebra (or before), we should have all learned about that nifty rule that states that all values can be represented over 1. That's essentially what's happening here.

(5/9) * (F - 32)

 5       (F - 32)
---  *  ----------
 9          1

(I'm going to mention here that I failed College Algebra the first time around, and Calculus, too. This is going to be a really frustrating interesting entertaining trip for me!)

The printf section of this particular program is throwing me slightly. They describe that the appearance of  printf can altered with certain characters. The %d\t%d\n line is representing where the arguments are being substituted, and that there is to be a tab in between them. Adding , fahr, celsius to the end of that statement specifies which argument comes first, and which comes after the tab. That makes slightly more sense.

They do admit that more detail will be paid to these particular features in Chapter 7 of K&R. They actually wait until then to discuss further into formatted input with scanf. I scanned ahead in King's textbook and found scanf detailed in Chapter 2! It is really interesting seeing how different people choose to teach others about coding.

K&R also shows how detailed this particular program can become, by showing how to right-justify one side of the output, and how to make the entire program slightly more accurate by switching from integer arithmetic to floating-points.

I think I'm going to pause here, though. I love how straight-forward K&R is, but it is definitely not for the faint of heart. If it's been a while since you've had a math class, be ready to read over things multiple times and refresh your memory on particular mathematical rules before you can really understand some of their phrasing. King's book is much more down-to-earth in the first chapters, it seems.

No comments:

Post a Comment