Start New Discussion within our Software Development Community

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

include <stdio.h>

include <stdlib.h>

include<math.h>

include<string.h>

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

  1. you forgot to include the variable degree in the argument after printing out insert the number of coefficients
  2. why did you manipulate the value of degree in the for loop at the get_poly function to always increment?
This article has been dead for over six months. Start a new discussion instead.