This lab presents a simple introduction to arrays. When implementing the program for this lab, you are not allowed to use loops. This is because I want you to focus on the characteristics of arrays independent of loops (and we may not have formally discussed loops before you finish this lab).
You can get support files by downloading lab4.zip. This zip file will unpack to create a directory named lab4. Develop your solution in a file named poly.c.
For this lab you will develop a program that performs simple arithmetic on polynomials; specifically, your program will perform addition and multiplication.
In your program, polynomial will be represented by an array. The values in the array will represent the coefficients of the polynomial. The indices of the array will represent the exponents.
This means that the polynomial 2.7x2 + 3.1x + 2 will be represented by the following array.
double poly1[3];This array can also be initialized (e.g., for test cases) directly as follows.
poly1[0] = 2; /* This is the coefficient for x^0. */
poly1[1] = 3.1; /* This is the coefficient for x^1. */
poly1[2] = 2.7; /* This is the coefficient for x^2. */
double poly1[3] = {2, 3.1, 2.7};
You may think this mapping of a polynomial to an array is a bit odd. In fact, attributing meaning to indices of an array (and not just the values within the array) is a pretty important skill that allows an array to be used as more than just a substitution for a collection of variables.
You should develop your program as follows.
Develop the function poly_add2. This function will take two polynomials of degree two and compute the sum of the two polynomials. Since we cannot return an array, this function will place the result of the addition in a third array passed as an argument to the function. (That "result" array will be defined in the function that calls this function.)
void poly_add2(double poly1[], double poly2[], double result[])
Develop the function poly_mult2. This function will take two polynomials of degree two and compute the product of the two polynomials. Again, this function will place the result of the multiplication in a third array passed as an argument to the function.
Again, though the use of loops would allow one to generalize this function, for this lab you cannot use any loops. Think carefully about how to compute the product of polynomials and how that relates to the representation of polynomials in this lab.
Note that the resulting polynomial will, in general (and for all of our cases), be of greater degree. In this case, the result can be of at most degree four, so your result array will need to be large enough to hold the entire product.
void poly_mult2(double poly1[], double poly2[], double result[])
As always, you must develop test cases for the functions that you write. It is recommended that you develop these test cases before you write the code for each function. Place the test cases in the test_cases function. You are writing three separate programs. Each program must contain a test_cases function.
Note that there is no checkit macro for an array. Instead you will need to write a checkit for each element on your result array.
Prompt the user for two polynomials (of degree two) and print the sum and product.
Enter the coefficients for the first polynomial of degree two: 2 3.1 2.7
Enter the coefficients for the second polynomial of degree two: 9 1.1 4.7
sum: 11.000000 + 4.200000x + 7.400000x^2
product: 18.000000 + 30.100000x + 37.110000x^2 + 17.540000x^3 + 12.690000x^4
Demonstrate your work to your instructor to have this lab recorded as completed.