2D array of doubles

This is a discussion on 2D array of doubles within the C++ Programming forums, part of the General Programming Boards category; I want to be able to store distances between cities using C++, and store these distances using an array of ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    23

    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. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    1
    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. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,509
    Quote Originally Posted by disk_empty View Post
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,899
    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. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    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
    Last edited by hencherz; 02-25-2012 at 05:15 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Why don't you store fixed map coordinates instead? Then you can calculate the distance between two of any cities.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by Elysia View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Quote Originally Posted by iMalc View Post
    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.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    23
    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..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() and doubles?
    By Frankie15 in forum C Programming
    Replies: 4
    Last Post: 09-27-2011, 06:53 PM
  2. Doubles Problem
    By Jony in forum C Programming
    Replies: 1
    Last Post: 08-22-2011, 12:48 AM
  3. problem with displaying doubles in array
    By Seiro in forum C Programming
    Replies: 5
    Last Post: 04-21-2006, 05:06 PM
  4. Passing array of doubles to ATL COM dll
    By wakeup in forum C++ Programming
    Replies: 3
    Last Post: 04-11-2006, 03:53 AM
  5. Using Doubles
    By jay kay in forum Windows Programming
    Replies: 4
    Last Post: 03-22-2005, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21