calculate travel distance between all the cities in particular country

by kalai 2009-10-05 19:44:16

We can calculate traveling between each cities and store the distance value in array using
separators and display the value. This is not an optimal solution
There is an optimal solution to store the distance ,
*consider a string containing alphabets and number

var al=" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

*consider an array containing city name

var a = new Array();
a[0]="Albany, New York* tdjadd";
a[1]="Albuquerque, New Mexico* kere";
a[2]="Atlanta, Georgia* ia";
a[3]="Augusta, Maine* ";


Here Albany, Albuquerque, Atlanta and Augusta is city name
In each array element along with the city name a string like 'tdjadd' is present after the sperator '*'
This string is nothing but the distance which is converted into string of length 2 for each distance value.
This string formation is based on below method
if the distance is <100
For example
98 , round the number divisible by 10 ie, 90 divide by 10 and convert 9 to equivalent character from 'al' string ie 'i'.
if the distance is >100 and <1000
For example 985, round the number divisible by 10, ie 980 divide by 10, u will get 98, now there are two numbers 9 and 8, convert each digit to equivalent character
9 to 'i'
8 to 'h'
if the distance is >1000
For example 2766, round the number divisible by 10, ie 2760 divide by 10, u will get 276, now there are three numbers 2,7 and 6, Here there are three
numbers but we have to convert into 2 characters so join 2 and 7 as one number '27' and convert to character 'z'
and last number 6 to f
Now equivalent character for 276 is zf

<u>Explanation of string formulation to store distance in the above array</u>
In this array, last element ie., a[3] Augusta has no string character after the separator '*', which represent that distance between Augusta and Augusta is '0',
so no string is formulated.
In array element a[2] string 'ia' is present after '*' seperator, This is formed by below method
ForExample if distance between Augusta and Atlanta is 913, round the last number to 0, this can be done by
x=ROUND(number/10,0);
Now X=91;
Here we have to split 9 and 1 in order to convert to equivalent character
so
firstnumber=FLOOR(x/10,1);
secondnumber=x-(y*10);

Now,
firstnumber=9
secondnumber=1
Now convert 9 to character 'i' and 1 to 'a'
Now the string 'ia' is equivalent to the distance 913

In the array element a[1] distance string 'kere' contains 4 character, first two character ke represent the distance between
Albuquerque and Atlanta, the second pair 're' represent distance between Albuquerque and Augusta

In the array element a[0] distance string 'tdjadd' contains 6 character, first two character td represent the distance between
Albany and Albuquerque,
the second pair 'ja' represents the distance between Albany and Atlanta, the third pair 'dd' represent distance between Albany and Augusta


<u>How the distance is calculated from the string</u>

ForExample Albuquerque and Atlanta is selected, the distance string is 'ke' convert k to its
equivalent number form 'al' string 11 and e to 5
Now concatenate the two numbers 11 and 5 to 115 and multiply by 10, Now you will get the distance
Distance between Albuquerque and Atlanta is 1150

Tagged in:

1604
like
0
dislike
0
mail
flag

You must LOGIN to add comments