I created a quick method in my program to compute the distance between two points using the distance formula, here's the code:
#include <iostream>
#include <cmath>
using namespace std;
int distanceFormula(int x1, int y1, int x2, int y2) {
double d = sqrt((x1-x2)^2(y1-y2)^2);
return d;
}
it gives me a compiler error on the line where I declare the d variable saying that
error: expression cannot be used as a function.
What does this mean? And what am I doing wrong?
Be careful, (x1-x2)^2 will not do an exponent of 2 here.
See http://www.cplusplus.com/reference/cmath/pow/.
Second, you probably forgot a + in your expression:
int distanceFormula(int x1, int y1, int x2, int y2) {
double d = sqrt(pow(x1-x2, 2) + pow(y1-y2, 2));
return d;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With