Due Wednesday, May 18th, 11:59pm

Goals for this Warmup

  • Practice programming with strings
  • Practice data representation

For this lab, you are welcome to get technical help from another student on how to use or install any of the tools involved. You may also get syntax help on C. You may not, however, get help on the algorithmic portion of the exercise outside of office hours or piazza questions to TAs or instructors.

Set up

Remember to create a new directory in your repository for this lab.
$  cd CNET-cs152-spr-16
$  mkdir hw7
$ svn add hw7
$  cd hw7

Duet Programming Setup

Do not forget to copy your finished files from your duet repository into your personal repository.. Your duet repository is https://phoenixforge.cs.uchicago.edu/svn/cs152-spr-16-duet-X. with your own pair number in place of X. Remember to create a new directory in your repository for this lab.
$  cd cs152-spr-16-duet-X
$  svn update  
$  mkdir hw7
$ svn add hw7
$ cd hw7

You will be using the full power of repositories - repositories have two purposes: 1) backup your work in case you accidentally delete something 2) allow two people to work on different files of the same project at the same time. We will use this for both purposes.

At any given time, you should coordinate with your duet partner as to who is editing which file. Whenever your file is in a stable state (you completed something and, depending on the phases, got the compile errors out of it), you commit your file. Whenever you want to get the latest set of stable changes from your partner, you svn update.

Good luck and have fun learning together!

Problems:

In this warm-up, you are going to practice going between the binary representation of integers and different representations to print out (decimal, hex, octal, binary). You are going to perform these operations by storing the result in a provided string rather than print to a file or to the screen. This will make it easier for you to check your work. For efficiency, a character array (with unknown contents) will be passed into the functions. The functions then fill in the character array to create a string with the right representation. This means it must end with a '\0' character.

Copy these function prototypes into a head file warmup7.h.

int int_to_decimal_uns_string(unsigned int n, char *str); int int_to_decimal_sgn_string(int n, char *str); int int_to_hex_string(unsigned int n, char *str); int int_to_octal_string(unsigned int n, char *str); int int_to_binary_string(unsigned int n, char *str);

You are to write the function header in the .h and the .c files. n is the number that is going to be translated, and str is the location where the result will be placed. str has already been allocated by the caller. Do not allocate space for it. It returns the number of digits in the string representation.

If you call int_to_binary_string(17,buf); where buf is a char array with at least 33 chars, it will write 00000000000000000000000000010001 into buf and return 32.

int_to_hex_string(15,buf) will write 0F to buf and return 2.

Please note the following details:
  • When writing hexadecimal digits, letter digits must be capitalized (A,B,C,D,E,F).
  • The int_to_binary_string function must write 32 character for 32 bits (even when there are many leading zeros).
  • For the int_to_hex_string, only write as many digits as necessary to print the number. A leading 0 is required if the left-most digit has a leading 1. That is, if it is value 8 or higher. Thus, you add one to the number of digits because you added a leading 0.
  • int_to_decimal_sgn_string prints a negative sign first, then prints the digits.
Some input/output pairs. If people ask clarification for other functions, I will add more input/output pairs:
  • int_to_hex_string(7) -> 7
  • int_to_hex_string(31) -> 1F
  • int_to_hex_string(9) -> 09
  • int_to_decimal_uns_string(55) -> 55
  • int_to_decimal_sgn_string(-55) -> -55
  • int_to_octal_string(55) -> 67

Place your code in warmup7.c

You will be graded both for correctness and proper abstraction and generalization. Do not submit large blocks of similar code that have been copied, pasted, and edited. Think carefully instead and use your programming skills to write auxiliary functions that generalize computational patterns by abstracting over different inputs.

You may not use any C library calls in these functions. You must place every character in the array directly.

You will be graded on the efficiency of your solution. You should have a linear time solution that is very clean and easy to read.

Place your test cases in test_warmup7.c. Make a Makefile using prior weeks' makefiles as examples. It needs to create an executable named convert.

Testing

The above design greatly simplifies your testing. Now, to test a result, you can do the following:
char buf[50]; int_to_hex_string(31,buf); if (!strcmp(buf,"1F")) { // it is correct } else { // it is incorrect }

Submit

At this point, you should have done the following:
  • Checked out your repository
  • Created a folder named hw7 in your repository and run svn add hw7
    $ svn add hw7
  • Created , added, and filled in four files: warmup7.h, warmup7.c, test_warmup7.c and Makefile inside your hw7 directory.
  • If you participated in Duet Programming, make sure you svn update your duet repository, then copy over the files into your personal repository. The cp command copies files.
    mkdir CNET-cs152-spr-16/hw7
    cp cs152-spr-16-duet-0x/hw7/* CNET-cs152-spr-16/hw7/*
    cd CNET-cs152-spr-16
    svn add hw7
    
  • $ svn add *7* Makefile
  • Compiled your executable manually and with the Makefile
  • Executed your code to make sure it runs properly and inspected the results.
  • $ svn commit -m "hw7 warmup complete"
Now you're ready to move on to hw7!! Remember that the homework is completed individually.