check my code pls.

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.
Last edited on
What are you supposed to do when x1==x2? Aren't you supposed to handle this somehow?
.
Last edited on
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;

return 0;
}
have you tried to run it and see if it is successful ?
I think it works cuz when i type in both x as 1 , the equation of the line is 1.
is that right?
Isn't the formula to find y - intercept ( b ) should be:
b = y2 - ( m * y1 ); ?

And as the instruction says if x1 == x2 it should say the slope is undefined, not x1
Last edited on
@nevermore28,
If y = mx + b, then b = y - mx.

@boy3005
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.
closed account (NUj6URfi)
Please use code tags in the future.

You use:
[code]
to start it and
closed account (NUj6URfi)
[/code] to end it
Topic archived. No new replies allowed.