Programming due Sunday, Jan. 31 at 10:00pm, late deadline at 11:59pm.
This assignment has four objectives:
To learn how to to compile multiple C files into an executable program.
To demonstrate that many of the Java programming constructs you learned are
valid in C. (Java and C++ syntax were based on C).
To learn how to convert between human readable numeric values and internal
numeric values and vice-versa.
To become comfortable working in various number bases.
To learning a little about C pointers.
To learn simple iteration through a C string.
First read the Getting Started section below and then study the
documentation for numconv.h in the Files tab to understand the
details of the assignment. In addition, these two references provide
additional information:
Number Systems by Dr Sanjay Rajopadhye. Includes section dealing with
fractions.
Create a directory for this assignment. A general scheme might be
to have a directory for each CS class you are taking and beneath that,
a directory for each assignment. The name of the directory is
arbitrary, but you may find it useful to name it for the assignment
(e.g. P2).
Copy the three files into this directory. It is easiest to right click on
the link, and do a Save Target As.. for each of the files.
In the terminal type ./testConv and read how to run the
the program.
You now have a functioning program. All the commands work.
However, none will produce correct results at this point.
Completing the Code
Before attempting to write any of the functions of numconv.c,
study the documentation in found in the files tab.
Plan what you need to do before writing code.
The best way to complete the code is to follow a
write/compile/test sequence. Do not attempt to write everything at
once. Rather, choose one function and do the following steps.
Write some/all of one function in numconv.c using
your favorite editor.
Save your changes and recompile numconv.c using
the command shown above. You may find it convenient to work with
both a terminal and editor window at the same time.
Repeat steps 1 and 2 until there are no errors or warnings.
Rebuild the executable using the link command shown above.
Test the function you have been working on. Do not attempt to
move on until you complete and thoroughly test a function. You
will find that the function int2char() is called in
the int2ascii() function and that char2int()
is useful in ascii2int(). Therefore, you might write the
functions in the following order:
int2char()
char2int()
divRem()
ascii2int()
int2ascii()
ascii2double() [Extra Credit]
Repeat steps 1 thru 5 for the remaining functions.
Iterating thru a C string
In C, a string is an array of characters that is terminated with a character
having a value 0 (i.e. its ascii value is 0). Compare this to the ascii value
0x30 which represents the character '0'. You will learn that there is a
relationship between pointers and arrays later in the course. For now, simply
understand that if you are given a string (char* string) that you
may examine each of the characters with the code:
for (int i = 0; string[i] != 0; i++)
Later you will learn how to dereference the pointer and increment it. You will
need to do this in the ascii2int()/ascii2double() functions.
The divRem() function
This function gives you a simple introduction to pointers. The function
computes the quotient and remainder and returns both to the caller. The
result is that the function is able to "return" multiple values to the
caller. The caller passes pointers to the addresses where it wants the results
stored and the function uses the dereference operator to store the values.
Review your notes from class and study how pointers are used in
testConv.c.
You may implement this function however you want, including using the C
operators for division and modulus (/ %). However, you may
want to implement it using repeated subtraction as this will give you a
pattern to follow later in the course when you implement this functionality in
LC3 assembly language.
Specifications
Your program must meet the following specifications:
Work on your own, as always.
The name of the source code file must be exactly numconv.c.
Name the file exactly - upper and lower case matters!
Comments at the top as shown above.
Make sure your code runs on machines in the COMCS 120 lab.
Submit your program to the Checkin tab as you were shown in the recitation.
Read the syllabus for the late policy.
We will be checking programs for plagiarism, so please don't copy from anyone else.
Grading Criteria
100 points for perfect submission.
0 points for no submission, will not compile, submitted class file, etc.
Each test can make multiple calls to the function being tested, with different values.
Preliminary Tests
testCompile: checks that program compiles. (10 points)
test1: calls int2char with radix 10 and a value between 0..9. (5 points)
test2: calls int2char with radix 16 and a value between 10..15. (5 points)
test3: calls int2char with radix 16 and the value 16, an error case (2.5 points)
test4: calls char2int with radix 10 and a digit between '0'..'9'. (5 points)
test5: calls char2int with radix 16 and a digit between 'A'..'F'. (2.5 points)
test6: calls char2int with radix 16 and a digit between 'a'..'f'. (2.5 points)
test7: calls char2int with radix 16 and the digit '&', an error case. (2.5 points)
Final Tests
test8: calls divRem with radix 2 and an arbitrary value. (5 points)
test9: calls divRem with radix 10 and an arbitrary value. (5 points)
test10: calls divRem with radix 16 and an arbitrary value. (5 points)
test11: calls ascii2int with a string representing a hexadecimal value. (10 points)
test12: calls ascii2int with a string representing an octal value. (5 points)
test13: calls ascii2int with a string representing a binary value. (5 points)
test14: calls ascii2int with a string representing a base 6 value. (5 points)
test15: calls int2ascii to convert an integer to a hexadecimal string. (10 points)
test16: calls int2ascii to convert an integer to an octal string. (5 points)
test17: calls int2ascii to convert an integer to a binary string. (5 points)
test18: calls int2ascii to convert an integer to a base 6 string. (5 points)
test19: tests ascii2double to convert a base 10 string to a double. (2.5 points, extra credit)
test20: tests ascii2double to convert a base 2 string to a double. (2.5 points, extra credit)
test21: tests ascii2double to convert a base 16 string to a double. (2.5 points, extra credit)
test22: tests ascii2double to convert a base 6 string to a double. (2.5 points, extra credit)
Final grading includes the preliminary tests.
Submit the single file numconv.c to the Checkin tab on the course website, as you
were shown in the recitation.