CS 61C (Fall 2007)

Homework Assignment 2

Two exercises, submit as "hw2_1" and "hw2_2".  Due 2:45pm before lecture 9/12/2007.

Goals

This homework is intended to give you practice working with strings and pointers and with representing integer values in various bases. It also aims to provide you with some encouragement to get started on project 1.

Reading

Sections 5.1-5.10 in K&R.

Submission instructions

For exercise 1, submit three files named "tests.in", "tests.out", and "tests.rationale" in a directory named "hw2_1". For exercise 2, submit a file named "baseconvert.c" in a directory named "hw2_2". This homework is due at 2:45pm before class on Wednesday, September 12.

This is not a partnership assignment. Hand in your own work.

Exercise 1 (2 points)

One way to test your solution to project 1 is to run the program, type commands to it, and verify by "eyeballing" that the output is correct. This approach, however, has two disadvantages:

A better testing method is to create a file of commands, use UNIX's input redirection mechanism to submit the file to your program as standard input, route the output to a file, and compare it with correct output using the diff program. For example, one might test the initialFileSystem code as follows:

input file desired output comments
    ls
    pwd
    cd ..
    ls
    pwd
		
    > > /
    > > > /
		
Carriage returns are not printed by the program. The only output produced by the program comes from the > prompt in dirmain.c and the processing of the pwd command.

Suppose that you have already processed commands that build the directory pictured below. (Files named "a" and "b" are text files; files "dir1" and "dir2" are directories.)

List, in a file named "tests.in", a sequence of commands that would thoroughly test your code for the copyFile function. (These commands would go into the test file that you would build if following the approach just suggested.) In a file named "tests.out", list the expected output. Finally, in a file named "tests.rationale", explain why your command sequence provides convincing evidence that your copyFile code is correct (e.g. what common bugs it would reveal, or what cases it covers). Place the three files in a top-level directory named "hw2_1", and submit them all together.

Exercise 2 (2 points)

Write a C program named "baseconvert.c" that takes three command line arguments: a nonnegative numeral, the radix (expressed in decimal) used to represent that numeral, and an output radix (also in decimal). Your program should print the value represented by the numeral as interpreted in the input radix, translated into the output radix. For example:
command output notes
baseconvert 43 8 10 35 43 base 8 = 35 base 10
baseconvert fff 16 10 4095
baseconvert 1001 2 7 12 1001 base 2 is 9 base 10, which is 12 base 7
baseconvert abx 36 19 1i1d abx base 36 = 10*1296 + 11*36 + 33 = 13389 base 10 = 1*6859 + 18*361 + 1*19 + 13

Constraints on your solution

  1. Your program should work with any input and output radix between 2 and 36, using the "digits" 0-9 and a-z. You don't need to error-check the arguments. Be sure to test your program on boundary cases (the highest and lowest values for each of the three arguments).
  2. You may assume that the input value fits in an unsigned int.
  3. You may not use any library functions other than printf or putchar.
  4. You may not overwrite the contents of argv.
  5. You may not use array notation (i.e. left or right brackets) anywhere in your code; work only with pointers and variables of primitive types.