Power rule derivative finding C program prototype
by Zander 11-5-18
#include <stdio.h>
int main()
{
char equation[1000] = "x^(2)";
int equationlength = strlen(equation);
char diff = 'x';
int state1 = 0;
char equationderivative[1000];
char power[1000];
char newpower[1000];
char piece1[1000]; // used to find the newpower
int piece2 = 0; // (might be used) used to find the newpower in addition to piece1
// checking if the equation contains the variable of differentiation:
for(int i = 0; i < equationlength; i++){
if(equation[i] == diff){
state1 = 1;
}
}
// if state1 = 1, then differentiate. Else, return zero because the derivative of
// constant is zero.
if(state1 == 0){
equationderivative[0] = '0';
}
if(state1 == 1){
// find power:
for(int j = 0; j < equationlength; j++){
if(equation[j-1] == '^'){
for(int i = 0; i < 1000; i++){
if(equation[j] == '\0'){
break;
}
power[i] = equation[j];
j++;
}
break;
}
}
//printf("%s",power);
// next step is to apply the power rule:
int powerlength = strlen(power);
for(int i = 0; i < powerlength; i++){
if(power[i] == ')'){
newpower[i] = '-';
newpower[i+1] = '1';
newpower[i+2] = power[i];
break;
}
newpower[i] = power[i];
}
//printf("%s",newpower);
// now we just made our newpower. The next step is to assemble the derivative:
int newpowerlength = strlen(newpower);
for(int i = 0; i < powerlength; i++){
equationderivative[i] = power[i];
}
int derivativelength1 = strlen(equationderivative);
equationderivative[derivativelength1] = '*';
//printf("%s",equationderivative);
//int derivativelength2 = strlen(equationderivative);
int var = 0;
for(int derivativelength2 = strlen(equationderivative); derivativelength2 < 1000; derivativelength2++){
equationderivative[derivativelength2] = equation[var];
if(equation[var] == '^'){
var = 0;
break;
}
var++;
}
for(int derivativelength3 = strlen(equationderivative); derivativelength3 < 1000; derivativelength3++){
if(newpower[var] == '\0'){
var = 0;
break;
}
equationderivative[derivativelength3] = newpower[var];
var++;
}
}
printf("%s",equationderivative);
}
It's pretty straight forward. put in the function as a string of charactors, analyze the string, and apply the power rule.
Note: This code only works for functions such as bx^(a), and for a power, you must have "()" to enclose the power.
This code cannot do functions like f(x)+g(x).