CS U216 -- Algorithms and Data Structures for
Engineers
Assignment 4
Due 1 June, 2005
Assignment 4 involves writing functions that may be used in modelling
a simple electronic arithmetic circuit. In this assignment, C++ values
of type bool will be used extensively. The bool value true
should be considered equivalent to the mathematical number 1, and the
bool value false should be considered equivalent to the
mathematical number 0.
Your program must contain the following items:
- A global constant REGSIZE equal to 32.
- Two global arrays called reg0 and reg1.
These should be arrays of bool, and should have REGSIZE elements.
- A global variable called carry of type bool.
- A function with the prototype:
void load(int x)
This function should accept a non-negative argument x, and
fill the array reg0
with the binary expansion of x.
The least significant bit of x should be stored in reg0[0].
For example. The function call load(3) should
set reg0[0] and reg0[1] equal to true, and
all other elements of reg0 equal to false.
- A function with the prototype:
int fetch()
This function should return an int value which corresponds to the
binary number represented by the values in the elements of
reg0. If x is a non-negative int,
then x == fetch() is a postcondition for
load(x)
- A function with the prototype:
void writeReg()
This function should output each element of reg0
to cout in order of highest index to lowest index.
The output should be on a single line. Thus, if
reg0[0] and reg0[1] are equal to true, and
all other elements of reg0 are equal to false, then
a call to writeReg() should display the following on the console:
00000000000000000000000000000011
- A function with the prototype:
void swap()
This function should assign the elements that are in
reg0 to their corresponding locations in
reg1 and vice-versa.
- A function with the prototype:
void complement()
This function should change each element of
reg0 to its negation. That is, if
reg0[0] has the value true before the
function complement() is called, it should have the
value false afterward.
- A function with the prototype:
bool fullAdd(bool x, bool y)
This function adds the values of x, y and
the global variable carry,
and returns true (1) if the result of the addition is
odd, and false (0) if the result of the addition is even.
This function should also set the value of the global
variable carry to true (1) if the result of the addition is
greater than 1, and to false (0) otherwise.
- A function with the prototype:
void add()
This function should add the least significant elements of
reg0 and reg1, that is
reg0[0] and reg1[0],
together with the carry
using the fullAdd() function, and place the result in
reg0[0]. The function should then repeat the process
with the next pair of elements of reg0 and reg1,
and so on. The result should be that
the bits corresponding to the sum of reg0, reg1 and carry
will be computed and assigned into the elements of reg0,
and the value of carry should indicate whether
there was an unsigned overflow.
- A function with the prototype:
void loadCarryBit(bool x)
This function assigns the value x to the global variable carry.
- Driver functions which allows a user to unit test each of
the above functions.
- EXTRA CREDIT: Write a function with the prototype:
void subtract()
This function should subtract the value of reg0 from reg1,
and place the result in reg0. To get credit for this function,
it must consist entirely of calls to the above functions. Also, you must provide
a test driver for it.
Last updated 25 May 2005.