Thread: question about programming polynomials

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    20

    Question question about programming polynomials

    our homework is actually a long one, about numerical analysis, roots of poylnomials, etc. the problem is i don't know how to do the first step, which is storing/saving a user-input polynomial(eg this form: x^3-4x^2+2). do i use arrays?strings? and how do you save the polynomial as a function so that you can get the value of the polynomial when a value for x is given(say x = 2, then, from the ex above, f(x) = -6).

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    I don't know what option is best for your case, but why don't you experiment with different approaches?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    say i use strings to store the polynomial, how can i save it as a function?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    You can't save it directly as a function but rather pass it to a function that will translate it to the proper operations.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    how?

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    I have an idea about how to store the polynomial. My opinion is, store it in a struct that has two elements: an integer "maxPow" that represents the bigger power and an array of size "maxPow" that will store the coefficients of each part of the polynomial.
    Devoted my life to programming...

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by firehydrant View Post
    how?
    For that you'll need to know how to parse math expressions. There are many tutorials online, compiler expression parsing such get you started!
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    we haven't tackled structs yet in class, so as of the moment, i can't use those.

    if i use strings to store the polynomial, is there a way to pick out the numeric parts of it? that way, i can assign it as coefficients or exponents in my function.

  9. #9
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by firehydrant View Post
    if i use strings to store the polynomial, is there a way to pick out the numeric parts of it? that way, i can assign it as coefficients or exponents in my function.
    Of course there is, but as i said, that can be done by parsing the math expressions inside it.
    And since you don't know structs yet, this "translation" has to be done everytime you want to call that polynomial.
    Devoted my life to programming...

  10. #10
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    how do i do it exactly?

  11. #11
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Quote Originally Posted by firehydrant View Post
    how do i do it exactly?
    Search for Math Expression Parsing
    Devoted my life to programming...

  12. #12
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    ok
    Thanks!

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The simplest way is ask user from number of degree(n).
    and ask coefficient of each term.

    Code:
    Deg of polynomial: 2
    coefficient of x^2 : 1
    coefficient of x^1 : -4
    coeficient of x^0 : 2

  14. #14
    Registered User krishnapollu's Avatar
    Join Date
    Dec 2010
    Location
    KERALA
    Posts
    29
    i have a suggestion. but i think it will make the program a bit lengthy.
    anyway let me.

    i think u ppl hav heard about infix and postfix expressions.
    first of all, consider the input polynomial as an INFIX expression.
    convert it to POSTFIX and then evaluate it by giving values to variables.

  15. #15
    Registered User
    Join Date
    Feb 2011
    Posts
    20
    how do you that math expression parsing?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Generating Polynomials using Linked Lists
    By shaheedpak in forum C++ Programming
    Replies: 0
    Last Post: 09-25-2001, 01:22 PM

Tags for this Thread