Programming Assignment 1: Binary Arithmetic
Due Date: Wednesday, 02/05/03
Objectives: To test and
refresh your knowledge of functions, file i/o, array manipulation, loops, and conditionals.
Problem Specification
Write a program that simulates unsigned 8-bit binary addition and multiplication. In
writing your program, you may make the following assumptions:
- You may assume that all operands, when represented in binary, contain eight bits or fewer (each bit
is either 0 or 1).
- You may assume that all sums or products (i.e. solutions) contain eight bits or fewer. That is, you may
assume that input will be chosen so that overflow will not occur during any calculation.
Implementation Notes
You can use only functions in this program, not classes.
Your program will read input from a text file, test.txt, which has been
provided for you. The test file contains five arithmetic problems. Each problem is formatted as follows:
operand1
operand2
operator
For example, the first problem in the test file is
12
10
*
Your program must convert each operand to an 8-bit binary number, carry out the specified operation (either binary addition or
binary multiplication), and convert the solution (itself an 8-bit binary number) to decimal. The decimal solution should then be output to a neatly-formatted
text file.
Required Testing
Your program must calculate solutions for each problem in the given test file.
Grading Criteria
Your grade for this program will be based on:
- proper use and reuse of functions (including appropriate use of return statements and pass-by-value and pass-by-reference parameters), loops, and conditionals;
- file I/O;
- appropriate use of data structures; and
- efficiency of algorithms
Extra Credit
Note: All extra credit must be thoroughly tested and such tests must be included in your program submission.
- (+1) Detect and report overflow in addition and multiplication. You may continue to assume that operands
are representable with 8 bits. Of course, sums and products of these operands might require more than eight bits of
storage. Overflow detection must occur at the binary level; in other words, you cannot use numbers in decimal
form to check for overflow.
- (+4) Expand the range of operands to include negative integers. Encode negative operands using 8-bit
two's complement representation. Your code must produce the correct solution when one operand is negative, when
both are negative, when the solution is positive, and when the solution is negative. The 8-bit
two's complement solution must be converted to decimal before being stored in the output file. (For a quick primer on
two's complement representation, see this Web page.)