Program to find slope of a line in C++


In this article, we will learn how to find the Slope of a line using a program. But before we proceed into the program, let's first understand what the Slope of a line represents in mathematics and how to compute it using its mathematics formula in C++.

In mathematics, the Slope of a line is a numerical value that measures the line’s steepness and direction. It indicates the rate at which the line ups or down as you move along it from left to right (or horizontally). Generally, it is denoted by the letter "m". In mathematics, the steepness refers to the slope or gradient of a line.

In terms of mathematical, it stands for the alteration in the vertical direction (y-axis) to the alteration in the horizontal direction (x-axis) between any two points on the line. This ratio can be expressed as m = change in y/change in x.

To find the Slope of a line, you must have two points on the line. Let’s say (x1, y1) and (x2, y2) to be such points pairs on the draw line. Then, the slope m is calculated as below −

Slope (m) = (Y2 - Y1)/(X2 - X1)

Let's go through the input and output scenario to understand how input values relate to the desired output −

Input

p1(-1, 1),  p2(3, 3)

Output

0.5

Explanation

Using slope (m) formula =(Y2 - Y1)/(X2 - X1), Let the values be:

x1 = -1, y1 = 1
x2 = 3,  y2 = 3
The slope of line = 1/2 = 0.5

Following is the diagram, where we draw a Slope of a line and mention two points having the (x1, y1) and (x2, y2.) axis values. Same point values will be used in the program to find the Slope of a line −

Here's a step-by-step process to find the slope of a line −

Identify two coordinates with values on the line. To simplify this expression, we can call them (x1, y1) and (x2, y2).

Calculate the change in the vertical direction (Δy) by subtracting the y-coordinates of the two points.

Δ𝑦=y2−y1.

Calculate the change in the horizontal direction (Δ𝑥) by subtracting the 𝑥-coordinates of the two points.

Δx=x2−x1.

To find the slope of the line, we will use the geometrical formula defined to find the slope of a line using any two points p1(x1, y1) and p2(x2, y2) that lie on the line.

Slope (m) = Δy/Δx = y2−y1/ x2−x1 (Here Δ𝑥 and Δ𝑦 means change in movement.)

Example

Following is the program to find the Slope of a line in C++ −

#include <iostream>
#include <iomanip> 
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 " << fixed << setprecision(2) << calcSlope(points) << endl;
   return 0;
}

Output

The slope of the line is 0.50

Following are some key points about the Slope of a line −

  • The slope can be positive, negative, zero, or undefined, according to how the line slants up, slants down, or has no form.
  • If the slope is positive, the line will go up from the left to the right-hand side. It will be a series of increases moving from the left to the right side.
  • If the slope is still zero, the line is horizontal. However, this slope can also lead to an undefined slope, which means that the line is vertical.

Updated on: 22-May-2024

867 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements