I’m revising for a introduction to programming exam and I have a question which from a previous exam paper I am slightly stuck on.
The question:
Write a method that takes a double array as an argument with values representing the positions of train stations along a track. The method should return a two-dimensional array with the distances between each pair of stations in the argument. The array of distances should have only one entry for each pair of stations (i.e. do not use a rectangular array).
I have a solution to the question but I just can’t get the last bit where there should only be one entry for each pair. I have thought about creating a look up table will all the entries to see if the distance for the two stations but then the array would have lots of empty cells for the later stations because the distance would have already been calculated.
Here is my current solution
//Set of locations on the train line
private static double[] stations = {0.0, 2.0, 3.0, 5.0};
//Method to take the array of doubles and create distance map
public static double[][] getDistances(double[] st){
double[][] distanceMap = new double[st.length][st.length-1];
int x;
for(int i=0; i<st.length; i++){
x=0;
for(int j=0; j<st.length; j++){
if(j != i){
distanceMap[i][x] = Math.abs(st[i]-st[j]);
x++;
}
}
}
return distanceMap;
}
//Main method to get the distance map then loop over results
public static void main(String[] args){
double[][] arrayMatrix = getDistances(stations);
for(int i=0; i<arrayMatrix.length; i++){
for(int j=0; j<arrayMatrix[0].length; j++){
System.out.print(arrayMatrix[i][j]+" ");
}
System.out.println("");
}
}
If anyone could point me in the right direction it would be really appreciated.
Thanks in advance.
//EDIT
After some great advice from @izomorphius I have managed to solve the question. Thanks.
Here is the complete solution
//Set of locations on the train line
private static double[] stations = {0.0, 2.0, 3.0, 5.0};
//Method to take the array of doubles and create distance map
public static double[][] getDistances(double[] st){
double[][] distanceMap = new double[st.length-1][];
int size = st.length-1;
for(int i=0; i<distanceMap.length; i++){
distanceMap[i] = new double[size];
size--;
}
ArrayList<String> lut = new ArrayList<String>();
int x;
for(int i=0; i<distanceMap.length; i++){
x=0;
for(int j=0; j<st.length; j++){
if(j != i && !lut.contains(i+"/"+j)){
distanceMap[i][x] = Math.abs(st[i]-st[j]);
lut.add(i+"/"+j);
lut.add(j+"/"+i);
x++;
}
}
}
return distanceMap;
}
//Main method to get the distance map then loop over results
public static void main(String[] args){
double[][] arrayMatrix = getDistances(stations);
for(int i=0; i<arrayMatrix.length; i++){
for(int j=0; j<arrayMatrix[i].length; j++){
System.out.print(arrayMatrix[i][j]+" ");
}
System.out.println("");
}
}
What the statement says is “i.e. don’t use rectangular array”. The idea is to store only one value for each pair. For instance if you have the pair (a,b) and a < b store the distance between a and b in the array of a but not in the one of b. Thus the array for the first station will be of size n – 1(distances to all the other stations), for the second station it will be of size n – 2(all other stations except the first one) and so on. Therefor your array will be triangular not rectangular. I hope this tip is enough as after all the idea is not to have me solve your problem.