Hi im trying to create a piece of code that generates an array of random length 15 and then gives each element a random number between 0-100, i want to be able to test the distance between point 1(which is set at 0) and the distances stored in the array e.g element 2 has value 60 so distance from point 1 is 60, instead of displaying the distance from point 1 i want to display which points are closest to point 1 from smallest to largest, so far i have sorted the array to be smallest numbers first however was wondering how i would go about showing which element stores what distance and not the other way around e.g the nearest point to point 1(0) is point 4(89, the 2nd nearest point to point 1 is point 3(25), here is the code i have so far:
static double distance(double x1, double x2)
{
return Math.sqrt((x2-x1)*(x2-x1) );
//Math.sqrt is the square root of the 2 co-ordinates
}
public static void main(String[] args)
{
Random randomGenerator = new Random();
double x2, x1; //The Points
x1 = 0;
double distance;//The Math to work out distance
int randomInt2 = randomGenerator.nextInt(15)+1;//For Length of array
double [] distances = new double [randomInt2];//The Array
double store;
System.out.println("Distance Generated for point 1 : 0" );
System.out.println("Amount of points created is: "+randomInt2 );
int range = 0;//Amount of Points
while (range < randomInt2) {
int randomInt3 = randomGenerator.nextInt(1000);//Distance for all points besides first
x2 = randomInt3;
distance = distance(x1,x2);
store = distance;//stores the distance to be put into array
distances[range] = store;
range++;//increments amount of points each time
}
for (double val : distances) {
System.out.print("["+val+"],");
}
Arrays.sort(distances);// sorts array from highest to lowest
System.out.println("\nThe Nearest to point 1 is: " + distances[0]);
Any help would be greatly appreciated