CSC 270H1Y -- Summer 2001

Project 1

Due Thursday June 21 at 6 p.m.

Your code will be graded on both its correctness and its elegence. So please take the time to try to come up with short and easy to understand solutions. Please read the programming guides on the Assignments page.

Please consult the directory ~hsc/classes/270/examples on CDF. Some of the examples may prove useful in solving these problems.

1. Roots of a Polynomial (40%)

Write a program "factorpoly.c" which finds all the roots of a polynomial of degree 24 or less.

The program will input data in the following order:

The program will use Newton's Method to find each root. Because Newton's Method iteratively guesses a root until it finds one, the maximum number of iterations inputted will limit the running of the method. Also, once the difference between two guesses falls below the tolerance, we will assume that we found a root.

Here is a sample execution:

%eddie: factorpoly
>>>> 5 1 -3 -3 7 6 5.0e-7 100
        Degree:        4
        Coeff:         1 -3 -3 7 6 
        Tolerance:     5.0e-07
        # Interations: 100

        Root:  3.000000, P( 3.000000) = 0
        Root:  2.000000, P( 2.000000) = 0
        Root: -1.000000, P(-1.000000) = 2.27374e-13
        Root: -1.000000, P(-1.000000) = 0
This execution finds the roots of x^4 - 3x^3 - 3x^2 + 7x + 6.

I have started the program for you: factorpoly.c

You have to write the following routines:

Please place all your code in the file factorpoly.c and submit it electronically.


2. Floating Point Calculator (60%)

Write a program called "calculator.c" which performs basic arithmetic on a floating point number which has a 60-digit, base 10, mantissa. The operation of the calculator will be the same as the decimal version in the examples directory: ~hsc/classes/270/examples/calc.c.

The structure you will use for the floating point number is as follows:

#define MANTISSA_SIZE   60
#define NEGATIVE        1
#define POSITIVE        0

struct floating {
    char sign;
    char mantissa[MANTISSA_SIZE];
    signed char exponent;
};
The sign may be NEGATIVE or POSITIVE depending on the sign of the number. The mantissa is an array of characters, and the legal values of each element are '0',...,'9'. The exponent is an 8-bit signed number and so can hold values between -128 and +127.

You should realize that the above floating point structure will allow calculations of more precision than either the float or double type of C.

Hint: This is not an easy problem, and I do not expect everyone to complete it so the grading will be weighted accordingly. Please attempt each piece in order because each piece is more difficult than the previous piece, and each later piece may depend on an earlier one.

You are to add the following pieces:

  1. floating_add(num1, num2): Returns the result of adding each floating point number together. It is assumed that the signs of the two numbers are the same.
  2. floating_sub(num1, num2): Returns the result of subtracting the second floating point number from the first. It is assumed the signs of the two numbers are different.
  3. floating_mult(num1, num2): Returns the result of multiplying the two floating point numbers together.
  4. floating_div(num1, num2): Returns the result of dividing the first number by the second. To implement division, I want you to use Newton's Method to calculate the reciprocal of the second number and multiply the first number with the reciprocal of the second.

Besides divide-by-zero errors, your program will also have to catch underflow and overflow errors. These errors occur if the floating point number is too large or small to fit in the data structure. In this case, you check for this error by detecting if the exponent goes out of the [-128, 127] bound.

Since we are dealing with decimal and not binary numbers, the first digit after the decimal point is stored. For your homework, assume that the number 0 is stored with all digits of the mantissa equal to 0 and the exponent equal to -128. If an error occurs, your program should report the type of error and exit.

Hint: To simplify your error checking, you may want to reduce the MANTISSA_SIZE to something more reasonable (like 4), and increase it once you get the functions working.

Note that you should not try to convert the mantissa characters to integer or floating point because the mantissa is too large to fit in either a C integer or double. Thus, you will have to write your own arithmetic utilities.

Here is some code that reads in and prints our floating point numbers: floating_numbers.c

Place all of your code in a file called calculator.c and submit the file electronically.