Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

PDS Lab Assignment 5 [CS19003][Section-11]

Somak Aditya Satrajit Ghosh


January 4, 2023

Instructions:
• Write a C program for the tasks as described below. Save the file as A05 Roll Number.c. Build
and run to check your program. Upload the .c file for the assignment in MS teams.
• Use of any advanced concepts (not taught in class yet) is strictly prohibited. Same goes for using
any external libraries that are not explicitly mentioned during the problem discussion.
• Give meaningful comments to explain the functionality of the method used in your programs.
Comments and proper indentation carry credits.
• The use of mobile phones during the test is strictly prohibited.
Let us play with polynomials again! Here we will multiply two polynomials in coefficient representation
and point-value representation to check the equivalence between two methods of multiplication. You
may use 1D Array data structure, dynamic memory allocation, pointers, Loops, Iterations, Conditional
statements and functions for this assignment.
1. Write a function int* RandPolyGen(int d) which generates random polynomial of degree d with
integer coefficients less than 100. Write a function PrintPoly() with proper arguments and re-
turn type to print a polynomial in coefficient representation. Ask the user to input degree of two
polynomials and call RandPolyGen() twice to generate two random polynomials f (x) and g(x).
Call PrintPoly() function twice to print those random polynomials in coefficient representation.
PrintPoly() should print polynomial in a proper order of degree. [25]
2. Write a function EvalPoly() with proper arguments and return type, which take a polynomial and
evaluation point as input and return the polynomial evaluation on that point. Ask the user to input
an integer e. Call EvalPoly() twice to get f (e) and g(e). Print f (e) and g(e). [15]
3. Write a function PolyMultCoef() which takes two polynomials as input and return the product
polynomial in coefficient representation. Call PolyMultCoef() from main() to generate h(x) which
is the product of f (x) and g(x). Print h(x) by calling PrintPoly() from the main(). [30]
4. In this part you have to multiply the polynomials using point value representation. Note that if
you multiply a degree d1 polynomial and a degree d2 polynomial then you’ll get a degree d1 + d2
polynomial. To represent that you need n = (d1 + d2 ) + 1 points. Call a function RandGen() to
generate n random distinct points {α1 , . . . , αn } less than 4n. Print all the points. Evaluate f (x)
and g(x) on those n points by calling EvalPoly(). Multiply them point wise to generate point value
representation of m(x) = f (x)g(x). Print the f (x), g(x) and m(x) point wise.
Check whether {h(α1 ), . . . , h(αn )} is same as {m(α1 ), . . . , m(αn )}. If they are same print h(x)
and m(x) are equivalent, otherwise print an error message. [30]
5. Bonus: If degree d1 = d2 = 1, then call a function InterpolatePoly() with appropriate argu-
ments and return type to interpolate m(x) from {{α1 , m(α1 )}, . . . , {αn , m(αn )}}. Print m(x) in

1
coefficient representation using PrintPoly(). If that is same with h(x) print any success message,
otherwise print an error message. [10]
Extend this for any d1 and d2. [20]

Note:
• Include time.h and use rand() to generate random numbers. You need to initialize the seed using
srand(time(0)).

Lagrange Interpolation: Consider a degree two polynomial f (x). Let {{α1 , f (α1 )}, {α2 , f (α2 )}, {α3 , f (α3 )}}
be one point value representation of f (x). Then we can write f (x) as:

(x − α2 )(x − α3 ) (x − α1 )(x − α3 ) (x − α1 )(x − α2 )


f (x) = f (α1 ) · + f (α2 ) · + f (α3 ) · .
(α1 − α2 )(α1 − α3 ) (α2 − α1 )(α2 − α3 ) (α3 − α1 )(α3 − α2 )

Sample Output: Here is one sample output:


Please enter Degree of the polynomial f (x): 1
Please enter Degree of the polynomial g(x): 1
The Degree 1 polynomial is: f (x) = 8x
The Degree 1 polynomial is: g(x) = 5 + 4x
Please enter an evaluation point: 1
f (1) = 8
g(1) = 9
The product of f (x) and g(x) is: h(x) = 40x + 32x2
3 distinct points are: 3 4 8
Point value representation of f :
f (x) := {(3, 24), (4, 32), (8, 64)}
Point value representation of g:
g(x) := {(3, 17), (4, 21), (8, 37)}
Point value representation of f g:
m(x) := {(3, 408), (4, 672), (8, 2368)}
Point value representation of h(x):
h(x) := {(3, 408), (4, 672), (8, 2368)}
Check successful. h(x) and m(x) are equivalent
The result of interpolation is:
m(x) = 40.000000x + 32.000000x2
Yay! Success.

You might also like