12,896,953 members (60,347 online)
Rate this:
See more:
Hi

I want to solve an equation in asp.net c#

Equation is

```int x0 = 5;
int x1 = 2;
```

then

`y = (x-x0)/(x0-x1)`

it should return like

`y = (x-5)/3;`

But

It shows an error like x is undefined....

With Regards

Manju K
Posted 9-Aug-11 0:32am
Updated 9-Aug-11 0:39am
Slacker007104.5K
v3
digimanus 9-Aug-11 6:36am

please show all code. in your code above there is no int x;
Syed Salman Raza Zaidi 9-Aug-11 6:36am

Yes, it will give error as where are you assigining value for x??userinput or hardcoded?
BlackJack99 9-Aug-11 6:55am

i think he meant x as the user input and y as the return value.

Rate this:

## Solution 4

Converting math equations to C#[^]

:)
Rate this:

## Solution 1

i don't see you have been declare variable x, y
so that the compiler throws that error

to solve that error, you can declare x, y variable inside your function like:
```int x = 100; // value 100 is an example
int y = 100; // value 100 is an example
int x0 = 5;
int x1 = 2;```

....

or write an function like:
```public int MyEquation(int x)
{
int y;

return y;
}```
v2
manjukgowda 9-Aug-11 6:43am

I want to make it as it should return an equation like y = (x-5)/3;
Van Hua 9-Aug-11 6:46am

public int MyEquation(int x)
{
return (x - 5) / 3;
}
Rate this:

## Solution 2

You need to tell us what exactly you are trying to acheieve here. With the given information, this can be very easily done through String.Format. But does it help you? I think it will not.

```int x0 = 5;
int x1 = 3;
string originalString = "y = (x-{0})/({1})";

string updatedString = string.Format(originalString, x0, (x0 - x1));```

You need to implement a parser of your own which could solve the equations. One needs to be well aware of your requirements to help you out. Search google for "C# Equation Parser". It may give you some ideas to start with.
v2
manjukgowda 9-Aug-11 6:41am

You are not getting my question

I want to make it as it should return like y = (x-5)/3;
d@nish 9-Aug-11 6:47am

Yes, I got it right after posting my first reply. I have updated my answer.
Rate this:

## Solution 3

of course it will ,you haven't declared x,I see you have declared x0 and x1 only , the compiler won't understand what you want till you tell it

the solution of this type of problem is treating with variables of an equation (not of program asp...etc ) as string and using post-fix notation to solve it with some change that you will determine the variable as it is with out value
e.g
(X-x0)/(x0-x1)---post-fix--> Xx0-x0x1-/
the X means that it's variable of an equation solve it using stack I'll post the solution later.....

v2
Rate this:

## Solution 5

Programming languages (usually) don't directly support symbolic algebra manipulation, have a look at Computer Algebra Systems[^] page at Wikipedia to get an idea about what you are trying to do.
v3
Rate this:

## Solution 6

That's because you haven't declared `x` yet.

(Engineers shouldn't be allowed to do their own programming.)

Top Experts
Last 24hrsThis month
 OriginalGriff 300 CHill60 145 ppolymorphe 110 Richard MacCutchan 85 Maciej Los 50
 OriginalGriff 5,472 CHill60 3,673 Jochen Arndt 2,958 Karthik Bangalore 2,456 ppolymorphe 2,435