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:
#include <stdio.h>
int main(void)
{
float slope, intercept;
float x1, y1, x2, y2, x3, y3;
float dx, dy;
printf("Enter the coordinates of point 1: ");
scanf("%f", &x1);
scanf("%f", &y1);
printf("Enter the coordinates of point 2: ");
scanf("%f", &x2);
scanf("%f", &y2);
printf("\n");
printf("\n");
dx = x2 - x1;
dy = y2 - y1;
slope = dy / dx;
printf("The slope of the line is = %.5f", slope);
printf("\n");
printf("\n");
intercept = y1 - slope * x1;
printf("The slope intercept form is: y = %.5fx %c %.5f\n", slope, (intercept < 0) ? ' ' : '+', intercept);
printf("\n");
printf("\n");
return 0;
}