Line calculator

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <conio.h>
using namespace 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;
		}
}
Last edited on
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.
closed account (48T7M4Gy)
A slight change, b1 and b2 are just there as a double check.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
using namespace std;

void calculations();

int main() {
    
    calculations();
    return 0;
}

void calculations() {
    int x1 = 0, y1 = 0;
    int x2 = 0, y2 = 0;
        
    double b1 = 0;
    double b2 = 0;
        
    cout << "Line Calculator\n";
    
    cout << "Please enter in your first x point\n";
    cin >> x1;
    
    cout << "Please enter in your first y point\n";
    cin >> y1;
    
    cout << "Please enter in your second x point\n";
    cin >> x2;
    
    cout << "Please enter in your second y point\n";
    cin >> y2;
    
    if(x2 == x1)
    {
        cout << "Line is vertical through x = " << x1 << '\n';
        return;
    }
    else
    {
        double m = (y2 - y1) / (double)(x2 - x1); // <--
        cout << m << endl;
        
        if (m == 0) {
            cout << "Line is horizontal\n";
        }
        
        if (m > 0) {
            cout << "Line is increasing\n";
        }
        
        if (m < 0) {
            cout << "Line is decreasing\n"; // <--
        }
 
        b1 = y2 - m * x2;
        b2 = y1 - m * x1;

        cout << "y" << " = " << m << "x" << " + " << b1 << '\n';
        cout << "y" << " = " << m << "x" << " + " << b2 << '\n';

        return;
    }
}
Last edited on
I ended up fixing it earlier, my issue was the data types lol
Thank you for the input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <conio.h>
using namespace 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
		}
}
Last edited on
Topic archived. No new replies allowed.