# Thread: 2D array of doubles

1. ## 2D array of doubles

I want to be able to store distances between cities using C++, and store these distances using an array of 2D double (data type). I then want to ask the user to enter two of these cities and output the distances between the two cities.

This is what I've come up with so far:

The data itself in terms of correct distances isn't accurate as you can see, but my main focus is to try and implement the code so made up some data...

NewYork to Westbury: 220.8 miles
NewJersey to England: 99.43 miles
Miami to Los Angeles: 384.39 miles
Florida to Spain: 394.20 miles
California to Paris: 39.40 miles

Code:
```    double array[2][2] = {[0][4320.55], [0][32.77], [0][561.52], [0][243.57], [0][5479.16]};

//    string cities[5] = {"Newyork", "Westbury",```

Am I storing the distances in the array correctly, I don't seem to know how to go about doing this, I was going to store all cities in an array of the string data type. I also cannot use more than five cities which confused me even more as you can see from my code I had to stop...

2. hi hencherz,since the number of cities is definite,you could use an enum instead of string array

now it becomes simple...just declare a two dimensional array of double as you did and store the miles using the two city name as index

3. Originally Posted by disk_empty
hi hencherz,since the number of cities is definite,you could use an enum instead of string array

now it becomes simple...just declare a two dimensional array of double as you did and store the miles using the two city name as index
..This would work okay.. but converting enums implicitly to integers and relying on their internal number isn't a good idea.
The same could be dome with storing the city names in an array, the index would then represent the number.
And to find the distance between two cities, one would have to look up the position of the names in the array and use the two positions to get the distance in the double array.

4. Am I storing the distances in the array correctly,
No, you defined this array as double array[2][2] which means that you can have only 2 arrays of 2 doubles. Also your compiler should be complaining about the syntax. You may want to study this link for C++ Arrays.
also cannot use more than five cities which confused me even more as you can see from my code I had to stop...
You defined your string array with a size of 5, therefore you can only have 5 cities.

I would recommend that you abandon the use of arrays and think about using std::vector instead, this container will grow to hold the required number of elements and will keep track of it's size. You should also be considering using a class or structure to hold this information.

Also I would suggest that you look into using one of the graphing algorithms.

Jim

5. Thank you all for your suggestions.

Code:
```double array[5][2] = {[0][4320.55], [0][32.77], [0][561.52], [0][243.57], [0][5479.16]};
string cities[5] = {"Newyork", "Westbury", "Newjersey", "England", "Miami"};```
I've changed the array from [2][2] to [5][2], am I correct in thinking that I have declared 5 arrays of 2 doubles with [5][2]? (Or have I got it the wrong way around)

I'm sure my cities array is correct, however I'm a little confused about the double array, what two figures go in the double arrays..

Since I can only use 5 cities (that's the requirement) I don't know what figures to put in the double array anymore..

i.e. Newyork = 4320.55 miles, but from 4320.55 what?

The only way I see this making sense to me is if I have one fixed place like PlaceA and then have 5 cities stored and give the user the distance from PlaceA to any of the 5 cities stored in the array

6. Why don't you store fixed map coordinates instead? Then you can calculate the distance between two of any cities.

7. Originally Posted by Elysia
Why don't you store fixed map coordinates instead? Then you can calculate the distance between two of any cities.
Probably because the earth isn't flat so that isn't as easy to calculate reliably as one might first think.

hencherz: What do you think all those square brackets are doing for you around those values? It won't compile with them in there. You need commas and curly braces.

8. Originally Posted by iMalc
Probably because the earth isn't flat so that isn't as easy to calculate reliably as one might first think.
Obviously it is an approximation, but any approximation is better than none. I have not seen any evidence that suggests a better approximation is needed. If it was, a more complex solution would probably be needed.

9. Code:
```double array[5][2] = {{0, 4320.55}, {0, 32.77}, {0, 561.52}, {0,243.57}, {0,5479.16}};

string cities[5] = {"NewYork", "Westbury", "NewJersey", "England", "Miami"}```
After reading the above posts I have it in a state in which it compiles.

Now I want to see if I got the logic right.

1) make two string variables that each store the 2 cities that the user wants to know the distances from.

2) Then check the array for the distance between both cities.

I'm fairly new to working with an array of doubles, I have worked with single arrays quite a few times though

What kind of calculation am I looking to do to find the distance between them?

i.e. array[0] - array [2] = distance between them?

Dont quite know how I'm going to implement this..