In: Computer Science
in the c programming language input is given in the form The input will be of the form [number of terms] [coefficient k] [exponent k] … [coefficient 1] [exponent 1] eg.
5 ─3 7 824 5 ─7 3 1 2 9 0
in this there are 5 terms with -3x^7 being the highest
/* Initialize all coefficients and exponents of the polynomial to zero. */ void init_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE */ } /* end init_polynom */ /* Get inputs from user using scanf() and store them in the polynomial. */ void get_polynom( int coeff[ ], int exp[ ] ) { /* ADD YOUR CODE HERE */ } /* end get_polynom */ /* Convert the polynomial to a string s. */ void polynom_to_string( int coeff[ ], int exp[ ], char s[ ] ) { /* ADD YOUR CODE HERE */ } /* end polynom_to_string */ /* Evaluate the polynomial for the value of x and store the result p(x) in variable result. */ void eval_polynom( int coeff[ ], int exp[ ], double x, double *result ) { /* ADD YOUR CODE HERE */ } /* end eval_polynom */ /* Add two polynomials and the result is stored in the first polynomial (arrays co1[] and ex1[]). */ void add_polynom( int co1[ ], int ex1[ ], int co2[ ], int ex2[ ] ) { /* ADD YOUR CODE HERE */ } /* end add_ polynom */
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int n=0; // n is number of elements which is a global variable
/* Initialize all coefficients and exponents of the polynomial
to zero. */
void init_polynom(int coeff[],int exp[])
{
int i;
for(i=0;i<n;i++)
{
coeff[i]=0;
exp[i]=0;
}
}
/* Get inputs from user using scanf() and store them in the
polynomial. */
void get_polynom(int coeff[],int exp[])
{
int i;
for(i=0;i<n;i++)
{
scanf("%d
%d",&coeff[i],&exp[i]);
}
}
int addValueIntoString(char s[],int k,int value)
{
int temp=value,count=0;
while(temp>=10)
{
temp=temp/10;
count++;
}
int tens=(int)(pow(10,count));
while(tens>0)
{
int digit=value/tens;
s[k++]=(char)(digit+48);
value=value%tens;
tens=tens/10;
}
return k;
}
/* Convert the polynomial to a string s. */
void polynom_to_string(int coeff[],int exp[],char s[])
{
int i,k=0;
// k is
the variable that points to index in s[]
for(i=0;i<n;i++)
{
if(coeff[i]<0)
s[k++]='-';
// if
number is -ve, then '-' is added into s[]
int
value=abs(coeff[i]);
// adding the coeff into s[]
k=addValueIntoString(s,k,value);
s[k++]='x';
s[k++]='^';
k=addValueIntoString(s,k,exp[i]); // adding the exp
into s[]
if(i+1<n &&
coeff[i+1]>0)
s[k++]='+';
}
s[k++]='\0';
}
/* Evaluate the polynomial for the value of x and store the
result p(x) in variable result. */
void eval_polynom(int coeff[], int exp[], double x, double
*result)
{
double res=1;
int i;
for(i=0;i<n;i++)
{
if(exp[i]==0)
res=res+coeff[i];
else
res=res*coeff[i]*pow(x,exp[i]);
}
*result=res;
}
/* Add two polynomials and the result is stored in the first
polynomial (arrays co1[] and ex1[]). */
/* A small note to this method is, this method works correctly only
if both the polynomials have same degrees or else wrong outputs
will be generated. Please consider this point too. */
void add_polynom(int co1[], int ex1[], int co2[], int ex2[])
{
int i;
for(i=0;i<n;i++)
{
co1[i]=co1[i]+co2[i];
}
}
int main()
{
char str[100];
scanf("%d",&n);
int coeff[n],exp[n];
init_polynom(coeff,exp);
get_polynom(coeff,exp);
polynom_to_string(coeff,exp,str);
printf("%s\n",str);
double x=1,result=0;
double *ptr=&result;
eval_polynom(coeff,exp,x,ptr);
printf("%lf\n",*ptr);
add_polynom(coeff,exp,coeff,exp);
polynom_to_string(coeff,exp,str);
printf("%s\n",str);
return 0;
}