using C++ please Given two points (x1, y1) and (x2, y2), the slope of the line r
ID: 3879915 • Letter: U
Question
using C++ please
Given two points (x1, y1) and (x2, y2), the slope of the line running through those points is defined as rise / run, or
Recall that the slope is defined as rise / run, or (y2-y1) / (x2-x1). In the previous exercise you were allowed to assume that the run would never be 0, thus avoiding division by 0. This assumption is no longer true.
Since division by 0 is mathematically undefined, your task in this assignment is to check for a run == 0.0, and output “Slope: undefined” in this case. Example: given the inputs
Your program should now output
Explanation / Answer
Here is your code
#include <iostream>
using namespace std;
int main()
{
float x1,x2,y1,y2; // declare variables for first and second points
float slop; // for storing slop
cout<<"Enter X1, Y1 ";
cin>>x1>>y1; // take input from user first point
cout<<"Enter X2, Y2 ";
cin>>x2>>y2; // take input from user second point
cout<<"("<<x1<<","<<y1<<") ";
cout<<"("<<x2<<","<<y2<<") ";
if(x2-x1 == 0) // check for divisible by zero
{
cout<<"slop : undifined"; // if zero print undifined
}
else // else find slop and print it
{
slop = (y2-y1)/(x2-x1);
cout<<"slop : " << slop;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.