Not entirely sure what's going on here. The program is supposed to show if the entered points will make a line that is horizontal, increasing, decreasing, vertical, or not vertical.
My first issue, is that for line 40 where it checks if x1==x2, You would get a divide by zero error when you calculate the slope so you would have to break out of the program.
My second issue, is I believe my math on line 47 when I initiate b. I'm still attempting to get this correct.
Any other issues that you see, I would greatly appreciate input on. Thank you.
#include <iostream>
#include <conio.h>
usingnamespace std;
void calculations();
int main() {
calculations();
_getch();
return 0;
}
void calculations() {
cout << "Line Calculator" << endl;
cout << "Please enter in your first x point" << endl;
int x1;
cin >> x1;
cout << "Please enter in your first y point" << endl;
int y1;
cin >> y1;
cout << "Please enter in your second x point" << endl;
int x2;
cin >> x2;
cout << "Please enter in your second y point" << endl;
int y2;
cin >> y2;
double m = (y2 - y1) / (x2 - x1);
cout << m << endl;
if (m == 0) {
cout << "Line is horizontal" << endl;
}
if (m > 0) {
cout << "Line is increasing" << endl;
}
if (m < 0) {
cout << "Life is decreasing" << endl;
}
if (x1 == x2) {
cout << "Line is vertical" << endl;
}
else {
cout << "Line is not vertical" << endl;
int y = y1;
int x = y2;
int b = y - (m * x);
cout << "y" << " = " << m << "x" << " + " << b;
}
}
As you quite correctly say, if "x1" and "x2" are the same, line 29 will result in a division by zero. This makes sense, because a vertical line's slope is, by definition, undefined.
This tells you that vertical lines are a special case. You may want to determine if "x1" and "x2" are the same first - before determining the slope. If they are the same, you know you have a vertical line. Otherwise, calculate the slope.
#include <iostream>
#include <conio.h>
usingnamespace std;
void calculations(); //Prototype
int main() {
calculations(); //Function call
_getch();
return 0;
}
/****************************
This function computes the
line, displays y=mx+b if
it's not vertical
****************************/
void calculations() {
cout << "Line Calculator" << endl;
cout << "Please enter in your first x point" << endl;
double x1;
cin >> x1;
cout << "Please enter in your first y point" << endl;
double y1;
cin >> y1;
cout << "Please enter in your second x point" << endl;
double x2;
cin >> x2;
cout << "Please enter in your second y point" << endl;
double y2;
cin >> y2;
double m = (y2 - y1) / (x2 - x1); //Slope calculation
if (m == 0) { //If slope = 0
cout << "Line is horizontal" << endl;
}
if (m > 0) { //If slope is greater than 0
cout << "Line is increasing" << endl;
}
if (m < 0) { //If slope is less than 0
cout << "Line is decreasing" << endl;
}
if (x1 == x2) { //If points x1 and x2 are =
cout << "Line is vertical" << endl;
}
else {
cout << "Line is not vertical" << endl; //If x1 and x2 are not equal
double y = y1;
double x = y2;
double b = y - (m * x); //Calculation for B
cout << "y" << " = " << m << "x" << " + " << b; //Final display if not vertical
}
}