Pages: 1234
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.

[b]Program Solution[/b]

#include <iostream>
#include <conio.h>
using namespace std;
// Main Function
int main()
{
int y1,y2,x1,x2;

double resultSlope;
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;
resultSlope =(y2-y1)/(x2-x1);
if (resultSlope=1)
cout<< "Line will make the 45 degree angle with the horizon"<< endl;
if (resultSlope<1)
cout<< "Line wil travel more along X-axis & less along Y-axis"<< endl;
if (resultSlope>1)
cout<< "Line wil travel more along Y-axis & less along X-axis"<< endl;
{
cout<<"Press any Key to continue...[/sup]." ;
getch();
}
}
Last edited on
If x2==x1, then you will attempt to divide with 0. That would be an error. Besides, parallel to axis is a special case that you have to report separately. Same for y1==y2.

Integer division drops remainder, but comparing floats is bad too. In this case there is a third option.

if .. if .. if convert that to if .. else if .. else if. If resultSlope is already known to be 1, then the following if clauses will fail.

You don't return a value from function that you declare to return an integer. Return 0 as sign of success.
I try if ... else but their is Syntax error found. If u find out the errors detail i will try for my best.
Thankx
@keskiverto
 ``123456789`` ``````if ( 1 == resultSlope ) { cout<< "Line will make the 45 degree angle with the horizon"<< endl; } else if ( resultSlope < 1 ) { cout<< "Line wil travel more along X-axis & less along Y-axis"<< endl; } else if ..``````

Btw. `if (resultSlope=1)` first assigns 1 to be the value of resultSlope, then converts the return value of the assignment (which is 1) into boolean (i.e. to true) and finally uses that true to determine whether the statement following the if should be executed. Testing for equality requires ==. Placing a constant value on the left, as I have written, lets the compiler to notice if you accidentally type only one =.
Program only run his statement only. I change the values but their is no change in the progam runing :(.
Line wil travel more along X-axis & less along Y-axis

Last edited on
 ``1234567891011121314`` ``````if ( resultSlope=1 ) { cout<< "Line will make the 45 degree angle with the horizon"<< endl; } else if ( resultSlope < 1 ) { cout<< "Line wil travel more along X-axis & less along Y-axis"<< endl; } else if ( resultSlope > 1 ) cout<< "Line wil travel more along Y-axis & less along X-axis"<< endl; cout<<"Press any Key to continue...." ; getch(); }``````

it only print out only
Line will make the 45 degree angle with the horizon
Last edited on
probably because of your formula try something like this
 ``12345`` ``````int xMove = x2 - x1; int yMove = y2 - y1; if(xMove == yMove) std::cout << "Moves same on x and y axis." << std::endl; else if(xMove > yMove) std::cout << "Moves more on x than y axis." << std::endl; else if(xMove < yMove) std::cout << "Moves less on x than y axis." << std::endl;``````
`if ( resultSlope=1 )`
is assignment, not comparision, and will always evaluate to true.

Use ` if ( resultSlope == 1 )`
if I use `if ( resultSlope == 1 )` the program print out
Line wil travel more along X-axis & less along Y-axis.

The formula is not working I think .` resultSlope =(y2-y1)/(x2-x1);`
@cire
Well did you not read my post? I think that would work better than that divide thing lol
:@giblit where u post ?
giblit's solution is a good one & would work along as you only wanted to do exactly what was in the assignment. It is not clear whether it should work for ints and / or doubles, but one could easily be forgiven for thinking they wanted int.

Using doubles would be a much more elegant solution IMO, & might be worth some brownie points.

But resultSlope is a double, so `if ( resultSlope == 1 )` will hardly ever be true - even when (int)(y2 - y1) == (int)(x2 - x1). I can see your intention by using ints, but this is a bad idea, because of the division.

Doubles are stored as binary fractions and cannot represent every real number.

 http://www.cplusplus.com/forum/beginner/99565/#msg535515

Also - the variables x1, x2, y1, y2 should be doubles, but make sure you check for division by zero. Your use of ints does not take this into account nor the truncation.

Hope all goes well.
I still think even if they are doubles he should just subtract them from each other and not do the divide method so that way you can be save that either the denominator or numerator are not 0's And using doubles for if statements can be tricky if you are using == because like you said they are not exact 4 could be 3.9999999999999999999999 and 5 could be 5.0000000000000000000002 but looks like you found a way how to solve that problem on your link (I'm still new so I just convert my doubles to strings and take only the amount of decimal places I want but probably isn't the best solution.)
x1=10
y1=10
x2=20
y2=18

the print out "Line wil travel more along X-axis & less along Y-axis
if i change the values the print out remain same...

Can you post your new code. Make sure to use the code tags. Select your code then press the <> button on the right under the format menu.

@giblit
 I still think even if they are doubles he should just subtract them from each other and not do the divide method .....

Yes, that's right.

20 - 10 = 10 x Change
18 - 10 = 8 y change
so it does travel more along the x-axis than on the y-axis.
and if you are getting that each time in your loop you probably are not resetting the values.
Division should be accurate for any integer input the OP is likely to receive (as those values can be represented precisely,) thus, use int for input and don't worry about epsilon for this particular problem.

Your problem description is wrong, however, as is giblit's code.

If the x difference is -2 and the y difference is 2, what is the slope?
Are the differences equal?

If the y difference is 8 and the x difference is -2, that makes the slope -4. Is -4 > 1?
`abs(x2-x1)`
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include #include using namespace std; // Main Function int main() { int y1,y2,x1,x2; double resultSlope; 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; resultSlope =(y2-y1)/(x2-x1); if ( resultSlope==1 ) { cout<< "Line will make the 45 degree angle with the horizon"<< endl; } else if ( resultSlope < 1 ) { cout<< "Line wil travel more along X-axis & less along Y-axis"<< endl; } else if ( resultSlope > 1 ) cout<< "Line wil travel more along Y-axis & less along X-axis"<< endl; cout<<"Press any Key to continue...." ; getch(); }``````

look at the codes
Not much better. Integer division is still integer division. Storing it into double is actually worse than int.

Besides you cannot do that division like that. It is not safe.

Furthermore, you still don't do the two more cases.

Restart from giblit's model. Take absolute values of the differences.

Formally, there are six cases:
 ``123456`` ``````dx == dy == 0 dx == 0 dy == 0 dx == dy dx < dy dy < dx``````
Pages: 1234