Declaring Multi-Dimensional Arrays

Discussion in 'C++' started by joe, Dec 13, 2006.

  1. joe

    joe Guest

    hi all,

    I have perused the internet a bit and don't see a simple beginner's answer.
    All I need right now is how to declare a multi-dimensional array in C++.

    // one dimensional array - simple enough
    int array[4] = {0,0,0,0};

    // two dimensional array - ?
    int array[4][5] = {0,0,0,0 ... ?

    thx!
     
    joe, Dec 13, 2006
    #1
    1. Advertising

  2. joe

    Chris Guest

    joe wrote:
    > hi all,
    >
    > I have perused the internet a bit and don't see a simple beginner's answer.
    > All I need right now is how to declare a multi-dimensional array in C++.
    >
    > // one dimensional array - simple enough
    > int array[4] = {0,0,0,0};
    >
    > // two dimensional array - ?
    > int array[4][5] = {0,0,0,0 ... ?
    >
    > thx!


    try:

    int array[2][2] { { 1, 2 },
    { 3, 4} };

    that should give you the idea..
     
    Chris, Dec 13, 2006
    #2
    1. Advertising

  3. joe

    joe Guest

    "Chris" <> wrote in message
    news:...
    >
    > joe wrote:
    >> hi all,
    >>
    >> I have perused the internet a bit and don't see a simple beginner's
    >> answer.
    >> All I need right now is how to declare a multi-dimensional array in C++.
    >>
    >> // one dimensional array - simple enough
    >> int array[4] = {0,0,0,0};
    >>
    >> // two dimensional array - ?
    >> int array[4][5] = {0,0,0,0 ... ?
    >>
    >> thx!

    >
    > try:
    >
    > int array[2][2] { {1, 2},
    > {3, 4} };
    >
    > that should give you the idea..
    >


    I see it, thx!
     
    joe, Dec 13, 2006
    #3
  4. joe

    joe Guest

    "joe" <> wrote in message
    news:HqLfh.49301$-nyc.rr.com...
    >
    > "Chris" <> wrote in message
    > news:...
    >>
    >> joe wrote:
    >>> hi all,
    >>>
    >>> I have perused the internet a bit and don't see a simple beginner's
    >>> answer.
    >>> All I need right now is how to declare a multi-dimensional array in C++.
    >>>
    >>> // one dimensional array - simple enough
    >>> int array[4] = {0,0,0,0};
    >>>
    >>> // two dimensional array - ?
    >>> int array[4][5] = {0,0,0,0 ... ?
    >>>
    >>> thx!

    >>
    >> try:
    >>
    >> int array[2][2] { {1, 2},
    >> {3, 4} };
    >>
    >> that should give you the idea..
    >>

    >
    > I see it, thx!
    >
    >


    // arr4: passes a multidimensional array as a paramter
    //
    // 12/12/06
    // 9:11 PM

    // This worked just fine. Thanks again! :)

    #include <iostream>
    using namespace std;

    // DECLARATIONS
    void printArray (int arg[][3][3], int i, int j, int k)
    {
    cout << "\n";
    for (int n = 0 ; n < i ; n++)
    {
    for (int m = 0 ; m < i ; m++)
    {
    for (int l = 0 ; l < i ; l++)
    {
    cout << " " << arg[n][m][l] << " ";
    }
    cout << "\n";
    }
    cout << "\n";
    }
    cout << "\n";
    }

    int main ()
    {
    // INPUT
    int array1[3][3][3] = { { { 1, 2, 3}, { 4, 5, 6}, { 7, 8, 9} },
    { {10, 11, 12}, {13, 14, 15}, {16, 17, 18} },
    { {19, 20, 21}, {22, 23, 24}, {25, 26, 27} }
    };

    // CALCULATIONS


    // OUTPUT
    printArray(array1, 3, 3, 3);


    // EXIT
    cout << "\n done.\n";
    return 0;
    }
     
    joe, Dec 13, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    8,721
    Jim Lewis
    Mar 21, 2006
  2. Alf P. Steinbach
    Replies:
    0
    Views:
    441
    Alf P. Steinbach
    Aug 18, 2003
  3. John Harrison
    Replies:
    4
    Views:
    6,935
    Default User
    Aug 19, 2003
  4. Icosahedron
    Replies:
    8
    Views:
    666
    Vivek
    Aug 21, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    210
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page