I need help with finishing this C++ program ( I am not getting the correct outpu
ID: 3670199 • Letter: I
Question
I need help with finishing this C++ program ( I am not getting the correct outputs, please tell me what I need to fix too):
Recall, the equation for a straight line in cartesian coordinates (in 2 dimensions) is
y = ax + b
If we have two straight lines, with coefficients {a1, b1} and {a2, b2}, then the lines will either be parallel, or will intersect at some point
{xi = (b2 - b1) / (a1 - a2), yi = a1 * xi + b1}
Write a function called intersect that calculates these coordinates, using reference parameters for xi and yi, and value parameters for a1, b1, a2, b2.
The intersect function should also return an int value used to flag whether the lines are parallel (meaning that a1 == a2, which would cause a divide-by-zero error - make sure you avoid that!).
Use one of 2 global const variables for the return value:
const int OK = 0; // the values of {xi, yi} correctly represent the coordinates of the intersection of the two lines
const int DIV_ZERO = 1; // the two lines are parallel, and the values of {xi, yi} have not been altered.
Your function will NOT take user input, or output anything to screen - all that will be done in main.
The only code you will need to add to the main provided is the actual function invocation.
My code so far( what I actually added to the code is in bold, the rest is a template):
#include <iostream>
using namespace std;
// Global const values for intersect return value
const int OK = 0;
const int DIV_ZERO = 1;
// FIXME Add intersect function here
int intersect ( double& xi, double& yi, double a1, double b1, double a2, double b2)
{
xi= (b2-b1)/(a1-a2);
yi = a1 * xi + b1;
return OK;
}
int main()
{
// line 1: y = a1*x + b1
// line 2: y = a2*x + b2
// lines intersect at xi, yi
double a1 = 0.0, b1 = 0.0, a2 = 0.0, b2 = 0.0;
cout << "Enter coefficients for Line 1 (a1, b1): ";
cin >> a1 >> b1;
cout << endl;
cout << "Enter coefficients for Line 2 (a2, b2): ";
cin >> a2 >> b2;
cout << endl;
cout << "Line 1: Y = " << a1 << "X + " << b1 << endl;
cout << "Line 2: Y = " << a2 << "X + " << b2 << endl;
double xi, yi;
// FIXME: invoke intersect function here
double flag = 0;
flag = intersect(xi, yi, a1, b1, a2, b2);
if (flag == DIV_ZERO)
{
cout << "Lines are parallel" << endl;
}
else
{
cout << " Lines 1 & 2 intersect at {" << xi << ", " << yi << '}' << endl;
cout << "Proof: yi on Line 1 at (xi = " << xi << ") is "
<< a1 * xi + b1 << endl;
cout << " yi on Line 2 at (xi = " << xi << ") is "
<< a2 * xi + b2 << endl;
}
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
const int OK = 0;
const int DIV_ZERO = 1;
int intersect ( double& xi, double& yi, double a1, double b1, double a2, double b2)
{
if(a1!=a2){
xi= (b2-b1)/(a1-a2);
yi = a1 * xi + b1;
return OK;
}
return DIV_ZERO;
}
int main()
{
double a1 = 0.0, b1 = 0.0, a2 = 0.0, b2 = 0.0;
cout << "Enter coefficients for Line 1 (a1, b1): ";
cin >> a1 >> b1;
cout << endl;
cout << "Enter coefficients for Line 2 (a2, b2): ";
cin >> a2 >> b2;
cout << endl;
cout << "Line 1: Y = " << a1 << "X + " << b1 << endl;
cout << "Line 2: Y = " << a2 << "X + " << b2 << endl;
double xi, yi;
double flag = 0;
flag = intersect(xi, yi, a1, b1, a2, b2);
if (flag == DIV_ZERO)
{
cout << "Lines are parallel" << endl;
}
else
{
cout << " Lines 1 & 2 intersect at {" << xi << ", " << yi << '}' << endl;
cout << "Proof: yi on Line 1 at (xi = " << xi << ") is "
<< a1 * xi + b1 << endl;
cout << " yi on Line 2 at (xi = " << xi << ") is "
<< a2 * xi + b2 << endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.