Back to General discussions forum
If my understand of the problem is correct, given this formula: y = ax + b
, we are passing in
2 ordered pairs: x1 y1 x2 y2
. Then we are returning a & b.
The first half of my answers are correct, getting a
is just finding the slope: all you've got to do is divide difference of the two y-coordinates by the difference of the two x-coordinates.
But I'm not sure what I'm doing wrong when I'm trying to find b
. What I think I can do is: using the slope formula: y = ax + b
,
a
,(x1, y1)
,(x2, y2)
.(x1 - x2, y1 - y2)
,(x2 - x1, y2 - y1)
.This brute force approach is not working and I'm stuck on this.
What would your approach to finding b
in this problem be?
Your first approach (find a - the slope, then b - the intercept using (x1, y1)) should have worked. There's no need for brute force, only algebra.
Make sure you're returning integer values for a and b only. Without seeing your code it's hard to tell what's going wrong otherwise.
I found the bug in my program. Your suggestion to work through an entire problem in Algebra was a good one!
Turns out I was dividing y
by (m*x)
to get b
. But I should have been subtracting (m*x)
from y
.
Little things like that can be easy to miss when you're processing dozens of lines from an outside file, formatting & the like.
Thanks for your time.
I solved the problem on book. But i'am unable to write code for point-slope formula.
@CruiseDevice:
What code / language do you mean? How to determine a and b, or m and n respectively?
Edit: To avoid spoilers on this forum you might give me a PM, and I'll try to help.
I'am using c++.For solving the Linear function problems there are formulas, which are used to find the values of a and b.There is another formula called point-slope formula, i'am unable to think the logic for implementing that formula. For reference i'am posting this link where an example is shown on how to solve problems related with Linear Function.
http://www.algebra.com/algebra/homework/Linear-equations/Linear-equations.faq.question.530233.html
using m = (y2-y1)/(x2-x1), we get the value of m.
using y-y1 = m(x-x1),we will get the equation for x or y.
Using this newly obtained equation,we can find the value of c.
PS:In this problem the equation given is y(x) = ax+b . Iam using y = mx + c.
I'm quite confused about that point-slope thingy. I usually do
// y = mx + n
// m -> slope; n -> vertical shift
if (x2 != x1)
{
m = (y2 - y1) / (x2 - x1);
n = y1 - m*x1;
}
Right now I don't see a point in y - y1 = m * (x - x1). What's that x and y? Where and how will I obtain them?