Thread: help plz!!
View Single Post
Old 11-07-2004, 11:24 PM   #3 (permalink)
Redline
PHP Student
 
Join Date: Oct 2004
Location: Forest Grove, OR
Posts: 151
Redline is on a distinguished road
Send a message via AIM to Redline Send a message via MSN to Redline
Also, it looks like your program is only trying to find the x intercept? If that's true, it's unnecessary (and inaccurate) to use a loop. The formula to find the x intercept is

x = -b/m

where:
b = y intercept
m = slope

Explanation (if you want it)

The standard equation is y = mx + b. If you're looking for the x intercept, that is to say the exact point where a line with a given slope(m) and y intercept(b) cross the x axis, then the y value for that point would be zero. Plug that zero into the equation and solve for x to find the forumula for the x intercept:

y = m * x + b (y = 0)
0 = m * x + b (subtract b from both sides)
-b = m * x (divide both sides by m)
-b/m = x

So you could replace your for loop with the following code and achive the same results

Code:
x = -b/m;
cout << x <<endl;
That's a little easier and cleaner isn't it? The reason why you can't really do it the other way is this; when you increment x by .001 per cycle, the exact point at which y = 0 can and will get skipped over. The only time that would work is when x does not exceed 3 decimal places. I hope this was helpful
__________________
Current Project
Redline is offline   Reply With Quote