I'm trying to upgrade my class library to my current mathematical skill set and am having trouble with multi-term polynomials, especially those I where I don't know how many terms the user will be putting in. I know that the formula for the first derivative of a polynomial is ax^n = n * ax^n-1; and the second derivative is the derivative of the first derivative. So for example:

Find the derivative of 4x^2:
4x^2 = 2 * 4x^2 - 1 = 8x^2-1 = 8x

Find the second derivative of 4x^3:
4x^3 = 3 * 4x^3-1 = 12x^3-1 = 12x^2
12x^2 = 2 * 12x^2-1 = 24x^2-1 = 24x

Right now I just have an algorithm for the first derivative of a single term polynomial:

    // Pesudocode (C++ Syntax):
    int Base = 0;
    int Exponent = 0;
    string Variable = "";

    cout << "Please input a polynomial to the first degree in the form (4 y 9): ";
    cin >> Base >> Variable >> Exponent;

    Base = Exponent * Base;
    Exponent = Exponent > 1 ? Exponent - 1 : 0;
    Variable = Exponent == 1 ? Variable : Variable + "^";
    Variable = Exponent == 0 ? "" : Variable;
    string exString = (Exponent == 1 || Exponent == 0) ? "" : (const char*)Exponent;

    cout << "The first derivative of the polynomial you submited is: " << Base << Variable << exString << "\n";

Chose to not work on the second derivatives until I figure out the whole parameter problem. I'm sure I have to use arrays or lists to accomplish this, but I'm not sure how the algorithm would work for a polynomial with 3 or more terms:

3x^2 + 13x + 4

Also the derivative of a constant (basically any number without a variable and exponent of 2 or higher) is equal to 0 for those who don't know and the derivative of infinity is undefined.

4 Years
Discussion Span
Last Post by lxXTaCoXxl

here is the code i generated, however you will need to implement the calculations where it says "calculate":

struct poly
    int base;
    char fact;
    int exponent;

int numPolynomials;

cout << "Enter the number of polynomials: ";
cin >> polynomials;

polynomials *p[sizeof numPolunomials];

for(int i = 0; i<=numPolynomials; i++)
    cout << "Enter the base for polynomial " << i << ": ";
    cin >> p[i]->base;
    cout <<"\n\nEnter the factoral (character) for polynomial " << i << ": ";
    cin >> p[i]->fact;
    cout << "\n\nEnter the exponent for polynomial " << i << ": ";
    cin >> p[i]->exponent;
    cout << "\n\n";

std::string answers[sizeof nnumPolynomials];
int j = 0;
std::string final;
    if(p[j]->exponent == 1)
        answers[j] = p[j]->base + p[j]->fact;
    else if(p[j]->fact == "")
        answers[j] = p[j]->base;
        /* calculate */
}while(j != numPolynomials);

for(int k = 0; k <= numPolynomials; k++)
    final = final + " " + answers + " + ";

cout << "\n\nThe answer is: " + final;

retrun 0;

Two things:

1) I love your signature it's completely true.
2) How would I just allow the user to input the polynomial as they would write it in notepad: 3x^3 + 13x + 4 for example. The whole process of inputing each one by hand takes a while, but I suppose is a good way to solve it. I did enjoy your approach on the idea. I'll try implementing it and let you know what happens. However, what is line 14 doing? I don't see any data being stored to numPolynomials, and have no experience with the sizeof keyword. Based on it's name I'm guessing it's something to do with the value it holds and setting a variable up as an array style variable. That's what it looks like to me anyways. Also, if I remember correctly (not sure) but isn't * a pointer operator? Just needing some explinations on those before I can get started. :)

Thanks for the help,


you may need to use strtok for that. get the imput as string, convert to char array, check each char until an alpha charcter s found iand then check for the exponent.then create a new string using strtok for each polynomial. however this way is much longer becasue then you have to check the string agauin to decipher the data (the base, factoral and exponent

line 14:

polynomials *p[sizeof numPolynomials];

creates an array of pointers to struct polynmials the size of the specified number of polynomials.

btw, line 14 should say: "sizeof numPolynomials" instad of "sizeof numPolunomials" sorry about that. notepad++ does not come with a spell checker.

sizeof returns the size of the specified declarator, as long as it holds numerical data: take for instance this:

int x = 256;
char buffer[sizeof x]; // set the size of buffer to x

hopefully this clears up somethings

note : my code has a few errors. maily because i wrote it in notepad++ mostly where i have variables named wrong.
like cin >> polynomials should be cin >> numPolynomials; sorry.

Edited by TTTHXC


Parsing strings is a powerful tool. Using strtok() is one option to help, but often isn't the best option.

When you don't know the number of inputs ahead of time you can use an array that is big enough you never expect to fill it to capacity or you can use a self expanding container like a list, vector, etc. Many of the self expanding containers are available in the STL libraray and ready for use, once you comfortable the syntax used in the STL.


Well I thought about using a list, and all that fun stuff, but the guys over in the C# section helped me figure out my algorithm in C# and then I just converted it over to C++. Thanks for all the help.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.