Ayuda para pasar ESTE codigo hecho en JAVA A C++
Publicado por Gustavo (1 intervención) el 28/02/2019 23:27:38
Buenas tardes, quiero pasar el siguiente código hecho en JAVA a C++, pero cuando lo intento, el resultado en C++ difiere del de JAVA, me gustaría que me indicaran cuál es el error que he cometido .
El código escrito anteriormente, cumple con lo requerido. Caso contrario con el que voy a mostrar a continuación
Este es el "intento" que realice para pasar el código mencionado anteriormente a C++
Si pudieran ayudarme, se los agradecería un monton !
Desde ya, muchísimas gracias!
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class Ejerciciooo
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter the number of points: ");
final int numOfPoints = scan.nextInt();
double[][] points = new double[numOfPoints][2];
double shortestDistance=0;
double distance=0;
String closestPoint1="";
String closestPoint2="";
//enter x,y coords into the ix2 table points[][]
for (int i=0; i<numOfPoints; i++)
{
System.out.print("Enter point x" + i + ": ");
points[i][0] = scan.nextDouble();
System.out.print("Enter point y" + i + ": ");
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];
distance = Math.sqrt(dx*dx + dy*dy);
//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] + ")";
}
}
}
System.out.println("The shortest distance is: " + shortestDistance);
System.out.println("The closest points are " + closestPoint1 + " and " + closestPoint2);
}
}
El código escrito anteriormente, cumple con lo requerido. Caso contrario con el que voy a mostrar a continuación
Este es el "intento" que realice para pasar el código mencionado anteriormente a C++
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Enunciado:
**/
#include <iostream>
#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;
}
Si pudieran ayudarme, se los agradecería un monton !
Desde ya, muchísimas gracias!
Valora esta pregunta


0