CS 241 Data Organization using C
Parsing Command Line Arguments, Error Checking, and Conversion between Binary and Decimal


February 19, 2018

1 Reading Command-Line Arguments in a C Program

Many C programs created and used in both industry and in academia are designed with a command-line human interface. Kernighan and Ritchie provide a description of how to program this in section 5.10: Command-line Arguments.

2 Requirements

Write a C program, binary.c, with the usage:

usage:  
binary OPTION NUMBER  
  OPTION:  
    -b2d  NUMBER is binary and output will be in decimal.  
    -d2b  NUMBER is decimal and output will be in binary.  
 
  NUMBER:  
    number to be converted.

This program should handle numbers up to 0xFFFF and throw errors for larger numbers. Your program should have at least 2 main functions: one converting decimal to binary and the other turning binary to decimal. Both of these need to be recursive. (You can also cleverly turn them into one function, but that isn’t required for this assignment.)

In addition to writing a program, students need to diagram how their program works, using techniques shown in class. Particularly, diagram the output of binary -d2b 13. If your program does not function, output what you assume should happen given the specification.

2.1 Errors

When bad input is given, your program must print an error message.

2.2 Output Format for Binary Numbers

Every 4 binary numerals must be separated by a space. All leading zeros must be displayed. Thus, a 5-8 bit integer must display 8 bits and a 9-16 bit integer must display 16 bits.



Command Output


./binary -d2b 630011 1111


./binary -d2b 1 0001


2.3 Output Format for Decimal Numbers

Decimal numbers must be formatted with comma thousand separators. For example, the largest unsigned 8-bit number is 11111111 (base 2) = 255 (base 10).



Command Output


./binary -b2d 11111111255


./binary -b2d 1111 15


./binary -b2d 11 3


Similarly, the largest unsigned 16 bit number (11111111111111111  ) is 65,535 in base 10. This contains 5 digits plus a comma. characters:



Command Output


./binary -b2d 111111111111111165,535


WARNING: ANSI C does not include a format option for automatically adding comma thousand separators nor does the gcc used on moons.unm.edu. This format option is standard in C++ and in many expanded versions of C. If you are writing your code using a different C compiler and turn in code with such an option specified, your program will most likely not compile when tested and you will get a zero for the assignment.

It is not that hard to write your own code for outputting numbers with comma thousand separators. However, this is such a common task that a quick Google search should return a solution in ANSI C. It is ok for you to use such code. If you do use any code you did not write, be sure to avoid plagiarism by giving proper credit for the source.

Hint: You might find it helpful to use the strtoul function of <stdlib.h>. This function can convert a string of digits into an unsigned long value.

3 Turning in your assignment

Turn in the code (binary.c) through UNM Learn under Lab 5. You may turn in the written work by doing one of the following:

  1. Scanning it, and then turning it in on UNM Learn under Lab 5 named “binary˙diagram”. You may turn in a pdf, png, gif, txt, or jpg file. If you create this through word or a similar program, you need to convert your file to a pdf before submitting.
  2. Turning in a physical copy to the TA during lab.

No other methods of turning in this assignment will be accepted.

4 Grading Rubric (total of 29 points)

-2 points:
The program does not start with a comment stating the students first and last name and/or the source file is not named correctly.
-2 points:
Program compiles with warnings on moons.cs.unm.edu using /usr/bin/gcc with the -Wall -ansi -pedantic options (and options for any standard libraries you may want to use such as -lm for math.h). If you do use a standard library that requires a linking option, please include this as a note when you submit in Learn.
-8 points:
Functions turning binary to decimal/decimal to binary are not recursively defined.
3 points:
All code follows the CS 241 standards (including comments).
18 points:
Passes 18 known test cases: a point each. The test file is testbinary.sh and the output file is testbinary.out. Note, unlike past test files, this test file is not a data file to be input into your program by redirecting the standard input stream. The file testbinary.sh is a Linux shell script file (it is a text file containing a sequence of Linux commands). Note: for this shell script to run, it must have execution permission turned on:

chmod u+x testbinary.sh

Also, the script expects your executable to be named binary (not a.out), so make sure you specify that when you compile.

gcc -Wall -ansi -pedantic -o binary binary.c

I've provided you with a simple Makefile that uses these options so you won’t have to keep typing them.

8 points:
Diagramming how the program works correctly, including all the appropriate frames.