Thursday, May 23, 2013

BAH!

Working on coding and writing tonight, because my fiance has a few projects he's busy with. I took more notes from K&R, but man do I still have a long way to go. No better way to put your knowledge to the test with miniature tests, like using what you've learned so far to write a program from memory.

Looking back over that little chunk of text (I wrote it out in my notebook), I made 4 terrible mistakes that would've kept the whole thing from compiling... The worst one was leaving out the variable assignment!! Aggh!!

It's okay though. Deep breaths, and lots of note-taking. I've taken to slowly working through the descriptions of the statements, going step by step until I'm certain that I understand what they're talking about. If I can keep that up, I think I'll be okay.

Anyway, for more notes, click the break!





The For Statement

K&R demonstrated a variation on the conversion table:

#include <stdio.h>
main()
{
int fahr;
for (fahr = 0; fahr <= 300; fahr = fahr + 20)
    printf("%3d %6.1f\n", fahr, (5.0/9.0) * (fahr+32));
}

This produces the same answers as the previous one, but cuts down on the amount of space it takes up.

  • It eliminates most variables, as only fahr remains and it is a simple int.
  • Upper and lower limits and step size appear only as constants in the for statement
  • Computes Celsius within printf instead of in a separate statement
  • There is a general rule that states "in any context where the value of a variable can be used, a more complicated expression for that variable can be used as well"
    • The third argument of printf must be a floating-point to match the %6.1f, so a floating-point expression can occur there
For is a generalization of While

  • The initialization fahr = 0 occurs once before the loop begins.
  • The condition fahr <= 300 controls the loop.
  • Once the condition is evaluated, the body of the loop is executed (which is the single printf command, here)
  • After the execution, the condition is re-evaluated until it becomes false. This is where the increment step comes in; if the condition returns false, the step won't execute.
The body of the loop can be a single statement, or several statements enclosed in braces. The initialization, condition, and increment can be any kind of expression.

Choosing while or for is purely situational; whichever seems to be clearer for the operations that need to be done. For is more appropriate for loops that have logically related initializations and increments. While is a better choice for readability.

The exercise for this section was to modify the temperature conversion to print the table in reverse order -- from 300 to 0. Here's what I came up with:

#include <stdio.h>
main()
{
int fahr;
for (fahr = 300; fahr >= 0; fahr = fahr-20)
    printf("%3d %6.1f\n", fahr, (5.0/9.0) * (fahr-32));
}

Also, the other direction, from Celsius to Fahrenheit:

#include <stdio.h>
main()
{
int celsius;
for (celsius = 300; celsius >= 0; celsius = celsius-20)
    printf("%3d %6.1f\n", celsius, (celsius * (9.0/5.0)+32));
}

Moving on to section 1.4 of K&R, but feeling a little tired. We'll see how far I get tonight.

No comments:

Post a Comment