Question: Prove that after the following code runs, prod represents the product of the two polynomials represented by m and n. Representation is as follows: To
Prove that after the following code runs, prod represents the product of the two polynomials represented by m and n. Representation is as follows:
To represent x2 + x + 2 you would create an array, x, that equals [2, 1, 1], where x[i] stores the coefficient of the term of power i.
Use induction and a suitable loop invariant to prove the correctness of this code.
Precondition: { m and n are non-empty arrays, prod is an array of length (m.length + n.length - 1)}
int i = 0 while(i < prod.length) { prod[i] = 0; i++; } int i = m.length - 1; int j = n.length - 1; while(i >= 0) { while(j >= 0) { prod[i + j] = prod[i + j] + m[i] * n[j]; j--; } i--; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
