### How do you make C++ solve equations? Hey, I'm new too C++ so I have a pretty simple question I guess.

How do you make the program find x in the following...

5 = 2 + x

So yeah that's it just wondering what I have to do to solve for x. you write x = 5-2; !
really, if you want to make your program solve equations, you'll have to write some code. (and it won't be too easy)
Last edited on is it preset integer variables ? like var1 = 5; if so..
 ``12345`` ``````var1 = 5; var2 = 2; varX = x; varX = var1 - var2; std::cout << var1 << "=" << var2 << "+" << varX << std::endl;``````

See what's going on? Code it so that X or the value subtracted from the other integers...i'll let you play with the idea : ) -> Just know this is 1 idea,..not "the" idea. How hard is it? Firstly, what result do you expect?
If you want to be able to write `5 = 2+x;` in your code, it's not possible.
If you want to print "enter an equation:", then when user enters "5=2+x" print "x = 3 !!!", it is possible. How hard it is depends on the complexity of equations. For linear equations it wouldn't be hard at all.
The hardest part would be parsing the string.
If you really want to do it, for practice write a calculator which could calculate "2+3-4" or something. I'd then explain the rest (if needed). Ok so you can't code it to find the value of a variable in a linear equation? yes
 ``12345`` ``````int main(){ int x; 5 = 2+x; return x; }``````

can never work. Hi hamsterman why don't you show ProAlbino your small little equation solving functions? You even have source code where he can learn! try this. Hope this help.

 ``1234567891011121314`` ``````#include using namespace std; int main() { /*5 = 2 + x find the value of x */ cout << "the value of x in (5 = 2 + x) is " << 5 - 2; return 0; } ``````

http://codewall.blogspot.com @sohguanh
apparently that's not what he wants. also, I have a feeling a link wouldn't be helpful at all..

by the way, I was bored so...
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364`` ``````#include struct VAR{ float i; }; struct LINE{//k*x+a float a, k; VAR* x; LINE(){} LINE(int a) : a(a), k(0), x(0) {} LINE(VAR& v) : a(0), k(1), x(&v) {} }; LINE operator + (LINE A, LINE B){//assumes that A.x == 0 or B.x == 0 or A.x == B.x LINE R; R.a = A.a + B.a; R.k = A.k + B.k; if(A.x) R.x = A.x; else R.x = B.x; return R; } LINE operator - (LINE A, LINE B){//same as + LINE R; R.a = A.a - B.a; R.k = A.k - B.k; if(A.x) R.x = A.x; else R.x = B.x; return R; } LINE operator * (LINE A, LINE B){//assumes that A.x == 0 or B.x == 0 LINE R; R.a = A.a * B.a; R.k = A.k * B.a + B.k * A.a; if(A.x) R.x = A.x; else R.x = B.x; return R; } LINE operator / (LINE A, LINE B){//assumes that B.x == 0 LINE R; R.a = A.a / B.a; R.k = A.k / B.a; R.x = A.x; return R; } void operator == (LINE A, LINE B){ LINE C = A - B; C.x->i = -C.a/C.k; } int main(){ VAR x; 5 == (2 + (x-7)*10)/2; std::cout << "x = " << x.i; std::cin.get(); return 0; }``````

:)
Last edited on
Topic archived. No new replies allowed.