Hi all.. I wrote a program for this:
A C program can represent a real polynomial p(x) of degree n as an array of the real coefficients a_0,a_1,…a_n (a_n≠0).
〖p(x)=a_0+a_1 x+a_2 x〗^2+⋯+a_n x^n
Write a program that inputs a polynomial of maximum degree 8 and then evaluates the polynomial at various values of x. Include a function get_poly that fills the array of coefficients and sets the degree of the polynomial, and a function eval_poly that evaluates a polynomial at a given value of x. Use these function prototypes:
void get_poly (double coeff [ ], int* degreep);
double eval_poly (const double coeff[ ], int degree, (double x);
here is my program
void get_poly(double coeff[],int* degree);
double eval_poly(const double coeff[],int degree,double z);
int main()
{
int deg;
double res,base,coeff[8];
printf("Insert a degree [ less than 8 ] for the polynomial please :\n");
scanf("%d",°);
printf("Insert the %d coefficients of the polynomial please : \n",deg+1);
get_poly(coeff,°);
printf("Insert the value of the base x please : \n");
scanf("%lf",&base);
res=eval_poly(coeff,deg,base);
printf("the result is %f",res);
return 0;
}
void get_poly(double coeff[],int* degree)
{
int i;
for(i=0;i<*++degree;i++)
scanf("%lf",&coeff[i]);
}
double eval_poly(const double coeff[],int degree,double x)
{
int i;
double res=0.0;
for(i=0;i<=degree;i++)
res=res+coeff[i]*pow(x,i);
return res;
}
the problem occured when the degree choosed is 1.. it supposed to take 2 cofficients.. but it takes 3 and the third one being ignored.. why? and how can this be fixed? :S