public class Diabetes {
public static void main(String[] args) {
double T = Double.parseDouble(args[0]);
double deltat = Double.parseDouble(args[1]);
int n = StdIn.readInt();
int i = 1;
double r = StdIn.readDouble();
double[] positionX = new double[n];
double[] positionY = new double[n];
double[] velocityX = new double[n];
double[] velocityY = new double[n];
double[] mass = new double[n];
String[] image = new String[n];
double[] deltaX = new double[n * n];
double[] deltaY = new double[n * n];
double[] radius = new double[n * n];
double[] force = new double[n * n * n];
double g = 6.67E-11;
double[] forceX = new double[n * n];
double[] forceY = new double[n * n];
double[] cosine = new double[n * n];
double[] sine = new double[n * n];
double[] accelerationX = new double[n * n];
double[] accelerationY = new double[n * n];
double[] newvelocityX = new double[n * n];
double[] newvelocityY = new double[n * n];
double[] newpositionX = new double[n * n];
double[] newpositionY = new double[n * n];
int positionX_counter = 0;
int positionY_counter = 0;
int velocityX_counter = 0;
int velocityY_counter = 0;
int mass_counter = 0;
int image_counter=0;
//System.out.println("n = " + n + "r = " + r);
for (int j = 0; j < n; j++) {
for(int k = 1; k < 7; k++)
if (k == 6) {
image[image_counter] = StdIn.readString();
System.out.println(image[image_counter]);
image_counter++;
//System.out.println("image counter = " + image_counter);
}
else if (k == 5) {
mass[mass_counter] = Double.parseDouble(StdIn.readString());
System.out.println(mass[mass_counter]);
mass_counter++;
//System.out.println("masscounter = " + mass_counter);
}
else if (k == 4) {
velocityY[velocityY_counter] = Double.parseDouble(StdIn.readString());
System.out.println(velocityY[velocityY_counter]);
velocityY_counter++;
//System.out.println("velcounter = " + velocity_counter);
}
else if (k == 3) {
velocityX[velocityX_counter] = Double.parseDouble(StdIn.readString());
System.out.println(velocityX[velocityX_counter]);
velocityX_counter++;
//System.out.println("velcounter = " + velocity_counter);
}
else if (k == 2) {
positionY[positionY_counter] = Double.parseDouble(StdIn.readString());
System.out.println(positionY[positionY_counter]);
positionY_counter++;
}
else if (k == 1) {
positionX[positionX_counter] = Double.parseDouble(StdIn.readString());
System.out.println(positionX[positionX_counter]);
positionX_counter++;
}
}
for (int m = 0; m <= 10; m++) {
if ( i == 5 ) {
i = 1;
}
else {
deltaX[m] = positionX[i] - positionX[m];
deltaY[m] = positionY[i] - positionY[m];
radius[m] = Math.sqrt((deltaX[i] * deltaX[i]) + (deltaY[i] * deltaY[i]));
force[m] = (g * mass[m] * mass[i]) / (radius[m] * radius[m]);
cosine[m] = (deltaX[m] / radius[m]);
sine[m] = (deltaY[m] / radius[m]);
forceX[m] = force[m] * cosine[m];
forceY[m] = force[m] * sine[m];
accelerationX[m] = forceX[m] / mass[i];
accelerationY[m] = forceY[m] / mass[i];
newvelocityX[m] = velocityX[i] + (deltat*accelerationX[m]);
newvelocityY[m] = velocityY[i] + (deltat*accelerationY[m]);
newpositionX[m] = positionX[i] + (deltat*velocityX[i]);
//System.out.println(newpositionX[m]);
newpositionY[m] = positionY[i] + (deltat*velocityY[i]);
i++;
System.out.println(i);
}
}
}
}
Hey Guys, really confused by this code I've written and why it's throwing an error. Essentially I have some arrays of length (5) and I'm trying to do some calculations with them and store those in an array of length 10. So the code is designed to do ten calculations with five number arrays, so naturally I designed it to have two different counters within the for loop. The first one should reset when it reaches 5, and the other one kills the loop when it hits 10. What doesn't make any sense at all is that the program is spitting out the output that i=5, but it's not going into the if statement and reseting itself so I get thrown an error about my array being out of bounds.
Thanks for any help