Click here to Skip to main content
15,884,083 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I want to solve an equation in asp.net c#

Equation is

C#
int x0 = 5;
     int x1 = 2;


then

C#
y = (x-x0)/(x0-x1)

it should return like

C#
y = (x-5)/3;


But

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


Please help me in solving this.


With Regards

Thanks in Advance

Manju K
Posted
Updated 9-Aug-11 0:39am
v3
Comments
Herman<T>.Instance 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.

Please have a look this site which might help you,

Converting math equations to C#[^]

:)
 
Share this answer
 
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:
C#
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:
C#
public int MyEquation(int x)
{
     int y;

     // here is your code

     return y;
}
 
Share this answer
 
v2
Comments
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;
}
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.

C#
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.
 
Share this answer
 
v2
Comments
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;
dan!sh 9-Aug-11 6:47am    
Yes, I got it right after posting my first reply. I have updated my answer.
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.....



 
Share this answer
 
v2
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.
 
Share this answer
 
v3
That's because you haven't declared x yet.

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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900