User will enter 4 different points and findSlope (function) will calculate the value for slope and return back the value to be cout in main function.
But when I ran the program, it had logic error for slope value. Any idea why?
#include <iostream>
#include <iomanip>
using namespace std;
float findSlope(float a,float b,float c,float d)
{
return (d-b/c-a);
}
int main()
{
float slope,p1,q1,p2,q2;
cout << "Enter x1: ";
cin >> p1;
cout << "Enter y1: ";
cin >> q1;
cout << "Enter x2: ";
cin >> p2;
cout << "Enter y2: ";
cin >> q2;
slope=findSlope(p1,q1,p2,q2);
cout << "Point1" << "\t\tPoint2" << "\t\tSlope" << endl;
cout << fixed << setprecision(2) << p1 << "," << q1 << "\t" << p2 << "," << q2 << "\t" << slope << endl;
return 0;
}
return (db/ca);
Please look at order of operations again. What you intended to calculate was (db)/(ca)
. Right now you are calculating d - (b/c) - a
. This is a basic mathematical thing and your calculator would most likely give you the same output if you had tried to check this yourself.
Your slope function has
return (d-b/c-a)
did you mean
return ((d-b)/(c-a))
Operator precedence will do the division first, without the brackets.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.