CS 202 - Homework 2

Due: Monday 2/26, at the beginning of class

This assignment has a written part and a programming part. Please type your solutions to the written problems and save them in PDF format as hw2_written.pdf. Don't forget to include your name.

Submit your files by the beginning of your class section using the HW 2 submission page.

  1. Martin, chapter 2, problems

  2. Write two C programs to convert a number from any base to decimal and vice versa. For full credit, use nested multiplication and repeated division as discussed in class (see also Martin 2.1.3). Skeletons for both programs are provided in my directory ~schar/cs202/hw2/ on basin: Sample executables are provided in the same directory, but these are restricted to bases 7 and 13. To run these, you'll have to ssh to basin. Once your program works, you should make sure that you get the same output for these bases. (Of course, test your program for other bases as well!)

    If you are working on the lab machines, you can copy my directory 'hw2' into your cs202 directory (don't omit the "." in the second command -- it means "current directory"):

    basin:~$ cd cs202
    basin:~/cs202$ cp -r ~schar/cs202/hw2 .
    basin:~/cs202$ cd hw2
    Submit your source code for each program using the link above. Make sure your name is at the top! In addition, please submit the output your programs produce for each of the test cases below. Copy and paste your session from the terminal window into a text file hw2_output.txt.
    ./baseB2dec 2 1001
    ./baseB2dec 3 12021
    ./baseB2dec 8 177
    ./baseB2dec 16 177
    ./baseB2dec 16 abc
    ./dec2baseB 13 2
    ./dec2baseB 13 16
    ./dec2baseB 255 16
    ./dec2baseB 1001 11
    ./dec2baseB 16137787 31