CS208 Guest Lecture: Mastering C

Who am I?

I am currently a faculty member in Computer Science at the University of British Columbia. I just started in September, but next year I'll be teaching a course strikingly similar to this one. Prior to joining UBC, I was on the faculty at Harvard for 25 years and the last few years, I taught a course quite similar to this one (CS61). I love teaching undergraduates and particularly getting students excited about the parts of a system that are hidden from most people. And, I really love C! (I also love creating interesting, 100% edible cakes. If you want a different scavenger hunt, find my cake collection on my web site.) I'm also always looking for great graduate students, so if you ever find yourself contemplating graduate school, I hope you'll think back on this class and get in touch.

Today's Learning Objectives

Things I am assuming

In-class Exercise

We're going to send you on a bit of a scavenger hunt -- inside a program! You can find the program source code here. And to make things easy to compile, here is a Makefile. (You should be able to place both these files in the same directory and and then just type "make" to produce the program treasure.) If that doesn't work, let us know so we can get you unstuck.

Some handy info about the treasure program

Your Mission

Pick a partner and begin exploring! Run the program with different parameters to find the items in the list below. There are two or more different ways to produce most of these results. How many can you find?

If you find them all, we will be impressed, but then feel free to tinker with the program and add new treasures! Trade them with other groups.

Getting Started

  1. Find “sixty-one” using ./treasure 6. (If you run ./treasure 6 without arguments, it prints “6”. Can you find other arguments so that it prints something like “sixty-one”?)
  2. Find “sixty-one” using ./treasure 5.
  3. Find an expired cow using ./treasure 0.
  4. Find an expired cow using ./treasure 12.
  5. Find a precious stone using ./treasure 21.
  6. Find a precious stone using ./treasure 14.
  7. Find a greeting cat using ./treasure 16.
  8. Find a greeting cat using ./treasure 2.

Tips

We’ve hidden treasure in the program in many different ways. You’ll make progress by looking at the code, by reasoning through its behavior, and, importantly, by running experiments. You can even change the code to see what happens! (But your answers should work on the original code.) Also use the Web liberally. Don’t understand a term? Search for it!

Don’t be afraid to treat the code like a scientist: run experiments! We often try to understand code by reading it, but that can be difficult, even for simple loops. If you get stuck, try instead to get a feel for a function by observing its behavior. So try running a treasure function with different arguments. What happens? Use the results to develop a hypothesis about its behavior. Then check your hypothesis against the code, and confirm or refute it by running the code with different arguments.

Acknowledgements

Thanks to my brilliant colleague, Eddie Kohler, for inventing this assignment and writing the code.