Ian Lundberg
Ian Lundberg

Reputation: 1883

how to calculate the value of a variable?

I need to know how to take an equation with a variable and have it calculate and output the value of said variable. example of the equation.

2900 = 1 * T * ((52 + 6) / 12)

I need the program to take all values and give me the value of 'T'. Any and all help will be greatly appreciated :)

Upvotes: 0

Views: 4177

Answers (2)

Elhana
Elhana

Reputation: 309

If equation is the same, only parameters change - then just rearrange it to a variable.

If whole equation is a user input, then it is getting ugly quick ("2*cos(log(x^3)) = - e^tg(x)") and there is no silver bullet. Easiest thing you can do is evaluate it at runtime (for example with NCalc) and "bruteforce" the solutions.

Upvotes: 3

Preet Sangha
Preet Sangha

Reputation: 65506

First rearrange the equation to the form T=....

2900 = 1 * T * ((52 + 6) / 12)

becomes (for example)

T = 2900/(52 + 6) * 12 / 1

Next replace the number with variables

T = a/(b + c) * d / e

then you write a function to calculate T given a-e

double T(double a, double b, double b, double c, double d, double e) {
 return  a/(b + c) * d / e;
}

then you use it like this

double T = T(2900, 52, 6, 12, 1)

Upvotes: 1

Related Questions