# Looping through an array

• 12-12-2012, 11:59 PM
Person264
Looping through an array
Hi, new here so I apologise if I'm doing it wrong.

I have a simulation that is simulating a particle travelling through the centre of the earth. I have it working using simple harmonic motion with the gravitational force being proportional to the position. What I would also like to do is use the data from the Preliminary Earth Reference Model to set the acceleration of the particle as it passes through the earth. I have an array filled with the radius steps, going from 0 to 6371000 in steps of 200000 and also an array with the accelerations at each of those points. So for the radiusArray[i] there is a corresponding accelArray[i]. What I would like is to be able to loop through the radius, and when the particle falls through the earth, it will have an acceleration assigned a value related to the height it is from the center.

Here is my code that I know does not work
Code:

```double R = 6371000; double distFrom0 = R - Math.abs(particle.getHeight()); double accel = 0; for(int i=(PERMradii.length-1); i>=0; i--){         if(distFrom0>=PERMradii[i]){                 if(particle.getHeight()<-6371000){                         accel = PERMaccel[i];                 }else{                         accel = -PERMaccel[i];                 }         } }```
PERMradii is where the radius data is stored, and PERMaccel is the acceleration array
I want the program to check if the distance from the centre is between two of the array entries for radius, then set the acceleration to the acceleration at that position. So if distFrom0 is between 0 and 200000, the acceleration should be zero, the next step would be 0.7311ms^-2 and so on. Should I even be using a for loop?