You'll need to enter this input as a string. Then you'll have to "parse" the expression. This means that you will need to identify what is a coeffecient, what is a variable, what is an exponent, etc...
Then you just need to apply your derivative algorithm:
1 2
coeffecient*=exponent;
exponent--;
Ussually when I need to calculate a derivative, I do not evaluate the new expression. I take the change in the value and divide it by the change in time. Sometimes I do this every iteration, and sometimes I do a moving average over a period of time.
// these you can populate using simple I/O and for loop
int size = 5;
float coef [size] = {1,2,3,4,5};
float exponent[size] = {8,7,6,5,4};
// New function coefficient and exponent
float coef_new[size];
float exponent_new[size];
for (int i = 0; i < size; i++)
{
coef_new[i] = coef[i] * exponent[i];
exponent_new[i] = exponent[i] - 1;
}
// Print function
for (i = 0; i < size; i++)
{
cout << coef_new[i] << "x^" << exponent_new[i] << " ";
}
Is your program supposed to derivate any functions or just those of the form ax^b?
If so, thepedestian solution work fine.
If not, you have to learn about parsing to analyse the function and AST(Abstract Syntax Tree) to store your function.
Here is a link to understand LL parsers which can be silmply implemented by hand: http://en.wikipedia.org/wiki/LL_parser
A link to AST: http://en.wikipedia.org/wiki/Abstract_syntax_tree
Here your AST will represent functions so a node could be:
-a constant
-a variable
-a multiplication
...
expression tree is more or less the same thing as AST so yes it is useful. Read the links i gave in my precedent post, i think it is useful for your program.
LL means the parser parse Left to right and construct the tree by Leftmost derivation which means the first nonterminal symbol is rewriten with a grammar rule.
LR means the parser parse Left to right and construct the tree by Rightmost derivation which means the last nonterminal symbol is rewriten with a grammar rule.
To put it simply, LL parsers are easy to implement by hand as opposite to LR which are very difficult to implement by hand and are genarated with tools like Bison