1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

initializing a multi-dimensional array

Discussion in 'C++' started by imported_John, Sep 30, 2006.

  1. imported_John

    imported_John New Member

    Joined:
    Sep 30, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I can do this

    double T[2][3] = { {1,3,4},{7,8,9}};

    but not this

    double T[2][3];

    T = { { a,b,c} , {d,e,f} }

    Why is that? Will I have to assign T[?][?] 6 times to do this? any better ways to do this assignment?

    Thanks,
    --j
     
  2. hl2mods.info

    hl2mods.info New Member

    Joined:
    Mar 29, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Initializing Multidimensional Arrays
    You can also initialize multidimensional arrays. You assign the list of values to array elements
    in order, with the last array subscript changing while each of the former hold steady.
    Therefore, if you have the following array, the first three elements go into theArray[0]; the
    next three into theArray[1]; and so on:
    int theArray[5][3];
    You initialize this array by writing
    int theArray[5][3] = { 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 };
    11
    For the sake of clarity, you could group the initializations with braces, as in the following
    example:
    int theArray[5][3] = { {1,2,3},
    {4,5,6},
    {7,8,9},
    {10,11,12},
    {13,14,15} };
    The compiler ignores the inner braces, which make it easier to understand how the numbers
    are distributed.
    Each value must be separated by a comma, without regard to the braces. The entire
    initialization set must be within braces, and it must end with a semicolon.
    Listing 11.5 creates a two-dimensional array. The first dimension is the set of numbers from
    0 to 5. The second dimension consists of the double of each value in the first dimension.

    or VECTORS
     
  3. tehtehghost

    tehtehghost New Member

    Joined:
    Apr 18, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I never reply while browsing forums but I'm already pissed off. C'MON people... start THINKINGe! 99% of your ANSWERS is just a junk, nothing more!!!
    hl2mods.info - go to hell, your post is shit and spam. Read the first post once more. He asked how to assign multiple values at once to multi-dimensional array AFTER creating it.

    learn to use your brain, all of you.
    peace

    Ahh and I dont care about post's date. If even 1 person starts using his brain after reading this, it's a success.
     
  4. johny_1015_1981

    johny_1015_1981 New Member

    Joined:
    Oct 5, 2008
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    Dear Sir,
    There some people who at least try to help. But you are not helping. More over, you are inspiring him not to help again.

    If you thing hl2mods.info is wrong answer the right one or just go to hell and dont bother those people.

    This site needs people who want to answer. This site definitly do not need those people who think they know but wont answer. so please just go to hell live with satan and never come back
     
  5. LuciferiA

    LuciferiA New Member

    Joined:
    Sep 8, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    I second the mOtion.. :D
     
  6. Latduppy73

    Latduppy73 New Member

    Joined:
    Oct 3, 2009
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    United States
    Location:
    United States
    Home Page:
    initializing a multi dimensional array

    coercing maybe

    wow seems a very interesting conundrum ... and i thought i had it bad with multi-dimensional hashes ... just curious but why do you want to do this for anyway?
     
  7. JonRambo

    JonRambo New Member

    Joined:
    Apr 9, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    if a b c d are variables then that assignment should work else if you were trying to assign characters then you need '' for single characters and "" for string however for strings you would need a different data type and a different means of initilising

    so 'a' will initialise the array with the numerical value associated with a, there are no letters in computing only numbers 0 and 1

    ps the cake is a lie
     
  8. mujahid ahmed

    mujahid ahmed New Member

    Joined:
    Sep 14, 2011
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    post the multi dimention arry program guys :)
     
  9. ophelia.2010

    ophelia.2010 New Member

    Joined:
    Oct 18, 2011
    Messages:
    14
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    london
    Everyone please stop fighting !!! we are all here to help!!!

    The reason why you are not getting is that double is meant only for long integers and not characters!!! if you want to initialize characters then do the following:

    char T[2][3]={{a,b,c},{d,e,f}};

    Now it will work perfectly!!!! Hope this helped you!!!
     

Share This Page