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

Extra Credit

For 15% extra credit, make sure that print_poly() prints "nicely": 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: 25 Oct 2012 11:19:09 EDT by Richard Chang
to Fall 2012 CMSC 313 Homepage