- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find slope of a line in C++
In this problem, we are given the coordinates of two points of a line. Our task is to create a Program to find slope of a line in C++.
Problem Description − We will find the slope of the line using the coordinates of two points on the line that are given.
Let’s take an example to understand the problem
Input
p1(-1, 1), p2(3, 3)
Output
½ = 0.5
Solution approach
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 = (Y2 - Y1)/(X2 - X1)
Program to illustrate the working of our solution
Example
#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); }
Output
The slope of the line is 0.5
Advertisements