CMSC313, Computer Organization & Assembly Language Programming, Fall 2012
Project 5: Polynomials
Due: Thursday October 18, 2012, 11:59pm
Objective
The objective of this programming project is to practice
working with structs, arrays and functions in C.
Assignment
Your assignment is to write a C program that implements the
functions of the polynomials abstract data type (ADT) declared
in the following header file (poly5.h):
#define MAX_DEG 15
typedef struct {
unsigned int deg ;
int coeff[MAX_DEG+1] ;
} poly ;
poly add_poly ( poly p1, poly p2) ;
poly sub_poly ( poly p1, poly p2) ;
poly mul_poly ( poly p1, poly p2) ;
void print_poly (poly p) ;
You must implement the functions add_poly, sub_poly,
mul_poly and print_poly. These implementations
should go in a separate .c file.
Once you have implemented these functions, then you can use polynomials
in the main function simply by calling functions like this:
poly p1 = { 3, {2, -1, 1, -2} } ;
poly p2 = { 1, {1, 3} } ;
poly answer ;
answer = add_poly(p1, p2) ;
print_poly(p1) ;
printf(" plus ") ;
print_poly(p2) ;
printf(" equals ") ;
print_poly(answer) ;
printf("\n") ;
The code above should produce output that looks like:
-2 x^3 + x^2 - x + 2 plus 3 x + 1 equals -2 x^3 + x^2 + 2 x + 3
Implementation Notes
-
Please actually read these implementation notes.
A lot of questions are
from students who haven't read the notes. If you have questions about
something in the notes, then by all means ask and we are happy to answer
them. But, please actually read these notes.
- You must work with the header file poly5.h given in
/afs/umbc.edu/users/c/h/chang/pub/cs313/poly5.h
You are not allowed to alter this file.
- The deg field is the highest degree of the polynomial.
The coefficients of the polynomial are stored in the coeff
array. For example, the polynomial
-2 x3
+ x2
- x
+ 2
is represented in a variable p by:
poly p ;
p.deg = 3 ;
p.coeff[0] = 2 ;
p.coeff[1] = -1 ;
p.coeff[2] = 1 ;
p.coeff[3] = -2 ;
Make sure you are not off by one. Also note that the smallest degree
comes first.
-
The constant MAX_DEG has the highest degree polynomial that your program
has to handle. The degree of a polynomial is the largest exponent.
In the example, above
-2 x3
+ x2
- x
+ 2
has degree 3.
- Do NOT assume that uninitialized parts of the coeff[] array are 0.
In the example above,
p.coeff[4],
p.coeff[5],
p.coeff[6],
... ,
p.coeff[MAX_DEG]
may hold garbage values.
- Yes, you are passing struct parameters by value and
returning a struct value from your functions. This is usually
frowned upon and declared to be "inefficient" (which it may well be).
Nevertheless, passing and returning a struct by value is allowed by C.
- If you don't remember how to multiply polynomials, please review.
For example:
(−2 x3
+ x2
− x
+ 2)
(3 x
+ 1)
=
−6 x4
+ x3
− 2 x2
+ 5 x
+ 2
-
A small test file is available at:
/afs/umbc.edu/users/c/h/chang/pub/cs313/main5.c
The output of the main5.c program, when compiled with your implementation of the
poly functions, should look something like:
-2 x^3 + x^2 - x + 2 plus 3 x + 1 equals -2 x^3 + x^2 + 2 x + 3
-2 x^3 + x^2 - x + 2 minus 3 x + 1 equals -2 x^3 + x^2 - 4 x + 1
-2 x^3 + x^2 - x + 2 times 3 x + 1 equals -6 x^4 + x^3 - 2 x^2 + 5 x + 2
- You must also make your own test cases and submit them.
- Your program should report an error in mul_poly() if
multiplying the two polynomials would result in a polynomial with a
degree that exceeds MAX_DEG. In this case, you can return
anything to the main program.
Extra Credit
For 15% extra credit, make sure that print_poly() prints
"nicely":
- It should make a special case for the leading term.
- It should make a special case for the linear and constant
terms.
- It should avoid printing a term when the coefficient is zero.
- It should avoid printing a coefficient when the coefficient is 1.
- It should subtract instead of adding a negative term.
For example, the polynomial
2 x4
+ x2
− 2 x
+ 5
should be printed as
2 x^4 + x^2 - 2 x + 5
instead of
+ 2 x^4 + 0 x^3 + 1 x^2 + -2 x^1 + 5 x^0
Turning in your program
Use the UNIX submit command on the GL system to turn in
your project.
When you are done, use the script Unix command to record
yourself running your program.
You should submit 3 files: main5.c, poly5.c and
typescript. You should not submit poly5.h since this
must be identical to the one distributed. Note: main5.c must
have test cases that demonstrate the features of your program. You
should not rely on the simple test cases in the original
main5.c.
The UNIX command to submit should look something like:
submit cs313 proj5 main5.c poly5.c typescript
Last Modified:
22 Jul 2024 11:28:31 EDT
by
Richard Chang
to Fall 2012 CMSC 313 Homepage