Displaying graph equations

Hi everyone, I'm creating a program where I have to display the equations y=mx+b and x=a and display whether the line is horizontal, vertical, increasing, or decreasing. I need some assistance with the vertical line code along with other things.

Here's my code:
#include <iostream>
using namespace std;

int main() {
// declare variables
int x1, y1, x2, y2;
int m, b;
// Prompt user
cout << "Please enter two pairs of points: " << endl;
cin >> x1 >> y1 >> x2 >> y2;

// finding the slope and y-int
m = (y2 - y1) / (x2 - x1);
b = y1 - (m * x1);

// determine if line is increasing or decreasing
if (x1 == x2 && y1 == y2) {
cout << "y=" << m << "x +" << b << endl
<< "This line is increasing." << endl;
}
// determine if is vertical or horizontal
else if (x1 == x2 && y1 != y2) {
cout << "x=" << a << endl
<< "This line is vertical." << endl;
}
else if (y1 != y2 && x1 != x2) {
cout << "y=" a << endl
<< "This line is horizontal." << endl;


}


system("PAUSE");
}
if (x1 == x2 && y1 == y2) this means the 2 points are totally identical, and there is no line at all.

I would think it is increasing if y2 > y1 assuming order of point entry is left to right. If the points can be anywhere you have to figure out which is left and which is right also.

vertical looks ok... xs are the same, ys are not, that is correct.

but horizontal is wrong, ys should be equal.

Basically, your code looks about right but the math is not correct.
Thanks jonnin for letting me know. I will get to that. :)
And also, if I wanted to display "x=" << a <<, what would i have to do in order to compute that?
Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post, highlight your code and click the <> button on the right.

1
2
int x1, y1, x2, y2;
int m, b;

Be sure to initialise your variables, as when input fails these variables will end up with garbage values, leading to undefined behaviour.

 
m = (y2 - y1) / (x2 - x1);

What happens when x2 equals to x1?

1
2
3
4
5
// determine if line is increasing or decreasing
if (x1 == x2 && y1 == y2) {
cout << "y=" << m << "x +" << b << endl
<< "This line is increasing." << endl;
}

Wouldn't the logical method be to check the gradient(m)? Likewise, with your horizontal and vertical checks.
Topic archived. No new replies allowed.