Implementation A polynomial can be represented by a collection of coefficient/ex
ID: 3883062 • Letter: I
Question
Implementation
A polynomial can be represented by a collection of coefficient/exponent pairs. For example, the above
polynomial can be represented by the following collection of number pairs:
(4,5) (7,3) (-1,2) (9,0)
One way to do this would be to use an array of structures. You may assume a limit of 10 terms in the
equation. Also, all exponents will be positive and integers. All coefficients will be integers.
Methods
You will need to implement the following methods:
• void addTerm(int coefficient, int exponent) – add the specified term to the
polynomial. Used repeatedly to construct the polynomial.
• int degree() - return the degree of the polynomial – that is, the highest exponent.
• int coefficient(int power) – return the coefficient of the term with the given power, or 0
if there is no such term.
• int numberOfTerms() - return the number of terms in the polynomial.
• int value(int x) – return the value of the polynomial for the given value of x.
Main Program
• Your main program should read pairs of numbers from a file and create a Polynomial by
repeatedly calling the addTerm() method
• You will run your program 2 times, using 2 different input files:
For each execution, output the following:
Degree: d
# terms: t
coeff(0): c
coeff(1): c
:
:
coeff(7): c
value(1): v
Explanation / Answer
#include <stdio.h>
#include <conio.h>
#include <math.h>
typedef struct node
{ int power;
float coeff;
struct node *next;
}node;
node * insert(node *head,int power1,float coeff1);
node * create();
node * padd(node *p1,node *p2);
node * pmul(node *p1,node *p2);
float eval(node *p1,float x);
void print(node *head);
node *insert(node *head,int power1,float coeff1)
{ node *p,*q;
/*terms are store in increasing order of power*/
/*pointer is used to locate the correct place of insertion.
pointer p is used to store the address of the node created for
the current term.If a term with same power is found,coefficients
are added*/
p=(node*) malloc(sizeof(node));
p->power=power1;
p->coeff=coeff1;
p->next=NULL;
if(head==NULL)
{
head=p;
head->next=head;
return(head);
}
if(power1>head->power)
{
p->next=head->next;
head->next=p;
head=p;
return(head);
}
if(power1==head->power)
{
head->coeff=head->coeff+coeff1;
return(head);
}
q=head;
while(q->next!=head && power1>=q->next->power)
q=q->next;
if(p->power==q->power)
q->coeff=q->coeff+coeff1;
else
{
p->next=q->next;
q->next=p;
}
return(head);
}
node *create()
{
int n,i,power1;
float coeff1;
node *head=NULL;
printf(" Enter No. of Terms:");
scanf("%d",&n);
printf(" enter a term as a tuple of (power,coefficient) : ");
for(i=1;i<=n;i++)
{ scanf("%d%f",&power1,&coeff1);
head=insert(head,power1,coeff1);
}
return(head);
}
int degree(node *head){
/* node *p;
int res=0;
p=head->next;
res=p->coeff;
while(p!=head->next){ /* This is also solution to find highest degree of polynomial when polynomial's are not in increasing */
if (res < p->coeff) /* order of power*/
{
res=p->coeff;
}
p=p->next;
}
return res; */
return head->power; /* As Per time of creation of polynomial, I handled in such a way that terms are store in increasing order of power */
}
int coefficient(node *head,int p)
{
while(head->next!=NULL)
{
if(head->power == p){
return head->coeff;
}
head=head->next;
}
}
int numberOfTerms(node *head)
{
int no_terms=0;
while(head->next!=NULL)
{
no_terms=no_terms+1;
head=head->next;
}
return no_terms;
}
float eval(node *head,float x)
{ float value=0.00;
node *p;
p=head->next;
do
{ value=value+p->coeff * pow(x,p->power);
p=p->next;
}while(p!=head->next);
return(value);
}
void print( node *head)
{ node *p;
p=head->next;
printf(" ");
do
{
printf("%6.2fx^%d ",p->coeff,p->power);
p=p->next;
}while(p!=head->next);
}
void main()
{ node *p1;
int op,deg,pow,n;
float value,x;
p1=NULL;
clrscr();
do
{ printf(" 1)Create first polynomial");
printf(" 2) Find degree");
printf(" 3) find coefficient");
printf(" 4) find number of terms");
printf(" 5)Evaluate Polynomial 6)Quit");
printf(" Enter Your Choice: ");
scanf("%d",&op);
switch(op)
{ case 1: p1=create();break;
case 2: deg=degree(p1); printf("%d",deg);break;
case 3: printf(" Enter the power");scanf("%d",&pow);coef=coefficient(p1,pow);
printf(" Coeff(%d) is %d",pow,coef);break;
case 4: n=numberOfTerms(p);printf(" terms are %d",n);break;
case 5: printf(" Enter the value of X:");
scanf("%f",&x);
value=eval(p1,x); /* Eval function is to calculate the value of polynomial */
printf(" value(%d) = %6.2f",x,value);
break;
}
}while(op!=6);
}