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 ;
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.
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.
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;
}
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!
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;
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>.