CS13002 Programming and Data Structures |
(Autumn semester) Section A |
Assignment Set - 4
Please write your Roll Number, Name and Assignment Number at the header of each program.
Assignment 4.a: Filename: a4a.c
The polynomial of degree n:
a0 + a1x + a2x2 + ... + anxn
can be represented by an array a[ ] of floating point numbers, where a[k] stores the coefficient ak
(a) Write a function: int readpoly(float arr[]) that does the following:
- It reads the degree of a polynomial
- It reads the coefficients of the polynomial and stores them in the array. Coefficients may be positive, negative, or zero.
- It returns the degree of the polynomial
Assume that none of the polynomials that are read have degree greater than 9. Choose the dimensions of your arrays accordingly.
(b) Write a function that receives a polynomial as an input parameter, and returns its maximum and minimum coefficients as output parameters
- The polynomial is represented by an array, arr[ ], which is passed as input parameter.
- The degree of the polynomial is also passed as an input parameter.
- The output parameters are two separate variables maxcoeff and mincoeff respectively.
(c) Write a function: float evalpoly(float arr[], float x) that returns the value of the polynomial arr[ ] for a given value of x
The Horner's method for evaluating polynomials works as follows. Rewrite the polynomial as:
a0 + x(a1 + x(a2 + x(... + x(an-1 + x(an)) ...)))
Now write a for-loop that starts from the inside of this expression and works outwards in each iteration.
In the first iteration we compute val = x(an)
In the second iteration we compute val = x(an-1 + val)
In the third iteration we compute val = x(an-2 + val) ... and so on.(d) Write a function: void productpoly(float a[], float b[], float c[])
that computes the product of two polynomials a[ ] and b[ ] and stores the product polynomial in c[ ].The product of 3 + 2x - 5x2 and 2 + 7x is the polynomial 6 + 25x + 4x2 - 35x3
Choose the dimension of array c[ ] keeping in mind that the degree of the product is greater than that of a[ ] and b[ ]Write a main( ) program that does the following steps:
- It reads two polynomials A and B using the function readpoly( )
- It uses the function productpoly( ) to compute the product of A and B and store it in C
- It prints the coefficients of C
- It uses the function of part (b) to print the minimum and maximum coefficients of A, B and C.
- It reads a value of x, and uses the function evalpoly( ) to compute and print the value of A, B and C for that value of x. The user is prompted to repeat this step.