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.
The program will input data in the following order:
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) = 0This 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.
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:
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.