Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. 3D array

3D array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K kDevloper

    Hi, How to initialise a 3D array through a function? Regards, KDevloper

    I Offline
    I Offline
    Iain Clarke Warrior Programmer
    wrote on last edited by
    #2

    int a [2][2][2] = { { {0,0}, {0,0} }, { {0,0}, {0,0} } );

    or...

    int a [9][7][5];
    initialise (a,9,7,5);

    ...

    void initialise (int ***a, int n1, int n2, int n3)
    {
    int i,j,k;
    for (i = 0; i < n1; i++)
    {
    for (j = 0; j < n2; j++)
    {
    for (k = 0; k < n2; k++)
    {
    a [n1][n2][n3] = 97;
    }
    }
    }
    }

    Tada! Note I initialise to 97. You hopefuly know more about what you want to do than I do, and can do things better. If not, go back to bed - you'll achieve more. Iain.

    K C 2 Replies Last reply
    0
    • I Iain Clarke Warrior Programmer

      int a [2][2][2] = { { {0,0}, {0,0} }, { {0,0}, {0,0} } );

      or...

      int a [9][7][5];
      initialise (a,9,7,5);

      ...

      void initialise (int ***a, int n1, int n2, int n3)
      {
      int i,j,k;
      for (i = 0; i < n1; i++)
      {
      for (j = 0; j < n2; j++)
      {
      for (k = 0; k < n2; k++)
      {
      a [n1][n2][n3] = 97;
      }
      }
      }
      }

      Tada! Note I initialise to 97. You hopefuly know more about what you want to do than I do, and can do things better. If not, go back to bed - you'll achieve more. Iain.

      K Offline
      K Offline
      kDevloper
      wrote on last edited by
      #3

      Thanks.

      1 Reply Last reply
      0
      • I Iain Clarke Warrior Programmer

        int a [2][2][2] = { { {0,0}, {0,0} }, { {0,0}, {0,0} } );

        or...

        int a [9][7][5];
        initialise (a,9,7,5);

        ...

        void initialise (int ***a, int n1, int n2, int n3)
        {
        int i,j,k;
        for (i = 0; i < n1; i++)
        {
        for (j = 0; j < n2; j++)
        {
        for (k = 0; k < n2; k++)
        {
        a [n1][n2][n3] = 97;
        }
        }
        }
        }

        Tada! Note I initialise to 97. You hopefuly know more about what you want to do than I do, and can do things better. If not, go back to bed - you'll achieve more. Iain.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #4

        Nah...

        Iain Clarke wrote:

        for (k = 0; k < n2; k++)

        should be

        for (k = 0; k < n3; k++)

        Iain Clarke wrote:

        a [n1][n2][n3] = 97;

        should be

        a [i][j][k] = 97;

        Did you disable the error checking feature of your mental compiler? ;P Please don't blame me, remeber: "this is going on my arrogant..." :-D

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        K I 2 Replies Last reply
        0
        • C CPallini

          Nah...

          Iain Clarke wrote:

          for (k = 0; k < n2; k++)

          should be

          for (k = 0; k < n3; k++)

          Iain Clarke wrote:

          a [n1][n2][n3] = 97;

          should be

          a [i][j][k] = 97;

          Did you disable the error checking feature of your mental compiler? ;P Please don't blame me, remeber: "this is going on my arrogant..." :-D

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          K Offline
          K Offline
          kDevloper
          wrote on last edited by
          #5

          Hi CPallini, I have not disabled my mental compiler, i understood, thats a typo error.So i corrected it from my side, without mentioning it.. Thanks & Regards, KDevloper

          C 1 Reply Last reply
          0
          • K kDevloper

            Hi CPallini, I have not disabled my mental compiler, i understood, thats a typo error.So i corrected it from my side, without mentioning it.. Thanks & Regards, KDevloper

            C Offline
            C Offline
            CPallini
            wrote on last edited by
            #6

            Beacause you're a fair, kind man. On the other hand, I've arrogant assumptions... :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            K R 2 Replies Last reply
            0
            • C CPallini

              Nah...

              Iain Clarke wrote:

              for (k = 0; k < n2; k++)

              should be

              for (k = 0; k < n3; k++)

              Iain Clarke wrote:

              a [n1][n2][n3] = 97;

              should be

              a [i][j][k] = 97;

              Did you disable the error checking feature of your mental compiler? ;P Please don't blame me, remeber: "this is going on my arrogant..." :-D

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              I Offline
              I Offline
              Iain Clarke Warrior Programmer
              wrote on last edited by
              #7

              I see KDeveloper is more generous of spirit than you. :(( I will now go away and sulk for at least 10 seconds. Not that I'd let you make a typo and get away with it either! :cool: Iain.

              C 1 Reply Last reply
              0
              • C CPallini

                Beacause you're a fair, kind man. On the other hand, I've arrogant assumptions... :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                K Offline
                K Offline
                kDevloper
                wrote on last edited by
                #8

                Hah.. :)

                1 Reply Last reply
                0
                • I Iain Clarke Warrior Programmer

                  I see KDeveloper is more generous of spirit than you. :(( I will now go away and sulk for at least 10 seconds. Not that I'd let you make a typo and get away with it either! :cool: Iain.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #9

                  Iain Clarke wrote:

                  Not that I'd let you make a typo and get away with it either!

                  Nah, you're just too kind and fair, like KDeveloper. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  1 Reply Last reply
                  0
                  • C CPallini

                    Beacause you're a fair, kind man. On the other hand, I've arrogant assumptions... :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #10

                    CPallini wrote:

                    I've arrogant assumptions...

                    But then I may have a superb reason...

                    It is a crappy thing, but it's life -^ Carlo Pallini

                    C 1 Reply Last reply
                    0
                    • R Rajesh R Subramanian

                      CPallini wrote:

                      I've arrogant assumptions...

                      But then I may have a superb reason...

                      It is a crappy thing, but it's life -^ Carlo Pallini

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #11

                      Yes. :-D BTW I really love that Iain's sentence. :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                      [My articles]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups