

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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
- Related Questions & Answers
- How to find the 95% confidence interval for the slope of regression line in R?
- How is the slope of a capital market line (Sharpe Ratio) defined?
- Find the slope of the given number using C++
- Program to find the mid-point of a line in C++
- How to add a regression line to a plot in base R if intercept and slope are given?
- Program to find the number of possible position in a line in Python
- How to create a scatterplot with regression line using ggplot2 with 0 intercept and slope equals to 1 in R?
- Program to find position of first event number of line l of a triangle of numbers in Python
- Program to find number of sets of k-non-overlapping line segments in Python
- Program to find line passing through 2 Points in C++
- Program to Find Out the Number of Moves to Reach the Finish Line in Python
- C++ Program to Apply Above-Below-on Test to Find the Position of a Point with respect to a Line
- Program to find out the number of integral coordinates on a straight line between two points in Python
- Java Program to output fixed number of array elements in a line
- C++ program to find the number of triangles amongst horizontal and vertical line segments
Advertisements