CS1101C Practical Exam

Session 1 (0800 - 0945 hours)

Polynomials

The name of your C program file must be called poly.c, files with any other name will not be marked.

The deadline for this lab is Wednesday 09 November 2005, 09:45 hours. Strictly no submissions will be accepted after the deadline.

Background

A polynomial is of the form:

anxn + an-1xn-1 + an-2xn-2 + ... + a2x2 + a1x + a0

For simplicity, we assume that all the coefficients a0, a1, ..., an are integers. The above equation has degree n. We assume that a polynomial will have a maximum degree of 9, and we will be able to store a maximum of 10 polynomials.

Polynomial Functions

Since we have a maximum of 10 polynomials, we create 10 slots for polynomials, numbered slot 0 to slot 9. Each polynomial will be stored at a particular slot. We can then do some mathematical functions on each polynomial, and store the result in a different slot.

We wish to implement the following functions:

  1. Store a polynomial at a particular slot.
  2. Differentiate a polynomial with respect to x, and store the result at a different slot.
  3. Add two polynomials and store the result at a different slot.
  4. Subtract two polynomials and store the result at a different slot.
  5. Multiply two polynomials and store the result at a different slot.

Task

Your program should read a list of commands from a text file which specify the polynomial functions. After processing all the functions, display each polynomial at each of the 10 slots.

Text File

Each line in the text file called poly1.txt represents a command followed by parameters. This text file has been copied into your directory by the pesetup command. Here is the text file poly1.txt:
1 0 2 7 8 9
1 1 3 2 -3 -1 4
2 1 2
3 0 1 3
4 0 1 4
2 1 7
5 0 1 7
0

We will look at each line in turn.


1 0 2 7 8 9

1: indicates we want to store a polynomial.
0: indicates we store it at slot 0. If there is an existing polynomial at slot 0, it is overwritten.
2: indicates the degree of the polynomial.
7 8 9: indicates the coefficients of the polynomial, starting from the highest degree. This represents the polynomial 7x2 + 8x + 9.


1 1 3 2 -3 -1 4

1: indicates we want to store a polynomial.
1: indicates we store it at slot 1. If there is an existing polynomial at slot 1, it is overwritten.
3: indicates the degree of the polynomial.
2 -3 -1 4: indicates the coefficients of the polynomial, starting from the highest degree. This represents the polynomial 2x3 - 3x2 - x + 4.


2 1 2

2: indicates we want to differentiate a polynomial with respect to x.
1: indicates we want to differentiate the polynomial at slot 1.
2: indicates we want to store the result at slot 2.

For those who forgot how to do differentiating: Differentiating axn with respect to x gives the result: naxn-1


3 0 1 3

3: indicates we want to add two polynomials.
0 1: indicates we want to add the two polynomials at slots 0 and 1.
3: indicates we want to store the result at slot 3.


4 0 1 4

4: indicates we want to subtract the second polynomial from the first polynomial.
0 1: suppose f0(x) is the polynomial at slot 0, and suppose f1(x) is the polynomial at slot 1. We want to calculate f0(x) - f1(x).
4: indicates we want to store the result at slot 4.


2 1 7

Indicates we want to differentiate the polynomial at slot 1 and store the result at slot 7.


5 0 1 7

5: indicates we want to multiply two polynomials.
0 1: indicates we want to multiply the two polynomials at slots 0 and 1.
7: indicates we want to store the result at slot 7. The existing polynomial at slot 7 is overwritten.


0

0: indicates we want to exit the program.


You may assume that the target slot is always different from the source slot(s). If there is an existing polynomial at a target slot, it is overwritten.

You may also assume that all numbers in the text file are valid integers. It is possible for a coefficient to be zero. No error checking is required.

After processing all the commands, display the polynomials at all ten slots. If a slot does not contain any polynomial, just display a blank line, as shown below. If a term's coefficients is zero, do not display the term.

Sample Run

Assuming the sample file poly1.txt contains lines as shown above, the following is a sample run:
Poly #0: (7)x^2 + (8)x^1 + (9)x^0
Poly #1: (2)x^3 + (-3)x^2 + (-1)x^1 + (4)x^0
Poly #2: (6)x^2 + (-6)x^1 + (-1)x^0
Poly #3: (2)x^3 + (4)x^2 + (7)x^1 + (13)x^0
Poly #4: (-2)x^3 + (10)x^2 + (9)x^1 + (5)x^0
Poly #5:
Poly #6:
Poly #7: (14)x^5 + (-5)x^4 + (-13)x^3 + (-7)x^2 + (23)x^1 + (36)x^0
Poly #8:
Poly #9:
You are reminded to follow the sample output exactly; else marks will be deducted.

Remember to submit your program using the submit poly.c command, and check your submission using the check command.

All the best!

UNIX commands

Some useful UNIX commands (in case you forgot what you did in Lab 0):

  1. dir”: lists all the files in the directory.
  2. cp a.txt b.txt”: copies a.txt to b.txt.
  3. mv a.txt b.txt”: moves / renames a.txt to b.txt.
  4. cat a.txt”: shows the contents of a.txt.