Homework 6
ECE 417
Program
Purpose
This program is intended to provide understanding in the area of procedure/function
implementation.
Program Statement
Write a Pentium program that calculates and prints out (in a nice form)
the first derivative of a simple polynomial.
Sample program run:
Enter degree of polynomial: 3
Enter coefficient of x^3: 2
Enter coefficient of x^2: -7
Enter coefficient of x^1: 0
Enter coefficient of x^0: 25
The polynomial entered is
2*x^3+-7*x^2+25
The first derivative is
6*x^2+-14*x^1
Derivatives
The first derivative of a polynomial is easy to calculate. The derivative
of a sum of terms is the sum of the derivatives of the terms. So, each
term in the derivative of the polynomial is calculated separately. For
a term in the polynomial, a*x^n, its first derivative will be a*n*x^(n-1).
Examples: The first derivative of -2*x^6 + 3*x^4 + 8*x^3
is -12*x^5 + 12*x^3 + 24*x^2
The first derivative of 1*x^5 + 12*x^4 + 3*x^2 + 58
is 5*x^4 + 48*x^3 + 6*x^1
Requirements
-
For this program, a legal integer as input will be 1 or more digits (0-9),
optionally proceded by a sign (+ or -), and optionally surrounded by spaces
or tabs. A newline character ends the legal input. If the program gets
illegal input, have it print out an appropriate error message and exit.
Some examples of legal decimal integers:
123
123000
+123
-123
0
+0
-0
000
Some examples of illegal decimal integers:
-123.34 <- value is -123, the '.' terminates the int
12:45 PM <- value of 1st int is 12, the ':' terminates the int
0a
+000123 <- octal int in C, can't have leading zero
- 123 <- sign needs to be next to 1st digit
0x123 <- a hex int, also, 'x' is not a digit
abc
-
The maximum degree of polynomial will be 5. Have the program print out
an error message and exit if the user enters a degree larger than 5.
-
The following procedures/functions must be implemented and used:
get_poly -- a procedure that gets and places in an array the
coefficients of the user entered polynomial. The input parameter to the
procedure is the address of the array. This procedure calls function get_int
to get the coefficients from the user.
put_poly -- a procedure that prints out a polynomial in a
nice form. The input parameter to the procedure is the address of the array
containing the polynomial. This procedure calls procedure print_int
to print out the coefficients of the polynomial.
get_int -- a function, which returns a flag indicating an
error detected in user input. The single parameter to this procedure is
the address of an integer variable. The function uses the address as the
location to place the integer it gets.
print_int -- a procedure which prints out an integer passed
to the procedure.
derivative -- a procedure which calculates the derivative.
The procedure receives one parameter: the address of the array containing
the coefficients.
-
Write the program such that it runs once, and then exits. Do not incorporate
a loop to execute the program more than once.
-
Comment your program appropriately. Every procedure/function needs a header
stating its function and its parameters. The contents of the activation
record for any procedure is also helpful.
Hints
-
If we limit ourselves to polynomials of degree 5, with integer coefficients,
a nice implementation of the program uses a 6 element array of integers
to hold the coefficients of the polynomial terms. An array index (starting
numbering with 0) is the degree of the term.
-
You may start with the code given at the end of Chapter 11 (the gcd program)
if you like. The program contains procedures for both get_int
and print_int.
-
Good advice is to implement get_poly and put_poly first,
before implementing the derivative procedure.
Handing In the Program
For this program, you will need to turn in a zip file. To make grading
easier, please name your file hw6.GX.zip. No printout will be turned
in.
Homework
There is NO homework with this assignment. The program will keep you busy
enough!