Assignment 2

 

A common implementation of a polynomial is to store its coefficients in an array. For example, 2x9 + 14x6 – 2x + 99 can be stored in an integer array of size 10 (int A[10]) like this:

A:

99

-2

0

0

0

0

14

0

0

2

 

0

1

2

3

4

5

6

7

8

9

Assume that the maximum degree is 9, do the following problems:

 

2.1.        Write a function P_input(int A[ ]) to read in the coefficients of a polynomial and store them in array A.

2.2.        Write a function P_add(int A[ ], int B[ ], int C[ ]) to add polynomials A and B, and store the result in C. In other words, compute C = A+B.

2.3.        Write a function P_subtract(int A[ ], int B[ ], int C[ ]) to compute C = A-B.

2.4.        Write a function P_multiply(int A[ ], int B[ ], int C[ ]) to compute C = A*B. Note that the maximum degree may be as large as 18.

2.5.        Write a function P_output(int A[ ], int d) to print out a polynomial A of degree d, where zero coefficients are skipped. For example, the above example should be printed as 2*x**9 + 14*x**6 – 2*x + 99

2.6.        Write a main program that calls the above functions to show that they are correct.

2.7.        (For more advanced students) Write a function P_divide(int A[ ], int B[ ], int Q[ ], int R[ ]) to compute Q = A/B + R. (Q stands for quotient, or 商式, and R stands for remainder, or 餘式).