CS 270
|
First read the Getting Started and Completing the Code sections 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:
Save Target As..
for each of the files.
numconv.h
(do not modify)numconv.c
(complete this file)testConv.c
(do not modify)cd
command can be used for this.
c11 -g -Wall numconv.c testConv.c -o testConv
This command performs both the compilation and linking steps. You must
run this command every time you make a change to your file.
./testConv
and read how to run the
the program. There are 6 functions which you must implement. The
testConv
program will call one of your functions depending
on the arguments that you pass. For example, if you type
./testConv i2c 2 6
, your int2char(...)
function
will be called with the parameters 2 and 6. This allows you to test one
function at a time.
You now have a functioning program. All the commands (i2c, c2i, etc.) do something. However, none will produce correct results at this point.
numconv.c
,
study the documentation found in the Files tab. You are especially
interested in the documentation for numconv.h
. Notice that this
file contains function declarations. For example:
int char2int (int radix, char digit);
A declaration lacks a function body. It simply tells the compiler the name
of the function and the parameters that it takes. The actual function body
is defined in numconv.c
. This is a common practice in C: we
have a .h file that declares the functions just to tell the compiler that
they exist, and then we have a .c file that defines them (specifies what
they do). In this exercise, numconv.h
is included by both
numconv.c
and testConv.c
. That way,
testConv.c
knows about the existence of the functions defined
in numconv.c
.
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.
numconv.c
using
your favorite editor.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.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()
make sure
this function does not print leading zeroes!frac2double()
[Extra Credit]divRem()
functiondivRem()
function computes the quotient and remainder and
returns both to the caller using two pointers. 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. This is the same way the functions in
the P1 assignment used to return the area of a figure. 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, a good exercise
is to implement it using repeated subtraction.
int2char()
: 6 lineschar2int()
: 13 linesdivRem()
: 2 lines (not the iterative solution)ascii2int()
: 3 linesint2ascii()
: 6 linesfrac2double()
: 3 linesRefer to the following videos where Andres talks about the functions and gives you tips:
int2char()
and char2int()
:
click here (make sure
to look at the corrections in the video description)divRem()
:
click hereascii2int()
:
click hereint2ascii()
:
click herefrac2double()
: no video for this function (extra credit
function)your name goes here
comment at the top of
numconv.c
.
numconv.c
to the Checkin tab on the course website, as you
were shown in the recitation.