Declaring variables for use on Calculus deriving program

I'm pretty new to programming and I'm trying to create a deriving program to do derivatives. For now, I would only like to know how I could declare a variable such as derivative to be the equation where the user inputs what each variable is. I am currently just trying to develop a product rule calculating program. I am having trouble do to getting the actual letter x into the equation so I could display the answer as "45x^(3)+12" or such. Is it possible to do so the way I'm going? Or is there a different way to approach this problem? Or maybe would I have to tell the user to input a value for x and the output would be a plain number?
Last edited on
If I was to require the input of an x for power rule, why does the equation a*n*(x^(n-1)) with variables a=2, n=3, x=1 come out to be 18? Here's my Power Rule Code:

#include <iostream>
using namespace std;
int a, x, n, derivative;
int main ()
{
cout<< "Hello. I am a computer program. I can calculate the derivative of an expression using power rule.";
cout<< "\nPlease type the values of a and n for use like the ex.:";
cout<< "\ny=ax^n";
cin.get ();
cout<< "a=?\n";
cin>> a;
cout<< "x=?\n";
cin>> x;
cout<< "n=?\n";
cin>> n;
derivative=a*n*(x^(n-1));

cout<< "dy/dx=" << derivative;
return 0;
}
^ Is the bit wise XOR operator, not the power operator. You'll have to use the math library for a power function.
I found this out on my own by comparing the binary codes and what the answers I was getting. Thank you, though! I just didn't see your post in time. I found that you have to include math.h and example command is pow(2, 3) which would come out to 8. Would you happen to know anything about my original question?
I'm not really sure what you're asking up there. Are you wanting to display the actual 'x' in the equation, or the value of x in the equation? Either way it pretty simple
I want to display x so that x's power could be shown and answers such as 24x+32 or whatever could be shown. Can it (not sure what to call it, the computer? processor?) process an equation with a letter inside and give an answer such as the example, 24x+32? I will post my revised power rule of having to input a value for x.
//Power Rule Program
#include <iostream>
#include <math.h>
using namespace std;

int a, x, n, y, derivative;

int main ()
{
cout<< "Hello. I am a computer program. I can calculate the derivative of an expression using power rule.";
cout<< "\nPlease type the values of a and n for use like the ex.:";
cout<< "\ny=ax^n";
cin.get ();
cout<< "a=?\n";
cin>> a;
cout<< "x=?\n";
cin>> x;
cout<< "n=?\n";
cin>> n;

y=pow(x, (n-1));
derivative=a*n*y;

cout<< "dy/dx=" << derivative;
return 0;
}
Last edited on
First of all, always enter your code using the code tool:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//Power Rule Program
#include <iostream>
#include <math.h>
using namespace std;

int a, x, n, y, derivative;

int main ()
{
cout<< "Hello. I am a computer program. I can calculate the derivative of an expression using power rule.";
cout<< "\nPlease type the values of a and n for use like the ex.:";
cout<< "\ny=ax^n";
cin.get ();
cout<< "a=?\n";
cin>> a;
cout<< "x=?\n";
cin>> x;
cout<< "n=?\n";
cin>> n;

y=pow(x, (n-1));
derivative=a*n*y;

cout<< "dy/dx=" << derivative;
return 0;
}



Second of all, i think this is over my head.
Haven't even learned calculus yet... XD
Last edited on
Didn't know there was a code tool, thanks. Well, it would be helpful if you could help me figure out how to get something simple like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Equation simplifier
#include <iostream>
using namespace std;

int a, b, c, d, x, answerleft, answerright; //I know x couldn't be an integer in this case, correct?

int main ()
{
    cout<< "I can simplify equatinos of format 'ax+c=bx+d'.\nPlease enter the values for a, b, c, and d.\n";
    cin.get ();
    cout<< "a=?\n";
    cin>> a;
    cout<< "c=?\n";
    cin>> c;
    cout<< "b=?\n";
    cin>> b;
    cout<< "d=?\n";
    cin>> d;

    answerleft=a*x-b*x;
    answerright=d-c;

    cout<< answerleft << "=" <<answerright;
    return 0;
}

I would like to be able to plug-in (ex.) a=3, c=2, b=7, and d=12 and have a return answer of -4x=10 not 0=10.
Last edited on
Well part of your problem is, x is never initialized. Not sure why your using it in that equation. I believe anymore, compilers default non-initialized numerics to 0, so your left side will always be 0. If it doesn't initialize for you, it'll just be garbage. Either way, it's bad.
If you simply display answerleft, it will display Integer value but not alphabetic numerals. So you cant display x because it is not integer, it is actually a variable name. I think you want to convert x (but not its value) to String or char type and display it as a variable in an equation. But i think it is very difficult in C/C++ (but i am not sure).
Last edited on
Hmmm perhaps if you know that x is always in the same place you could work around it..


 
cout << answerleftofx << "x" << answerrightofx << "=" << answerright;


Something like that maybe?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Equation simplifier
#include <iostream>
using namespace std;

int a, b, c, d, answerleft, answerright; //I know x couldn't be an integer in this case, correct?

int main ()
{
    cout<< "I can simplify equatinos of format 'ax+c=bx+d'.\nPlease enter the values for a, b, c, and d." << endl;
    cout<< "a= ";
    cin>> a;
    cout<< "c= ";
    cin>> c;
    cout<< "b= ";
    cin>> b;
    cout<< "d= ";
    cin>> d;

    answerleft=a-b;
    answerright=d-c;

    cout<< answerleft << "x=" <<answerright;
    return 0;
}
I know what my problem is in the code and I can work around it' but there are equations that don't work as well when trying to work around. Bibek Subedi is correct in what I was asking and trying to do.
Topic archived. No new replies allowed.