USING scheme USING scheme 5. (8 points) A single-variable polynomial of degree n
ID: 3732025 • Letter: U
Question
USING scheme
USING scheme
5. (8 points) A single-variable polynomial of degree n is written as where an,..., ao are coefficients. Suppose we represent such a polyno- mial as a list (ao, a1, a2, , an) In this question, you are asked to write a Scheme function polyMult that performs polynomial multiplication of two polynomials. For in- stance, when given (1 2 1) and (1 2 1), polyMult should return (1 4 6 4 1), because (1 + 2r +226243+z We are going to implement the polynomial multiplication by convert- ing it into a series of polynomial addition operations. For the example, the multiplication can be performed in the following way (12x +x2)(1 2r r2) (1 + 2x +x2)+ (0+2r +42)+ (0+0 + 2 2r3+) = Do the following steps to implement the above polynomial multiplica- tion procedure (a) Write a function nzero, which takes a number n and returns a list of n zeros. For instance, calling nzero on 3 returns (O O 0) (b) Write a polynomial addition function polyAdd that adds two polynomials. For instance, when given (1 2 1) and (0 2 4 2), it returns (1 4 5 2)Explanation / Answer
ans:-
given
A single-variable polynomial of degree n is written as ao+aix+azx2+...+o„x'i where as are coefficients. Suppose we represent such a polyno-mial as a list (as,a,a2 ha this question, you are asked to write a Scheme function polyMult that performs polynomial multiplication of two polynomials. For in-stance, when given It 2 1) and (1 2 1), polyMult should return (1 4 6 4 1), because
(1+2x + x2)(1+2x + 02) =1+ 4x +6x2+4x3+x4
PROGRAM:-
int m, n, p;
int max(int m, int n) { return (m > n)? m: n; }
int maxnum(int m,int n, int p) { return (m > n) ? ((m > p) ? m : p):((n > p) ? n : p); }
int *nzero(int n) {
int arr[n] = {0};
return arr;
}
int *polyAdd(int A[], int B[])
{
int size = max(m, n);
int *sum = new int[size];
for (int i = 0; i<m; i++)
sum[i] = A[i];
for (int i=0; i<n; i++) {
sum[i] += B[i];
printf("sum[%d] = %d", i, sum[i]);
}
return sum;
}
int *polyMult(int A[], int B[])
{
int *mult = new int[m+n-1];
for (int i = 0; i<m+n-1; i++)
mult[i] = 0;
for (int i=0; i<m; i++)
{
for (int j=0; j<n; j++)
mult[i+j] += A[i]*B[j];
}
return mult;
}
int *polyAddList(int A[], int B[], int C[])
{
int size = maxnum(m, n, p);
int size_1 = max(m,n);
printf(" max = %d ", size);
int *sum = new int[size];
for (int i = 0; i<m; i++)
sum[i] = A[i];
for (int i=0; i<n; i++) {
sum[i] += B[i];
}
for (int i=0; i<size; i++) {
sum[i] += C[i];
printf("sum[%d] = %d", i, sum[i]);
}
return sum;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.