Instuction: Write a program that finds the equation of the line determined by two given points.For the two points you should use 4 variables double x1, y1, x2, y2; Once the user enters these values, the program continues by determining the equation of the line passing through (x1, y1) and (x2, y2).
Note, that there are two cases:
1. Slope is undefined (vertical line, i.e. x1 == x2)
2. Slope is defined. In the latter case, you will also have to compute the y-intercept. Use double m, b; for slope and y-intercept respectively.
The code should print the equation of the line
#include <iostream>
#include <math.h>
using namespace std;
int main() {
double x1, y1, x2, y2, m, b;
cout << " Enter first x value."<<endl;
cin >> x1;
cout << " Enter first y value."<<endl;
cin >> y1;
cout << " Enter second x value."<<endl;
cin >> x2;
cout << " Enter second y value."<<endl;
cin >> y2;
m =(y2-y1)/(x2-x1);
b = y1-( m * x1);
cout << " The equation of the line is " << "y" << "=" << m << "x" << "+" << b << endl;
return 0;
}
Is this correct and in according to the instructions? Thank you.
Well I guess it depends how you want to handle it. Somehow warn the user or return an undefined slope. Why would you want to use a function? Run your program when X1==X2 and decide if you are satisfied with the output.
is it correct now?
#include <iostream>
using namespace std;
int main() {
double x1, y1, x2, y2, m, b;
cout << " Enter first x value."<<endl;
cin >> x1;
cout << " Enter first y value."<<endl;
cin >> y1;
cout << " Enter second x value."<<endl;
cin >> x2;
cout << " Enter second y value."<<endl;
cin >> y2;
m =(y2-y1)/(x2-x1);
b = y1-( m * x1);
if ( x1 == x2 )
cout << " The equation of the line is " << x1 <<endl;
else
cout << " The equation of the line is " << "y" << "=" << m << "x" << "+" << b << endl;
I think it works cuz when i type in both x as 1 , the equation of the line is 1.
is that right?
One dataset is unlikely to prove you right but it might be sufficient to prove you wrong. Take maybe one point in each quadrant. Calculate by hand, then with your program and check if output matches in each case.