A (univariate) polynomial is specified by an array of its coefficients. Since on
ID: 3546144 • Letter: A
Question
A (univariate) polynomial is specified by an array of its coefficients. Since one cannot check during program execution the size of a static or dynamic array, one should additionally maintain the degree of a polynomial.
Define a structure to represent a polynomial with integer coefficients. The coefficient array is to be maintained dynamically so that the exact amount of memory needed to store the coefficient array is only allocated.
Write a function that, given a polynomial p (in this representation) and an integer a, returns the value p(a).
Write functions to implement the arithmetic routines addition, subtraction and multiplication on polynomials.
Explanation / Answer
#include <stdio.h>
#include<math.h>
typedef struct poly
{
int n;
struct poly *next;
} pol;
int main()
{
int a,i,j,m;
pol *head,*k;
printf("enter the degree of polynomial ");
scanf("%d",&a);
head=(pol *)malloc(sizeof(pol));
k=(pol *)malloc(sizeof(pol));
k=head;
printf("enter the coefficients of the polynomial ");
for(i = 0; i<=a; i++)
{
int p;
scanf("%d",&p);
pol *temp = (pol *)malloc(sizeof(pol));
head->next = temp;
temp->n = p;
temp->next = NULL;
head = temp;
}
printf("the polynomial is :");
head=k;
for(j=0;j<=a;j++)
{
printf("%d X^(%d)",head->next->n,(a-j));
head=head->next;
}
printf(" ");
head=k;
printf("enter the value of a ");
scanf("%d",&m);
printf("the value of f(a):");
int sum=0;
for(j=0;j<=a;j++)
{
sum=sum+(head->next->n*pow(m,(a-j)));
head=head->next;
}
printf("%d",sum);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.