I have just started a code that will hopefully end up being able to differentiate any inputted function.
It is relatively simple function, so at the moment it can only do powers. so
would go to:
Code:
x^3+x^2 //this is just for bug finding at the moment, and for demonstration purposes.
3x^2+2x^1 //this is the answer
At least, that's the idea. What I actually get is:
Code:
x^3+x^2
- ( ...then some random characters which look like something out of wingdings.
I know that what is happening is that it is both doing some crazy stuff to the function and failing to differentiate it. What I can't figure out is why.
Here is the code:
Code:
#include <stdio.h>
#include <stdlib.h>
int simple(char x);
int main()
{
char f[50];
int i;
char p;
int n;
printf("Write the function you want to be Differentiated: ");
fgets(f, 50, stdin);
printf("\n");
for(i=0;i<50;i++)
{
printf("%c", f[i]); //This is just in to make sure the string is right, and this is where the problems are.
}
getchar();
for(i=0;i<50;i++)
{
if (f[i]=="x" && f[i+1]=="^")
{
simple(f[i+2]);
}
if (f[i]=="+")
{
printf("+");
}
if (f[i]=="-")
{
printf("-");
}
}
getchar();
return 0;
}
int simple(char x)
{
int n;
n = atoi(&x);
printf("n");
printf("%d", n);
printf("x^");
printf("%d", n-1);
return 0;
}