Click here to Skip to main content
15,881,381 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The slope of the line is rise over run or (y2 - y1) / (x2 - x1). Enter value in the order of x1 y1 x2 y2 . do not calculate an undefined slope, ( just print the word "undefined" )

Example:

1 1 2 2 = 1
1 1 3 2 = 0.5
1 2 3 2 = 0
1 1 1 4 = undefined

This question is from Algebra 1, i can solve this using calculator and graph on paper but i am trying to write this program in C++. by the way i am using Microsoft visual C++ i have just downloaded from their website.

i have done this so far on visual studio c++:
how can i print "undefined" if the slope is not valid.

C++
#include <iostream>
using namespace std;

int main()
{
        int x1, y1, x2, y2;
	cout << "enter x1: ";
	   cin >> x1;
	cout << "enter y1: ";
	   cin >> y1;
	cout << "enter x2: ";
	   cin >> x2;
	cout << "enter y2: ";
	   cin >> y2;
	
	cout << y2 - y1 / x2 - x1 << endl;
	

	system("pause");
	return 0;
}
Posted
Updated 22-Oct-13 16:03pm
v2
Comments
Ranjan.D 22-Oct-13 22:16pm    
First of all you should know or ask your teacher for what values of x1 y1 x2 y2 the slope is invalid.

Printing "undefined" is easy as you could do with cout<<"undefined".
danial khan 22-Oct-13 22:24pm    
i mean the value: x1=1 y1=1 x2=1 y2=4 calculate using this formula (y2 - y1) / (x2 - x1). comes 3/0 if 0 is denominator print "undefined".
danial khan 22-Oct-13 22:29pm    
another example for 1 2 3 2 calculates using formula (y2 - y1) / (x2 - x1). comes 0/2 do not print the denominator just print numerator "0".
Member 10353629 23-Oct-13 1:03am    
yes,you shuld ask your teacher!!
Ranjan.D 22-Oct-13 22:35pm    
if (x2 - x1 == 0)
cout<<"undefined" << endl;
else
cout << y2 - y1 / x2 - x1 << endl;

I advise you to learn basics and code solutions. These are really really basic things of programming.

1 solution

Strictly speaking, it should be floating-point coordinates, but integers make the problem extremely simple. Floating-point values are approximate, and, these days, unfortunately, not so many people understand how to correctly work with them. Now, remember your Algebra 1, geometry, or whatever you learned, and think: what would make the problem of of drawing a line between two points indeterminate? Right, if two points have the same location, then you can draw infinite number of straight lines, from one point. So, you got one condition. There is another case: "infinite" derivative. This is when two x-coordinates are the same. In this case, if y-coordinates are different, you can draw a single straight line through two points, but this line would be vertical.

I would not worry that this line is vertical, because, the slope can be infinite (even though this case is not really covered by school algebra or even "elementary" calculus, but such thing can be formalized; the floating-point type even have special Inf values which you could assign to the result). The problem is that, with just two points, this infinite slope can be interpreted as either +Inf or −Inf, that is, this is an infinite case again. Note that this is the same very case where the integer division in your formula would give an exception. So, you can do another check for this case, or, too look cool :-), catch this exception and output your "underfined" when it is caught.

As this looks like a home assignment, no code samples for you. If you did not find out how to analyze this simplest mathematical problem, at least write the code with your own hands. I explained you 100% of what you need. And if you don't do such things with your own hands, you don't really learn; this simple idea has been proven on many examples.

Good luck,
—SA
 
Share this answer
 
Comments
danial khan 22-Oct-13 22:54pm    
i know these all basic mathematics formulas and problem like rise over run....... rise is y and run is X because x is horizontal and y is vertical. if the graph is vertical then it is undefined. BUT THE REASON WAS THAT I just wanted to know how i can write this thing in C++ because i just started c++. if u guys help me i appreciate.
Sergey Alexandrovich Kryukov 22-Oct-13 22:59pm    
Okay, if you know all that, why did not you explain what is your problem? Anyway, now it's you turn. Write the code and ask another question if you fail to succeed. Anyway, what don't you know? How to write "if" blocks? How to output the word "undefined"? How to handle exceptions (optional in this case)? If you don't know, first read about it in C++ manual/reference and try to implement it. This is how all people learned programming, really. If someone does all this work for you, you cannot learn anything at all. So, please go ahead and do it.
—SA
danial khan 22-Oct-13 23:06pm    
i have C++ book i can read that :) ok bye.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900