# CS201 Introduction to Programming Assignment 1 Solution Spring 2013

Problem Statement:

Write the C++ language program to calculate the slope of the line that is passing through two points and displays one of the following five messages according to the nature of the slope.

 Message No Description 1 Line will make the 45 degree angle with the horizon 2 Line will travel more along X-Axis and Less along Y-Axis 3 Line will travel more along Y-Axis and Less along X-Axis 4 Line is parallel to Y-Axis. 5 Line is parallel to X-Axis.

Detailed Description:

Your program will ask for the two inputs for the starting point P1(x1,y1) of the line i.e. X-Coordinate and

Y-Coordinate, and two inputs for the ending point P2(x2,y2) of the line i.e. X-Coordinate and Y-Coordinate.

Formula to calculate the slope of the line is

Slope = difference between y coordinates/difference between x coordinates

Slope can be one of the following five types depending upon its value.

• Slope=1

Line will make the 45 degree angle with the horizon

• Slope<1

Line will travel more along X-Axis and Less along Y-Axis

• Slope>1

Line will travel more along Y-Axis and Less along X-Axis

• Your program should have a check for the value of difference between X coordinates of the two points i.e. dx. In case difference between x coordinates is zero then slope should not be calculated in your program and following message should be displayed.

Line is parallel to Y-Axis.

• Your program should have a check for the value of difference between Y coordinates of the two points i.e. dy.  In case if the difference between y coordinates is zero then following message should be displayed.

Line is parallel to X-Axis.

For example Consider a line which passes through two points p1(10, 10) and p2(20, 18).

Your program should ask for X-coordinate of the starting point i.e. 10

Then it should prompt for the Y-coordinate of the starting point i.e.  10

Similarly your program will ask for the X and Y-coordinates of the ending point of the line i.e. 20 and 18 respectively.

Now difference between y coordinates is dy =  = 18 – 10 = 8

Whereas difference between x coordinates is dx =  = 20 – 10 = 10

Then message should be displayed according to the nature of the slope of the line.  In this case following message will be displayed.

Line shall travel more along X-axis and Less along Y-axis.

Solution:

```#include <iostream>
// <stdlib.h> is for system("pause");
#include <stdlib.h>
int main()```
```{
using namespace std;
/*```
```      Please pray for me....
Before submitting please change variables and then save against yourname.cppl

Question: Write a program to calculate the slope of the line
that is passing through two points and display 5 nature of slopw.

*/

int X1, X2;
int Y1, Y2;

float slope, SlopX, SlopY;

cout "Enter The X-Coordinate of the starting point = ";
cin >> X1;```
```      cout "Enter The Y-Coordinate of the starting point = ";
cin >> Y1;

cout "Enter the X-Coordinate of the Ending point = ";
cin >> X2;

cout "Enter the Y-Coordinate of the Ending point = ";
cin >> Y2;

SlopX = (X2 - X1);

SlopY = (Y2 - Y1);

slope = (SlopY) / (SlopX);

if(SlopX == 0){```
```       cout "Line is parallel to Y-Axis" endl;
}

else {

if(SlopY == 0){
cout "Line is parallel to X-Axis" endl;
}

else {
if(slope == 1){
cout "Line will make the 45 degree angle with the horizon" endl;
}

else{

if(slope < 1){
cout "Line will travel more along X-Axis and Less along Y-Axis" endl;
}

else {

if(slope > 1){
cout "Line will travel more along Y-Axis and Less along X-Axis" endl;
}
}
}

}

}
system("pause");
return false;
}```