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

Programming and Data Structure Laboratory (CS19001)

Spring 2022-23

Assignment for Week 7 (May 08, 2023)

Total Marks: 100 Duration: 3 hours

INSTRUCTIONS
a) All programs should be written in C.
b) The file containing your solution to problem ‘x’ should be named roll-week7-probx.c where ‘roll’ is your roll
number.
c) Use indentation and comments as necessary. You are allowed to consult your books, notes or manual pages.
d) Upload the three files separately on Moodle.

PROBLEMS

1. Write the following functions in C. [10+10+10+15=45]


a) A polynomial of degree n can be represented by an array of size n+1, where the array elements
contain the value of the coefficients. For example, the polynomial 5x4 -3x2 +12x + 3 can be represented
by an array with elements {5, 0, -3, 12, 3}. Write functions to perform the following computations on
polynomials (with integer coefficients) – evaluation at an integral point, addition and multiplication.
More specifically, write the following functions:
i) int eval_poly (int p[], int n, int z) that evaluates a polynomial of degree n
at z.
ii) void add_poly (int p[], int q[], int n, int r[]) that takes as input two
polynomials p and q of degree n, computes their sum, and stores it in polynomial r.
iii) void mult_poly (int p[], int q[], int n, int r[]) that takes as input two
polynomials p and q of degree n, computes their product, and stores it in polynomial r of degree
2n.
b) Write a main function that displays a menu option as shown in the sample output, and carries out
computation as specified.
Sample Output 1:
1. Evaluate a polynomial
2. Add two polynomials
3. Multiply two polynomials
What would you like to do? 1

Enter the degree of the polynomial: 3


Coefficient of x^0: -1
Coefficient of x^1: 2
Coefficient of x^2: -6
Coefficient of x^3: 2

Enter the value at which the polynomial must be evaluated: 3


The polynomial 2 x^3 - 6 x^2 + 2 x - 1 evaluates to 5 at 3.

Sample Output 2:
1. Evaluate a polynomial
2. Add two polynomials
3. Multiply two polynomials
What would you like to do? 3

Enter degree of first polynomial: 2


Coefficient of x^0: -4
Coefficient of x^1: 0
Coefficient of x^2: 5

Enter degree of second polynomial: 5


Coefficient of x^0: 1
Coefficient of x^1: 0
Coefficient of x^2: 3
Coefficient of x^3: 0
Coefficient of x^4: 1

The two polynomials you entered are


5 x^2 - 4
x^4 + 3 x^2 + 1
Their product is
5 x^6 + 11 x^2 - 7 x^2 - 4

2. Write a function merge() that takes two integer arrays A and B of sizes sizeA and sizeB as parameters,
Both the arrays A and B are assumed to be in sorted (increasing / non-decreasing) order. The function
merges the two arrays into a single sorted array C; the array C is allocated space dynamically inside the
function. The function returns a pointer to the first element of array C. The function prototype is as follows:
int *merge (int A[], int B[], int sizeA, int sizeB);

Write a main function that reads two sorted arrays, calls the function merge() to combine them into a
single sorted array , and print it. [30]

3. Write a program in C, which does the following.


a) Allocate space for a character array of size 500 dynamically using malloc(), and read a character
string (containing spaces) into the array.
b) Replace all occurrences of the word IITKGP by “Indian Institute of Technology Kharagpur”.
Print the output in proper formatted fashion. [25]

You might also like