Computer Science 3675
Section 001
Fall 2006
Programming Assignment 1

Assigned: Tuesday, August 24
Due: Tuesday, September 12, 11:59pm

Arithmetic on large integers can be done by storing an integer in an array, in binary notation. Each member of the array is either 0 or 1. Functions can be written to perform operations such as addition and multiplication.


Functions

Write an implementation of increment, addition and multiplication of nonnegative integers, stored in binary, in either Java or C++. Design your functions as shown in one of the following pages.

Follow the designs as shown, since otherwise your program will not link correctly with the tester.


Requirements

You are not asked to write a complete application here. Only three functions (or methods) are to be provided. They are for use in other programs. I will test them by using them in another program. Do not include a main function or method.

Your functions should meet the following requirements. (And yes, I will grade down for failure to meet these requirements, even if you have not been asked to meet such stringent requirements in the past.)

  1. Your functions must not make any assumptions about how large the arrays are, other than those explicitly stated. For example, it is unacceptable to assume that the numbers have no more than 100 bits in them. It must be possible to compile your class or functions and put them into a library, and find that they can be used for arbitrarily large integers without recompiling them.

  2. Say that a binary number is normalized if its most significant bit is 1, or if it is 0, and has 0 bits. Your functions must produce normalized results. They cannot presume, however, that their parameter arrays are normalized.

  3. Your functions must not have requirements that are not stated in the contracts. For example, for the C++ implementation, it is unacceptable to insist that array R be set to all zeros by the caller before calling sum or product, since the contract does not say anything about that.

  4. Your functions must not have any visible actions not stated in the contracts. For example, the contracts do not indicate that anything is being read or written, so nothing should be read or written. Your program cannot change any of the parameter arrays.

  5. Your functions must implement something close to the standard addition and multiplication algorithms. It is not acceptable, for example, to add x and y by starting at x and doing y increments. That is extremely slow. Similarly, it is not acceptable to multiply x and y by adding y to itself x times. That is also too slow.

    The multiplication algorithm that you learned in grade shool has you write down all of the intermediate products before adding them all up. It is not necessary to do that. Just accumulate the sum as you generate each intermediate product. Be sure to shift over an appropriate amount.

  6. Strive for simplicity and elegance in your program.


Testing the functions

Test your functions. In order to do the testing, you will want a function that prints a binary number. That is not part of the assignment, and should not be turned in, but software designers often find that they need to write extra functions to aid in testing.

If you do not test your functions, you can rest assured that they do not work, and they will fare poorly when graded. Test them thoroughly. Do not be satisfied with one or two tests.

Here are some recommended tests. You would be well advised to do others as well. All numbers are in standard binary notation (high order bit first). (x means times.)

  1. 0 + 0 = 0
  2. 0 x 0 = 0
  3. 1 + 1 = 10
  4. 1 x 1 = 1
  5. 10 + 11 = 101
  6. 10 x 11 = 110
  7. 101001 + 101111100 = 110100101
  8. 101111100 + 101001 = 110100101
  9. 101111100 x 101001 = 11110011011100
  10. 111111 + 111111111111 = 1000000111110
  11. 111111111111 + 111111 = 1000000111110
  12. 111111 x 111111111111 = 111110111111000001
  13. 111111111111 x 111111 = 111110111111000001


Submitting Your Work

Ensure that your program is on the lab computers. Log into one of the Unix computers in Austin 208, or to login.cs.ecu.edu by ssh, and run the following command to submit Arithmetic.java.

~karl/3675/bin/submit 1 Arithmetic.java
To turn in the C++ version, just change the file name to arithmetic.cpp.

You can check what you have submitted using

~karl/3675/bin/submit 1