A mi entender el de "JAVA" funciona bien, es decir, cumple con los requisitos. En cambio el de C++ no lo hace de forma completa.
Sin más preámbulos adjuntaré ambos códigos, para que si pueden, ayudarme a entender cuáles son los errores que he cometido en el "intento" de trasladar las líneas de código a C++.
VERSIÓN JAVA
Código
public class Ejerciciooo { { final int numOfPoints = scan.nextInt(); double[][] points = new double[numOfPoints][2]; double shortestDistance=0; double distance=0; //enter x,y coords into the ix2 table points[][] for (int i=0; i<numOfPoints; i++) { points[i][0] = scan.nextDouble(); points[i][1] = scan.nextDouble(); } //get the distance between the point in the ith row and the (m+1)th row //and check if it's shorter than the distance between 0th and 1st for (int i=0; i<numOfPoints; i++) { //use m=i rather than 0 to avoid duplicate computations for (int m=i; m<numOfPoints-1;m++) { double dx = points[i][0] - points[m+1][0]; double dy = points[i][1] - points[m+1][1]; //set shortestDistance and closestPoints to the first iteration if (m == 0 && i == 0) { shortestDistance = distance; closestPoint1 = "(" + points[0][0] + "," + points[0][1] + ")"; closestPoint2 = "(" + points[1][0] + "," + points[1][1] + ")"; } //then check if any further iterations have shorter distances else if (distance < shortestDistance) { shortestDistance = distance; closestPoint1 = "(" + points[i][0] + "," + points[i][1] + ")"; closestPoint2 = "(" + points[m+1][0] + "," + points[m+1][1] + ")"; } } } } }
VERSIÓN C++
Código
#include <conio.h> #include <math.h> #include <sstream> #include <string> using namespace std; int main () { int cantidad; cout<<"Digite la cantidad de puntos que desea: "; cin>>cantidad; double puntos [cantidad][2]; double dX = 0, dY = 0 , distancia = 0, distMasCorta = 0; ostringstream osA,osB; for (int i=0; i<cantidad; i++) { cout<<"Digite el valor correspondiente a X '"<<i+1<<"': "; cin>>puntos[i][0]; cout<<"Digite el valor correspondiente a Y '"<<i+1<<"': "; cin>>puntos[i][1]; cout<<endl; } for (int i=0; i<cantidad; i++) { for (int m=i; m<cantidad-1; m++) { dX = puntos[i][0] - puntos[m+1][0]; dY = puntos[i][1] - puntos[m+1][1]; distancia = sqrt (dX * dX + dY * dY); if (distancia < distMasCorta) { distMasCorta = distancia; osA << "(" << puntos[i][0] << "," << puntos[i][1] << ")"; osB << "(" << puntos[m+1][0] << "," << puntos[m+1][1] << ")"; } } } cout<<"La distancia mas corta es de '"<<distMasCorta<<"'"<<endl; cout<<"Los puntos más cercanos son: "<<osA.str()<<" y "<<osB.str()<<endl; getch(); return 0; }
Desde ya muchísimas gracias !