# Entering in an nth degree polynomial

• 03-02-2006
Noah
Entering in an nth degree polynomial
For my Engineering Class I have to write a program that will take in an nth degree polynomial. The degree will always be positive and an integer. Then I have to use Simpson's Rule and the Rectangular rule to compare the numerical integration results, well... on to the point. I am having trouble with the entering of the polynomial. I don't know if I should have the subfunction be type void or not, because I am going to have most of my calculations done in subfunctions. Also I am unsure whether or not to have the user input the polynomial coefficients as a string or a character array. Any help or advice would be appreciated.

Code:

```#include <stdio.h> #include <stdlib.h> void polynomial(char *); main() { } void polynomial(char *ptr) {                 int degree;         printf("\nEnter the degree of the polynomial you wish to integrate.");         scanf("%d", &degree);         ptr=(char *)malloc((degree+1) * sizeof(char));         if(ptr= NULL)         {                 puts("Memory Allocation Failed.");         } }```
When I have
Code:

`ptr=(char *)malloc((degree+1) * sizeof(char));`
the degree+1 is for the entire length of the polynomial, for instance if you had a 2nd degree polynomial you would have 3 terms, x^2 + x + #.
• 03-02-2006
Richie T
well what i would do is make your functions return the result of
the numerical integration - that way main can compare the two:
also you could use the maximum error calculation for simpsons
rule and see if the difference between the two methods is less
than it - just an aside. also, for reading in the coefficients, they're
gonna be integers, not chars - its not that easy to get a computer
to do calculations with variable names as opposed to numerical
values - dynamically allocate an array of ints, use a loop to
read in the correct coefficient into each element and then
implement your function - in my own opinion i would think it
better to enter the coefficients one by one. your output could
look like:

Code:

```Enter degree: 2 Enter the coefficient of x^2: 1 Enter the coefficient of x: 0 Enter constant: 5 The polynomial is: x^2 + 5```
getting output like that would be detailed (notice how it didn't
print x^1 and x^0? - just something i'd do if i had the time myself)

Good luck with implementing the numerical methods, hope i