Problem description

In mathematics, the slope of a line is a number that describes both the direction and the steepness of the line. Slope is calculated by finding the ratio of the vertical change to the horizontal change between any two distinct points on a line. The steepness of a line is measured by the absolute value of the slope. A slope with a greater absolute value indicates a steeper line. The direction of a line is either increasing, decreasing, horizontal, or vertical:

Slope is calculated by finding the ratio of the vertical change to the horizontal change between any two distinct points on a line. The steepness of a line is measured by the absolute value of the slope. A slope with a greater absolute value indicates a steeper line. The direction of a line is either increasing, decreasing, horizontal, or vertical:

  • A line is increasing if it goes up from left to right. The slope is positive, i.e., m > 0.
  • A line is decreasing if it goes down from left to right. The slope is negative, i.e., m < 0.
  • If a line is horizontal the slope is zero. This is a constant function.
  • If a line is vertical, the slope is undefined.

In mathematical language, the slope of the line connecting points (X1,Y1 ) and (X2,Y2) is:

m = delta y / delta x = vertical change / horizontal change = rise / run = Y2 - Y1 / X2 - X1

The formula fails for a vertical line, i.e., one that parallels the y axis. The slope is infinite, so the slope of a vertical line is considered undefined.

If y is a linear function of x, then the coefficient of x is the slope of the line created by plotting the function. Therefore, if the equation of the line is given in the form

y = mx + b

then m is the slope. This form of a lines equation is called the slope-intercept form, because b can be interpreted as the y-intercept of the line, that is, the y-coordinate where the line intersects the y-axis.

If the slope m of a line and a point (x1,y1) on the line are both known, then the equation of the line can be found using the point-slope formula:

y - y1 = m(x - x1)

The slope of the line defined by the linear equation ax + by + c = 0 is:

- a/b

The line segment connecting points (x1,y1) and (x2,y2) forms the hypotenuse of a right triangle and represents the distance between two points. The Pythagorean Theorem states that the square of the hypotenuse is equal to the sum of the squares of the other two sides, and it can be expressed as the Pythagorean equation:

a2 + b2 = c2

If the length of both a and b are known, then c can be calculated as:

c = square root of (a2 + b2)

If the length of the hypotenuse c and of one side (a or b) are known, then the length of the other side can be calculated as:

a = square root of (c2 - b2)

or

b = square root of (c2 - a2)

Program description

Write a C++ program that reads in two points (x1,y1) and (x2,y2) from standard input. The program writes to standard output the equation of the line, whether the line is vertical, horizontal, increasing, or decreasing, and the length of the line-segment connecting the two points. If l is a vertical line, its equation is x = a for some real number a. If l is a non-vertical line, output its equation in slope- intercept form. Your main function should return 1 if the line is vertical, or 0 if the line is not vertical.

Miscellaneous requirements

Your program should demonstrate at least one if(..else) statement, one switch statement, and one conditional operator. This program demonstrates input and output redirection; there should not be any direct user interaction with this program.

Input specification

Input is read from the stdin stream, using cin. The input consists of four real numbers separated by whitespace and represents the x-y coordinates of two points connected by a line. The numbers are in the order x1 y1 x2 y2.

Sample input

4 5 3 21

Sample interaction

$ echo 4 5 3 21 | ./a.out
Line equation: y = -16x + 69
Slope direction: decreasing
Line segment length: 16.0312
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.