### Simple Coordinate Plane

Hello, I'm creating a quick program to calculate midpoint and distance between two points. I'm having a little trouble and am wondering if anyone more experienced could assist me.
This is the program:

//This program is a start to solve basic coordinatre plane distances and midpoints
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
cout << "This is a calculator to find distance and the midpoint between two points.\n" ;
int x1 ;
int y1 ;
int x2 ;
int y2 ;
cout << "(\n" ;
cin >> x1 ;
cout << ",\n" ;
cin >> y1 ;
cout << ")\n\n" ;
cout << "(\n" ;
cin >> x2 ;
cout << ",\n" ;
cin >> y2 ;
cout << ")\n\n" ;
double midpointx ;
double distancey ;
double distancex ;
double distance ;
double distanceb4sqrt ;
double midpointy ;

midpointy = (y2 + y1) / 2 ;
midpointx = (x1 + x2) / 2 ;
distancex = (x2 - x1) * (x2 - x1) ;
distancey = (y2 - y1) * (y2 - y1) ;
distanceb4sqrt = distancex + distancey ;
distance = sqrt (distanceb4sqrt) ;
cout << " Midpoint: (" << midpointx << "," << midpointy << ")\n" << endl ;
cout << " Distance: SQRT " << distanceb4sqrt << endl ;
system ("PAUSE") ;

}

the problem is that the output for both equations is wrong, it doesn't do the math properly and I assume it has to do with the variable types I'm using but am not sure. Any help would be greatly appreciated. P.S. I am using the latest version of DEV-C++
I've been staring at this for 10 minutes and I can't really see any math mistakes either. Maybe I'm not good enough, lol.

One little thing I would suggest is to use ` double ` for all the variables.
Example:( 2 + 5 ) / 2 should be 3.5, but if you put it in an `int `, only 3 is stored.

And you should cout `distance`, not `distanceb4sqrt`, because your final answer is stored in `distance`.
To find the distance between x1 and x2, use absolute value with that.

http://www.cplusplus.com/reference/cmath/abs/

Remember, all distance formula is in reality is Pythagorean theorem on a grid.

After getting the difference between both points in distance you can square them.
There is no need for abs here.
 There is no need for abs here.

Can you elaborate on that please? In distance formula, when you're doing (x2 - x1) all you are doing really is meaning to find the distance between the two points. So absolute value is perfectly valid for this situation in my opinion.
(x1-x1) is squared - giving the same result for x1-x2 < 0 and x1-x2 > 0
i have fixed the code slightly and made all the variable types double, however, for some reason, it still doesn't get the right answer. The distance between say (1,2) and (3,4) is sqrt of 8 however the program displays sqrt of 0. ( i got rid of the final distance and distanceb4sqrt because i dont care the exact number, only the sqrt) The midpoint calculator also only displays in integers, not in decimals. I'm very confused and am going to try the asolute value way of solving. Slightly edited code:

//This program is a start to solve basic coordinatre plane distances and midpoints
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
cout << "This is a calculator to find distance and the midpoint between two points.\n" ;
double x1 ;
double y1 ;
double x2 ;
double y2 ;
cout << "(\n" ;
cin >> x1 ;
cout << ",\n" ;
cin >> y1 ;
cout << ")\n\n" ;
cout << "(\n" ;
cin >> x2 ;
cout << ",\n" ;
cin >> y2 ;
cout << ")\n\n" ;
double midpointx ;
double distancey ;
double distancex ;
double distance ;
double midpointy ;

midpointy = (y2 + y1) / 2 ;
midpointx = (x1 + x2) / 2 ;
distancex = (x2 - x1) * (x2 - x1) ;
distancey = (y2 - y1) * (y2 - y1) ;
distance = distancex + distancey ;

cout << " Midpoint: (" << midpointx << "," << midpointy << ")\n" << endl ;
cout << " Distance: SQRT " << distance << endl ;
system ("PAUSE") ;

}
My conclusion if the absolute value code doesn't work is that i'll just make separate functions for midpoint and distance or a menu to select which to do before inputting the four variables. Thanks for all the feedback!
First, please always use code tags, select your code, press the <> button on the right.

With your code:

But you didn't take the square root, all you have done is add the squares. There is a `sqrt` function in `<cmath>` which you will need to` #include `

You had all that in your first code.

You can combine the declaration of the variables with assignment:

 ``12345`` ``````double midpointx = (x1 + x2) / 2.0 ; // I like to fully specify a FP value double midpointy = (y2 + y1) / 2.0 ; double distancex = (x2 - x1) * (x2 - x1) ; double distancey = (y2 - y1) * (y2 - y1) ; double distance = distancex + distancey ; //calc square root here ``````

Good Luck !!
Last edited on
Well, I redid the whole program and yet still, the math is done wrong. I'm goin to just abandon the project until i learn what could possibly be wrong with the mathematics. Here's the revised code:

//This program is a start to solve basic coordinatre plane distances and midpoints
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;

double x1 ;
double y1 ;
double x2 ;
double y2 ;
double midpointy ;
double midpointx ;

double distancefunction(double distance)
{
double distancex ;
double distancey ;
distancex = (x2 - x1) * (x2 - x1) ;
distancey = (y2 - y1) * (y2 - y1) ;
distance = distancex + distancey ;

return distance ;
}

int main(int nNumberofArgs, char* pzArgs[])
{
cout << "This is a calculator to find distance and the midpoint between two points.\n" ;
cout << "(\n" ;
cin >> x1 ;
cout << ",\n" ;
cin >> y1 ;
cout << ")\n\n" ;
cout << "(\n" ;
cin >> x2 ;
cout << ",\n" ;
cin >> y2 ;
cout << ")\n\n" ;

midpointx = (x1 + x2) / 2 ;
midpointy = (y1 + y2) / 2 ;

cout << " Midpoint: " << midpointx << "," << midpointy << endl ;
cout<< " Distance: SQRT: " << distancefunction << endl ;
system ("PAUSE") ;
}
You are still not taking the square root of the distance variable. Again you had that in your first program.

PLEASE ALWAYS USE CODE TAGS
Last edited on
You should stick with trying to get this tiny program working. That's what programming is - getting stuff working, not just typing stuff in.

I can't see anything wrong with your code - but you have the tools to find out what's wrong.

Minutely examine what's going on at every step - even after just cin, until you have found out what is going wrong.
.
The formula is incorrect for finding the distance. It should be:

sqrt (x2 - x1) ^ 2 + (y2 - y1) ^ 2)

Although, to write powers in C++, you need to do pow(a,b) where a is the number and b is the power it's being raised to. sqrt(a) find the root of a. To use pow() and sqrt(), you need to #include <cmath>.
The program does work.
Topic archived. No new replies allowed.