6.s096 2013

Assignment 1

Assignments are due at midnight after the following lecture. This assignment is due on Thursday, January 10 at 11:59pm.

The companion lecture was Lecture 1: The Compilation Pipeline.

An example solution is now available.

- The Setup

Download and extract the assignment files. On Athena, that works like this:

wget http://web.mit.edu/6.s096/www/assignments/1/assignment1.tar.gz
tar zxvf assignment1.tar.gz

It contains 3 C files:

  1. fibeverse.c
  2. fibonacci.c
  3. reverse.c

And 2 header files (.h):

  1. fibonacci.h
  2. reverse.h

You can compile them with this command (though it won't work at first; see Problem 1):

gcc -Wall -std=c99 fibeverse.c reverse.c fibonacci.c -o fibeverse

You can run the resulting program with two arguments: a number, then a string (in quotes):

dynamic@mint-square:~$ ./fibeverse 6 'what a trip that was!'
was! that trip a what

The first line it prints is the 6th fibonacci number. The second line is the string you provided, with the words reversed.

- Problem 1

Unfortunately, the code doesn't compile as-is! Fix the compile errors and warnings. gcc should produce no output with the above command when you are done.

- Problem 2

I can't decide whether I want a program that computes fibonacci numbers or a program that reverses strings! ARgh! Let's modify fibeverse so that it can be compiled into either.

Use preprocessor macros we taught in class to make it so that I can choose which program it is at compile time.

When I compile it with this command, it should compute the fibonacci number but not reverse the second argument:

gcc -Wall -std=c99 -DFIBONACCI fibeverse.c reverse.c fibonacci.c -o fibonacci

Then I can run it like this:

./fibonacci 8

When I use this command, it should reverse the string I provide as the first argument, and not do any fibonacci calculation:

gcc -Wall -std=c99 -DREVERSE fibeverse.c reverse.c fibonacci.c -o reverse

Then I can run it like this:

./reverse 'a brave new world'

It should work as it originally did when I provide both compiler flags:

gcc -Wall -std=c99 -DFIBONACCI -DREVERSE fibeverse.c reverse.c fibonacci.c -o fibeverse

- Submit

Submit your version of the source files on Stellar.

That's easiest from an Athena cluster, but you can download your files from Athena too. First, compress them with a command like this:

tar zcvf assignment1-athenaid.tar.gz *.c *.h

Then you can download them with SCP. WinSCP is a good client for Windows. On Linux and OS X, you can use the scp command:

scp athenaid@athena.dialup.mit.edu:assignment1/assignment1-athenaid.tar.gz .

(Note the period at the end of the scp command!)