CS 101 Final Review

Review session took place on Sunday, 5-7. You can download the slides here. I also posted a brief overview of the correct answers to code samples.

Slides

The presentation is available in four formats, listed here in order of preference. Use the first listed that you(r software) can read.

ODP (requires OpenOffice). This is the original format, in which the talk was created. Note: IE may try to save this as a ZIP file. If it does, rename it to "review.odp".
PPT (requires MS Powerpoint). Some formatting may be off.
PDF (requires Adobe Acrobat). Links may not work.
SWF (requires Adobe Flash). Links definitely won't work. Only use this if you can't get any other format to work.

Code samples

The presentation points to several small code samples. Try to solve them yourself if you didn't attend the review session, or if you did but didn't have time to solve them there. The answers appear below. Those are largely from memory and may be incorrect; if you find an error, let me know. You can always download any of the samples into a file and compile and test it yourself.

Answers to code samples:

I recommend that you attempt to solve as much as you can yourself before reading the stuff below

Scope series: First, third and fourth print 5. Second and fifth (pointers) print 6.
Comparison functions: First fails, because it compares addresses rather than actual integers. Second works for ints, but may not for other data types. Third may or may not work, depending on what you need. Fifth works.
Structure series:First and third have no effect. Second and fifth effect the swap. Fourth causes a compile-time error because the dot operator is applied to a pointer. Remember: dot operator can only be applied to a structure, and arrow (->) operator can only be applied to a pointer to structure.
DMA series: First two cause segmentation faults (because the pointer was not initialized, and points to a random memory location, where you most likely don't have read/write access). Third works correctly. We didn't do the last two on Sunday, and they are probably outside the scope of the final and this class in general, but if you're curious, the fourth runs normally, but prints all zeroes (or some other random values), because the pointer, after going through the memory for writing stuff, was not reset to the beginning of the array - to the place where the zeroth element was stored - but instead now points right outside that array. The fifth example fixes this, and operates normally.
Attempts to write the recursive even function: First is a skeleton. Second causes segmentation fault, because it misses one of the two base cases (it will correctly return 1 for even values, but won't stop until it runs out of stack space for odd values; make sure you understand why). Third fixes this problem and runs correctly, but does not fit to the skeleton. Fourth uses a minor trick to fit the skeleton; it also works correctly.
We ran out of time before the pow2 example, but the sole solution given works correctly.

Questions?

If you have questions relating either to this presentation, or to the final exam in general, feel free to send me email.