Distance between two coordinates help

Oct 10, 2013 at 1:43am
Im beginning an assignment for class and am having trouble with this code. I am trying to find the square root of two points using what I know from class. What is preventing me from finding the distance between two points?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath> 
using namespace std;
double calcDistance (int x1, int y1, int x2, int y2);
void displayDistance(double distance);
int main()
{
 
  int x1, x2, y1, y2;
  double distance;
  
  cout << "Enter the first x and y coordinates: ";
  cin >> x1 >> y1;
  
  cout << "Enter the second x and y coordinates: ";
  cin >> x2 >> y2;
  
  int distancex = (x2 - x1)^2;
  int distancey = (y2 - y1)^2;

  double calcdistance = sqrt(distancex - distancey);
  return distance; 
  
  
  displayDistance(distance);
  system("pause");  
  return 0;
}
void displayDistance(double distance)
{
     cout << "The distance between the two points is: " << distance << endl;
 }
Oct 10, 2013 at 1:51am
Your problem is that the carrot caret operator ( ^ ) is the XOR bitwise operator not the power operator. What you want to do is use the pow function

so fix line 18 and 19 in this format ax = pow( a , x )

*edit fixed a type I meant caret not carrot

*edit2 You can also simply just add on line 20 this

1
2
distancex *= distrancex; //sq
distancey *= distancey; //sq 
Last edited on Oct 10, 2013 at 1:56am
Oct 10, 2013 at 2:11am
I made the necessary changes but, compiler tells me that it's wrong. Is there a more simpler way to write this?
Oct 10, 2013 at 2:17am
wait...why do you have line 4 and 22? They are doing nothing.
Oct 10, 2013 at 2:22am
I made the necessary changes but, compiler tells me that it's wrong.


The thing about compiler error messages is that they not only tell you exactly what line the error is occurring on, but they also give a very good indication of what the error is.

If you are getting error messages post them here, along with the lines of code that are causing them, and we can explain the messages to you.
Oct 10, 2013 at 2:46am
it gives me 17 E:\Dev-Cpp\CalculateArea.cpp call of overloaded `pow(int, int)' is ambiguous for some reason... I don't understand why i can't square the numbers directly
Oct 10, 2013 at 2:48am
do use pow to square 2 numbers. It's overkill.

1
2
3
pow(x,2);  // bad

(x*x);  // good 
Oct 10, 2013 at 2:55am
i dont understand why I cant simply use ^2. Isn't it the same code I had done earlier?

Just to note, I wrote most of this code using what I learned in class recently. So that would show my level of knowledge with syntax right now. Could someone explain why my code won't allow me to show the distance between two points?
Last edited on Oct 10, 2013 at 2:59am
Oct 10, 2013 at 3:20am
i dont understand why I cant simply use ^2.


^ is not the exponent operator in C++. It's the bitwise XOR operator, so... when you do x ^ 2 it does something completely different from what you think it does -- is does not square x... instead it toggles bit 1 of x.

See for yourself:

1
2
3
4
int foo = 10;
int bar = foo^2;

cout << bar; // outputs "8" ... not "100" 



Could someone explain why my code won't allow me to show the distance between two points?


1) The formula you're using in your original code is wrong. (even if ^ did what you think it does.. which again it doesn't).

2) You are mixing up two different variables. You have a variable named distance that is never initialized because you put the distance into a separate var named calcdistance.

3) You exit main (and therefore the entire program) before you print anything. The return distance; on line 22... when you return from main.. main exits. When main exits.. your program exits. So anything in main after line 22 will not be run.
Oct 10, 2013 at 3:26am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cmath> 
using namespace std;
void displayDistance(double distance);
int main()
{
 
  int x1, x2, y1, y2;
  
  cout << "Enter the first x and y coordinates: ";
  cin >> x1 >> y1;
  
  cout << "Enter the second x and y coordinates: ";
  cin >> x2 >> y2;
  
  int distancex = (x2 - x1) * (x2 - x1);
  int distancey = (y2 - y1) * (y2 - y1);

  double distance = sqrt(distancex - distancey); 
  
  displayDistance(distance);
  system("pause");  
  return 0;
}
void displayDistance(double distance)
{
     cout << "The distance between the two points is: " << distance << endl;
 }

would this be better? It still doesn't do what i want it to do though.
Oct 10, 2013 at 3:29am
Yes that is much better. Though line 19 is still wrong.

remember the formula is a2 + b2 = c2

You are doing a2 - b2 = c2
Oct 10, 2013 at 3:32am
Thank you so much! It finally works! Thanks again Disch!!!
Oct 10, 2013 at 3:34am
Glad to help.

It's always nice to see someone who's actually willing to do the work themselves instead of just looking for answer handouts. =)

Keep up the good work.
Topic archived. No new replies allowed.