Hey, basically the goal of the assignment was to create a program that would prompt the user to enter in the m and b values for the equation y = mx + b and then proceed to find the root of the linear function.
Conditions:
-Must use a loop
-Must not set the y equal to zero then solve for x.
-Root will be between x=-100 and x=100
-User will not input a slope of zero
My approach:
So far, I have created a for loop that would break if y was between -1 and 1
for (x = -100; x <= 100; x++)
{
y = slope*x + yIntercept;
cout << y << endl;
if (y > -1 && y < 1) break;
}
My hope was that where the root = 0, the y value would be zero
(between -1 and 1)
However, instead of finding where the root existed, the program would go from the entire range of -100 to 100.
Any help would be appreciated, I do not know if I am using the wrong approach, or if my loop is incorrect.
As far as I can tell, that loop looks correct. But just a couple things:
1. You can put the if condition in the for condition. You just need to negate it and and it to the condition that's already there. This won't make the program more correct, but it's better to avoid breaks if possible.
2. Is y an integer or a floating point number? If it's an integer, just check for equality to zero. If it's a float, the most common approach for comparing to zero is this: if (x<epsilon && x>-epsilon)
epsilon is a very small value (say, .0001. You'll choose one depending on your application). We do this for a reason that's too complicated to explain here. All you need to know is that you never check for equality when one of the values is a floating point.
3. x is probably being incremented by too large a value. Try incrementing it by epsilon.