Slope Calculator

#include <iostream>
using namespace std;
int main()
{
int x1;
int y1;
int x2;
int y2;

cout<< "\f";
cout<<"This program will calculate the slope of the two coordinates you put in."<<endl;
cout<<"For your first pair of coordinates, enter in your \"x\" coordinate ";
cin>> x1;
cout<<endl;

cout<< "\f";
cout<<"Now enter in your \"y\" coordinate ";
cin>> y1;
cout<<endl;

cout<< "\f";
cout<<"Then for your second pair of coordinates, enter in your \"x\" coordinate ";
cin>> x2;
cout<<endl;

cout<< "\f";
cout<<"Finally, enter in your \"y\" coordinate ";
cin>> y2;
cout<<endl;

cout<< y2 << " - " << y1
cout<<
cout
cout
cout
return 0;
}



How would I do y2-y1 and x2-x1 before it would divide? Are there grouping symbols I can use to indicated which one should go first? How would I do that?
Not sure what you're asking exactly. Something like this?
Edit - I see what you were trying to do. Is this what you were asking?
1
2
3
4
5
6
7
8
9
	float x1 = 0;
	float y1 = 0;
	float x2 = 2;
	float y2 = 1;

	float y_delta = (y2 - y1);
	float x_delta = (x2 - x1);

	float slope = (y_delta / x_delta);
Last edited on
closed account (48T7M4Gy)
1
2
3
4
5
6
float x_delta;
/*
*/
x_delta = (x2 - x1);
if( x_delta != 0) // <-- 
 slope = (y_delta / x_delta);

you may want to check first to make sure x or y isn't 0.
As far as grouping symbols, use parentheses.

float slope = (y2 - y1) / (x2 - x1);

However, you will be surprised by the result. Because you defined x1, y1, x2 and y2 as integers, the division operation will be integer division. You will get an integer result, and the "remainder" will be discarded. You need to cast one of the operands to a floating point value in order to perform floating point division in order to get the real-number value you would expect. You can use float or double for this purpose.

float slope = (float)(y2 - y1) / (x2 - x1);
Topic archived. No new replies allowed.