6
I want to calculate the Euclidean distance by the following formula:

So I tried, making this code:
#define SLEEP_1 1000
void HeaderClass::DistanciaEuclidianaEntrePontos() {
int x1, x2, y1, y2, distancia;
std::cout << "Coordenadas ponto 1 (x): ";
std::cin >> x1;
std::cout << "Coordenadas ponto 1 (y): ";
std::cin >> y1;
Sleep(SLEEP_1);
std::cout << "Coordenadas ponto 2 (x): ";
std::cin >> x2;
std::cout << "Coordenadas ponto 2 (y): ";
std::cin >> y2;
Sleep(SLEEP_1);
distancia = sqrt(((x2 - x1) ^ 2) + ((y2 - y1) ^ 2));
std::cout << "Distancia Euclidiana: " << distancia << std::endl;
}
but still I can’t get what I want...
for example:
(4-7) ² + (2-5) ² = 12
but the program says :
(4-7) ² + (2-5) ² = -214(...)
There will be some more way to do it?
What you’re getting this way, and what you hoped to get?
– Bacco
I don’t know, give me a huge number, for example : (4-7) ² + (2-5) ² = 12 but in the program it says : - 214...
– André
I gave an answer that should solve your problem, but I would like to ask: is
inteven if you want it, it wouldn’t bedouble? Thus the distance can be truncated, if it does not give an integer value.– mgibsonbr
Not because the double would conflict with the " 2 " (²)
– André
@André I don’t understand... You can calculate the square of a
doubleyeah, no problem...– mgibsonbr
In C/C++ the operator does not mean potentiation but OR Exclusive (Bitwise exclusive OR). Use the Pow function.
– anonimo