# Calculating segment costs

• 11-16-2012, 02:01 PM
sezzy
Calculating segment costs
I have been given a task of creating a 'traveling salesman' type programme in which I need to calculate costs.

I need to create an array for six segments - A-B=2, A-C=3, A-D=1, B-C=4, B-D=2,C-D=3

Now I'm not even sure that I've coded it correctly, I'm trying to ascertain the array...any help would be appreciated. I know I've incorrectly coded my 'answer', but I'm working on that now...I know I need to get it to loop, but I'm having a bit of a struggle.

Any help is appreciated.

import java.util.*;
public class Travel2 {
public static void main(String[] args) {

int[] cost=new int[6];
int index;

cost [0] = 2;
cost [1] = 3;
cost [2] = 1;
cost [3] = 4;
cost [4] = 2;
cost [5] = 3;

/*Create a new scanner to read inputs*/
Scanner reader = new Scanner (System.in);

System.out.println("Enter ID of segment 0 of path: ");
System.out.println("Enter ID of segment 1 of path: ");
System.out.println("Enter ID of segment 2 of path: ");

System.out.println("Total cost for this path: "+ (cost[0] + cost[1] + cost[2]));
}

}

Cheers,

Sez
• 11-16-2012, 02:56 PM
JosAH
Re: Calculating segment costs
Quote:

Originally Posted by sezzy
I need to create an array for six segments - A-B=2, A-C=3, A-D=1, B-C=4, B-D=2,C-D=3

How do we have to read this? Those are not just six linear equations are they?

kind regards,

Jos
• 11-16-2012, 03:16 PM
sezzy
Re: Calculating segment costs
Sorry, I should have been clearer, I know the answer is right in front of me, I'm overthinking it.

They correlate to segment numbers. I've been given a table with segment ID, Segment name and Travel cost, so A-B=\$2million and so on.

I need to calculate costs based on user input to four destinations (three inputs).
• 11-16-2012, 03:20 PM
Tolls
Re: Calculating segment costs
I think that's "distance" between all 4 places (A, B, C, D).
• 11-16-2012, 03:32 PM
sezzy
Re: Calculating segment costs
Yes, so when all is said and done, the program needs to calculate A-D-C-B = 1+3+4=8

I think I need to sleep on it and come back fresh in the morning, hopefully I'll be thinking clearer, at the moment I'm not even sure if I need to index it to get it to calculate as it should...