6.s096 2013

Assignment 2

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

The companion lecture was Lecture 2: The Core of the Language: Control structures, scope and uninitialized memory.

- Problem 1

Rewrite the program below, replacing the for loop with a combination of goto and if statements. The output of the program should stay the same. That is, the program should print out the arguments passed to it on the command line.

#include <stdio.h>

int main(int argc, char ** argv){
    for (int i = 1; i < argc; i++) {
        printf("%s\n", argv[i]);
    }
    return 0;
}

If you ran the program like this: ./prog one two three, it would print

one
two
three

- Problem 2

In lecture, we covered a varity of control structures for looping, such as for, while, do/while, goto, and several for testing conditions, such as if and switch.

Your task is to find seven different ways to print the odd numbers between 0 and 10 on a single line. You should give them names like amaze1, amaze2, etc. Here's a bonus amaze (that you can use as one of your seven if you like):

void amazeWOW() {
	int i;
	printf("amazeWOW:\t");
	for (i = 0; i <= 10; i++) {
		if (i % 2 == 1) {
			printf("%d ", i);
		}
	}
	printf("\n");
}

You may need to get a little creative!

Put all of your functions in the same C file and call them in order from the main() function. We should be able to compile your program with this command and see no errors:

gcc -Wall -std=c99 -o amaze amaze.c

- 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 assignment2-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:assignment2/assignment2-athenaid.tar.gz .

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