Thread: Entering in an nth degree polynomial

  1. #1
    Registered User
    Join Date
    Mar 2006

    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.

    #include <stdio.h>
    #include <stdlib.h>
    void polynomial(char *);
    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
    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 + #.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    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:

    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
    answered your questions.

    PS - you could avoid memory allocation and just define an array
    large enough to store a reasonable number of coefficients - say
    100. also, it wouldn't increase the complexity if you used double
    for the coefficients, its up to you, but its always good to give a
    user the freedom to enter in .87 if they so feel like it (and if the
    same makes logical sense for the program of course!)
    No No's:
    fflush (stdin); gets (); void main ();

    Example of fgets (); The FAQ, C/C++ Reference

    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition

    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program Degree to Radians
    By Cyberman86 in forum C Programming
    Replies: 5
    Last Post: 05-04-2009, 09:34 PM
  2. Computing Degree Question
    By cjwenigma in forum A Brief History of
    Replies: 2
    Last Post: 09-17-2007, 01:06 PM
  3. large polynomial
    By squeaker in forum C++ Programming
    Replies: 2
    Last Post: 03-22-2006, 01:15 AM
  4. Polynomial
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2002, 05:00 PM
  5. Polynomial Problem
    By softcoder76 in forum C++ Programming
    Replies: 5
    Last Post: 03-01-2002, 02:07 PM