Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7915699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:32:42+00:00 2026-06-03T14:32:42+00:00

I’m revising for a introduction to programming exam and I have a question which

  • 0

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("");
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T14:32:44+00:00Added an answer on June 3, 2026 at 2:32 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.