Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1. The slope m of the line between the two points (x1, y1) and (x2, y2) is given by the formula
2 1
2 1
x x
y y
x
y
m






Write a program that will ask the user for the coordinates of two points and do the following:
 Determine if the two points form a line.
 Detect if the line is horizontal and alert the user.
 Detect if the line is vertical and alert the user.
 Calculate the slope of the line segment connecting them, if not 0 or undefined.
Use double-type variables for storing the coordinates and calculations made from them.
Sample Run:
Enter the coordinates of point 1 (x, y): 3 8
Enter the coordinates of point 2 (x, y): 3 8
Alert! These points do not form a line.
Sample Run:
Enter the coordinates of point 1 (x, y): 3 3
Enter the coordinates of point 2 (x, y): 7 8
The slope of the line is 1.25
Sample Run:
Enter the coordinates of point 1 (x, y): -2 9
Enter the coordinates of point 2 (x, y): 4 9
Alert! The line is horizontal.
Sample Run:
Enter the coordinates of point 1 (x, y): 5 -6
Enter the coordinates of point 2 (x, y): 5 9
Alert! The line is vertical.
Next modify the program to print the equation of the line in slope-intercept form (
y  mx  b
). Use the
appropriate “y = ” and “x = ” for horizontal and vertical lines, respectively.
Sample Run:
Enter the coordinates of point 1 (x, y): 3 5
Enter the coordinates of point 2 (x, y): 5 8
The slope of the line is 1.50
The slope-intercept form is y = 1.50x + .50

What I have tried:

C++
#include <stdio.h>
/* function main begins program execution */
int main(void)
{
float slope, intercept;
float x1, y1, x2, y2, x3, y3;
float dx, dy;

/*coordinate phase*/
printf("Enter the coordinates of point 1: "); /* prompt for input */
scanf("%f", &x1); /*coordinate from user*/
scanf("%f", &y1); /*coordinate from user*/
printf("Enter the coordinates of point 2: "); /* prompt for input */
scanf("%f", &x2); /*coordinate from user*/
scanf("%f", &y2); /*coordinate from user*/
printf("\n");
printf("\n");

/*slope phase*/
dx = x2 - x1;
dy = y2 - y1;
slope = dy / dx;

printf("The slope of the line is = %.5f", slope); /* display result */
printf("\n");
printf("\n");

/*y intercept phase*/
/* y = mx + b*/
/*intercept b = y - m*/
intercept = y1 - slope * x1;

/*line equation phase*/
printf("The slope intercept form is: y = %.5fx %c %.5f\n", slope, (intercept < 0) ? ' ' : '+', intercept); /* display result */
printf("\n");
printf("\n");

return 0; /* indicate that program ended successfully */
} // end function main *
Posted
Updated 5-Oct-17 21:25pm
v2
Comments
Rick York 5-Oct-17 22:03pm    
This section is for asking questions so what is your question?
Rick York 5-Oct-17 22:05pm    
One comment : watch out if x1 is equal to x2. That will result in division by zero and that is a bad thing.
«_Superman_» 6-Oct-17 0:58am    
I executed your code and got the output given below.
What is your question?

Enter the coordinates of point 1: 2.3
4.5
Enter the coordinates of point 2: 6.7
8.6


The slope of the line is = 0.93182

The slope intercept form is: y = 0.93182x + 2.35682

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900