Warning! This site is under development.
This class will be recorded! Its use will be limited to members of the class. Please do not share with others.
Approximate overview
Remember to check the eboard for these tasks. I’ll try to get them in the syllabus, but I am imperfect and behind, on my best days.
Readings on Unix
Readings Sam hasn’t written yet
Readings on binary search in C (optional)
Fun tasks
cat or less) that do something fun, interesting, or surprising.Did you figure out why indent is not working correctly?
Nope.
I own my code.
Yes, you do.
I’d prefer that you post it with my name.
That’s fine. But let me tell you a story.
Can we return to the binary search examples?
Certainly. It’s up a little bit after we’re done with Q&A.
As many of you know, there are times we want to “comment out” code to make it unavailable to the compiler. What are the techniques you know?
There are a lot of reasons we comment.
The one comment in the code Sam was stuck translating.
(define byte 8) ; bits
How do we “comment” or “comment out” code in C? (How do you tell the compiler to ignore text you’ve inserted in the file?)
// (double forward-slash). Comments out everything from
there to the end of the line./* to open a comment and a */ to end the comment and
everything in between is ignored.
/* ... */
comment./* ... */, don’t use
those for “shorter” comments./* ... */ in general.Detour
int *vals = malloc(10 * sizeof(int)); val -= 1;Back from detour. How else do we comment out code?
The C Preprocessor has a wonderful feature where you can include code only in certain situations.
#ifdef NAME
code
#endif // NAME
/* */ style.git
a good version control system. mercurial, svn, etc. are all
good enough for those purposes.)ifdef is not designed for commenting out code; rather, it is
designed to allow you to choose whether or not to include some
code.int
main (int argc, char *argv[])
{
#ifdef GREET
/* Print a greeting. */
printf ("Hello\n");
#endif
/* Print a goodbye. */
printf ("Bye\n");
} // main
Compiling and running with and without the greeting.
$ cc -DGREET silly.c -o silly
$ ./silly
Hello
Bye
$ cc silly.c -o silly
$ ./silly
Bye
History: We tried to write binary search on Tuesday. We generally failed. We could tell we failed because the awesome Bentley-inspired test suite caught errors. Amazingly, even though one procedure was recursive and one was iterative, both failed on a similar input.
So let’s figure out why …
5 min
nWhoops. Harder than we thought. Some of us spent too long.
Note: Insertion sort is your friend.
_Only two submissions. So sad. Which should we critique? _
This example is adapted from Kernighan and Plauger.
What does this do? (TPS)
for (int i = 1; i <= ROWS; i++)
for (int j = 1; j <= COLS; j++)
M[i-1][j-1] = (i/j)*(j/i);
How should you write it if you wanted to be clear?
To illustrate my point that understanding memory in C is important, let’s continue with a problem that a friend gave to me a while ago. He showed me the following fragment of C code.
x = malloc (...);
foo ();
bar ();
free (x);
The program was crashing on the call to free.
Here are some things they discovered.
free, the program ran through to completion.free before the call to bar, the program ran
through to completion.free in bar.What is likely to be wrong with their code? How would you trace the error?
TPS
Taken from Task 2b.
How do you learn *nix tools?
lsNo, that’s not “ones”.
ls -l, ls -a, ls -R, ls -tmanEveryone’s favorite gendered command.
man command - basic information on a commandman SECTION command - gives information from a particular section
section 1 is command-line tools, 3 is library functionsman -k term - Looks up all the man pages with the given term in their short descriptionduWhat does du do?
du -h DIR tells you how much is used by DIR and everything within it.grep (or egrep)Grinnell’s excellent program
grep PATTERN FILESgrep -R PATTERN DIRgrep -l PATTERN FILEStrIs that pronounced “tiara” because it’s the crown of commands?
tr 'SOURCE SET' 'TARGET SET'tarA sticky substance. Also “tape archive”.
uniqKind of like Unix, I think.
sortI wonder what this is an abbreviation for.
head and tailCan you write Scheme in the shell?
cutSounds dangerous.
killSounds even more dangerous.
killall teamswhichNo, not witch.
fileLearn about file types.
Let’s pretend that Sam has not reread Raymond in five years, and that half of your classmates neglected to do the reading. What are the key takeaways from the Raymond reading?
This example is adapted from Kernighan and Plauger.
What does this do? (TPS)
for (int i = 1; i <= ROWS; i++)
for (int j = 1; j <= COLS; j++)
M[i-1][j-1] = (i/j)*(j/i);
How should you write it if you wanted to be clear?
This one will be fun, because I’ll need to figure it out again.
To illustrate my point that understanding memory in C is important, let’s continue with a problem that a friend gave to me a while ago. He showed me the following fragment of C code.
x = malloc (...);
foo ();
bar ();
free (x);
The program was crashing on the call to free.
Here are some things they discovered.
free, the program ran through to completion.free before the call to bar, the program ran
through to completion.free in bar.What is likely to be wrong with their code? How would you trace the error?