Cho tọa độ hai điểm trên đường thẳng. Tính độ dốc (slope) của đường thẳng đó.
Ví dụ
Input: p1(-1, 1), p2(3, 3)

Output: 1/2 = 0.5
Công thức
Độ dốc = (Y2 - Y1) / (X2 - X1)
Ví dụ minh họa (C++)
#include <iostream>
using namespace std;
float calcSlope(float point[2][2]) {
float slope = (point[1][1] - point[0][1]) / (point[1][0] - point[0][0]);
return slope;
}
int main() {
float points[2][2] = {{-1, 1}, {3, 3}};
cout << "The slope of the line is " << calcSlope(points);
return 0;
}
Đầu ra
The slope of the line is 0.5