0

i am trying to make a for loop that will go through 9 different sets of points and execute them in my main driver with two other classes. I'm pretty stuck at the moment and can't figure out how to get the second (pointC2) to be set before the for loop runs again.

coordinates are:

X:{{86.92, 70.93, 97.74, 30.90, 51.66, 0.83, 55.91, 32.92, 65.26, 83.90},

Y:{2.47, 27.81, 34.36, 35.14, 31.70,21.77, 66.62, 75.23, 72.53, 4.710}};  

with

(86.92, 2.47) = (x1,y1)

(70.93, 27.81) = (x2,y2)

i tried setting it up as a multidimensional loop, but i couldn't get the right counter.

the basis of the assignment is to convert the cartesian coordinates to polar and then to find the distance from (x1,y1) to (x2,y2) to (x3,y3), etc.

here is the code i need to be executed after the for loop:

      //calls point1 from Cartesian
        Cartesian pointC1 = new Cartesian(x1, y1);

      //calls point2 from Cartesian
        Cartesian pointC2 = new Cartesian(x2, y2);

        double answer1 = Cartesian.distance(pointC1, pointC2);

      //prints out point1 and point2
        System.out.println("Point 1 = " + pointC2 + " Point 2 = " + pointC1);

      //prints out sum 
        System.out.println("Distance: " + answer1);

then goes goes again but this time from (x2,y2) and (x3, y3)

Mordechai
  • 15,437
  • 2
  • 41
  • 82
Cfs0004
  • 85
  • 2
  • 14

2 Answers2

1

I can tell you a procedure rather coding, but you have to study about arrays first!

Define,

array X: {{86.92, 70.93, 97.74, 30.90, 51.66, 0.83, 55.91, 32.92, 65.26, 83.90},

array Y: {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710}};

Then,

begin for loop from i = 0 to array size -1
x1 = array X [i];
y1 = array Y [i];
// rest of your code goes here
// ...
end for loop
mazhar islam
  • 5,561
  • 3
  • 20
  • 41
  • I have studied arrays, i just can't figure out how to do this one since i need the for loop to get point (x1, x1) then set that to Cartesian pointC1 = new Cartesian(X1, X2); then then do the next part of the for loop and set that to Cartesian pointC2 = new Cartesian(Y1, Y2); the calculate the distance. Then go again for (X2, X2) (X3,X3). i just can't figure out the order. Could you explain the array size -1? I haven't see that before – Cfs0004 Jun 28 '15 at 20:59
0
double[] x = {86.92, 70.93, 97.74, 30.90, 51.66, 0.83, 55.91, 32.92, 65.26, 83.90};
double[] y = {2.47, 27.81, 34.36, 35.14, 31.70, 21.77, 66.62, 75.23, 72.53, 4.710};
Cartesian[] pointC = new Cartesian[Math.min(x.length, y.length)];
double[] distance  = new double[pointC.length - 1];
for(int i = 0; i < pointC.length; i++){
    pointC[i] = new Cartesian(x[i], y[i]);
}
for(int i = 0; i < distance.length; i++){
    distance[i] = Cartesian.distance(pointC[i], pointC[i+1]);
}
afzalex
  • 8,598
  • 2
  • 34
  • 61