Evaluating a polynomial 
Author Message
 Evaluating a polynomial

I want to write a program to evaluate a polynomial at given points.
e.g. 3x^3 + 9x^2 -7x +17, using:

1. A function "read_poly" whose purpose is to read the degree and
coefficients
of a polynomial, e.g. read_poly(int *degree, float coeff[], int
maximum_degree);

2. Function "poly_val", to do the evaluation
e.g. float poly_val(int degree, float coef[], float x);

3. Function "print_poly", to output the results of the evaluation
e.g. print_poly(int degree, float coef[], float x, float y)

So far I've got a version of read_poly built that won't compile.

/* A program to test read_poly.*/
#include <stdio.h>
#include <stdlib.h>
#define MAX_DEGREE  5

read_poly(float coeff[],int maximum_degree, int *degree);

int main()
{

float read_poly(float coeff[],int maximum_degree, &degree);

   return 0;

Quote:
}

    /* Try one test of read_poly */

void read_poly(float coeff[],int maximum_degree,int *degree)
{
 int degree;                                                           /*
Degree of the polynomial */
 float coeff[];                                                         /*
Array to hold the coefficients */
 int maximum_degree = MAX_DEGREE;
 int n;
/* Input values read by scanf */
 int b[256];                                                             /*
Temporary storage for coefficients */

 printf("Enter a positive integer to use as the degree of a polynomial: ");
 while(((n = scanf("%d",&degree)) == 1) && (*degree=>0)); /* Verify integer
type input */
 {
  if (degree <= max_degree)     /* Verify degree is within set parameters */

    printf("Enter a series of numbers to use as the coefficients of the
polynomial: ");
    gets (b);
    coeff = malloc(strlen(b));
    }
    if (coeff == NULL)
     {
 printf( "Out of memory\n")
 exit(0);
     }
    else
    strcpy (coeff, b)
 }

 puts(coeff);

Quote:
}

 Anyone willing to take a look and help a newbie catch a clue?


Tue, 22 Aug 2000 03:00:00 GMT  
 Evaluating a polynomial

Look at Cephes at Netlib.  You can not only evaluate polynomials, you can
multiply, divide, add and subtract them.  And more.
--
Hypertext C-FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-FAQ ftp: ftp://rtfm.mit.edu, C-FAQ Book: ISBN 0-201-84519-9
Try "C Programming: A Modern Approach" ISBN 0-393-96945-2
Want Software?  Algorithms?  Pubs? http://www.infoseek.com



Tue, 22 Aug 2000 03:00:00 GMT  
 Evaluating a polynomial

Quote:

> I want to write a program to evaluate a polynomial at given points.
> e.g. 3x^3 + 9x^2 -7x +17, using:
:
> 1. A function "read_poly" whose purpose is to read the degree and
> coefficients
> of a polynomial, e.g. read_poly(int *degree, float coeff[], int
> maximum_degree);
:
> So far I've got a version of read_poly built that won't compile.

> /* A program to test read_poly.*/
> #include <stdio.h>
> #include <stdlib.h>
> #define MAX_DEGREE  5

> read_poly(float coeff[],int maximum_degree, int *degree);

        You should place the function prototype here.  That is, float read_...

Quote:

> int main()
> {

        You need to declare some local variables, at least,

-               float coeff[MAX_DEGREE + 1];
-               int   degree;

Quote:
> float read_poly(float coeff[],int maximum_degree, &degree);

        You should call the function here.  That is,

-               read_poly( coeff, MAX_DEGREE, &degree);

Quote:

>    return 0;
> }

>     /* Try one test of read_poly */

> void read_poly(float coeff[],int maximum_degree,int *degree)
> {
>  int degree;     /* Degree of the polynomial */
>  float coeff[];  /* Array to hold the coefficients */
>  int maximum_degree = MAX_DEGREE;
>  int n;          /* Input values read by scanf */
>  int b[256];     /* Temporary storage for coefficients */

        degree, coeff, and maximum_degree (if necessary) must be declared
either globally or as local to the main function.  n and b might be okay
local to read_poly.  Why do you allocate space for 256 coefficients if
the maximum is MAX_DEGREE + 1 coefficiencts (assuming non-negative exponents)?

        (( ok, the true answer to that question is revealed below. ))

Quote:
>  printf("Enter a positive integer to use as the degree of a polynomial: ");
>  while(((n = scanf("%d",&degree)) == 1) && (*degree=>0)); /* Verify integer
> type input */

        Yech, I'd recommend using gets() or fscanf().  There are many
infamous problems with scanf().  Also, since degree is received by read_poly
as the address of an int, &degree should be degree.  *degree is correct.

Quote:
>  {
>   if (degree <= max_degree)     /* Verify degree is within set parameters */

        C is case-sensitive by default.  max_degree should be MAX_DEGREE.

        degree is a memory location, *degree is an integer value.

        Also, you want this conditional to affect a block statement, not just
the prompt to enter a series of numbers.  Add a left curly brace after it, {,
and an additional right curly brace, }, below after strcpy().  As is, when
*degree is greater than MAX_DEGREE, it will skip the prompt but conveniently
wait for input from gets() anyway.

Quote:
>     printf("Enter a series of numbers to use as the coefficients of the
> polynomial: ");
>     gets (b);

        b is an array of integers, and not characters.  This shouldn't work
without a cast.  And it's certainly not a good idea.

Quote:
>     coeff = malloc(strlen(b));

        If you are using malloc() to allocate dynamic memory for coeff,
it should be declared as a pointer to float (float *) and not an array
of float.  This could cause you problems later.

Quote:
>     }
>     if (coeff == NULL)
>      {
>         printf( "Out of memory\n")
>         exit(0);
>      }

        It is good that you checked the return value of malloc().

Quote:
>     else
>     strcpy (coeff, b)
>  }

        Problem.  coeff is a pointer to a number of float (real number values)
variables.  The number of variables coeff points to just happens to be the same
as the number of characters (digits, decimal points, spaces, newlines, and
perhaps the name of somebody's dog) the user typed in when you prompted him
for a series of coefficients and used gets() to put them into an ... integer
.... buffer.

        So, if I type in "17.5 10 -2.0" then coeff is allocated to hold 13
coefficients (12 chars typed, plus a new line at the end which gets() returns).
But I only typed three.  This is because you used strlen() instead of a
routine to count the number of floats in the text the user types (using
strtok() to search for Spaces or Newlines as delimiters, or a routine like
vscanf() which can parse them).

        strcpy() will only copy chars from b to coeff.

        coeff doesn't have to be a pointer, it can be an array declared
MAX_DEGREE + 1 in length.

        b should be an array, 256 is a good value, but it should be an array
of chars.  As follows,

-               char b[256];    /* buffer to hold characters typed by user */
:
        Later, gets() should put the user's input into b.  Then on b, you
should use vscanf() to parse out the floating point values into their cor-
responding coeff's.  This of course, assumes the user follows your instruc-
tions and only types in a number of legit floating point values and no more
in number than the degree they entered earlier.

Quote:

>  puts(coeff);

        I doubt the program ever gets this far.  

Quote:
> }
:
>  Anyone willing to take a look and help a newbie catch a clue?

        Study harder.  :)
                                                        -- Stone


Wed, 23 Aug 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Question: do you know any algorithm in evaluating multidimensional polynomials?

2. Evaluating functions using IDebugger Evaluate member.

3. polynomial evaluation

4. CRC-8 polynomial

5. bitwise polynomial division

6. Polynomial division in C

7. printing a polynomial

8. polynomial CRC32

9. CRC-CCITT polynomial source code

10. Roots of polynomials

11. polynomial curvefit routine

12. Polynomial curve fitting routine?

 

 
Powered by phpBB® Forum Software