This forum is in archive mode. You will not be able to post new content.

Author Topic: C/C++ 3D arrays and more  (Read 1042 times)

0 Members and 1 Guest are viewing this topic.

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
C/C++ 3D arrays and more
« on: July 21, 2014, 12:35:46 AM »
Hey there i thought i help out the curious noobs and anyone else who wishes to know about multidimensional arrays in c/c++.This is kind of a tutorial but its not....i don't know  :-\ .

Multidimensional Arrays in c/c++
In c/c++ we declare and initialize arrays like:

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

//2D array
int array[2][5]={1,2,3,4,5,6,7,8,9,10};
The above are quite easy but what about:

int array[4][3][5];

so its also easy! :P ... to work out such an initialization you need to think groups and blocks.To initialize the above 3D array it will be like this:

int MultiArray[4][3][5]={{
{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,28,29,30}},
   {{31,32,33,34,35},{36,37,38,39,40},{41,42,43,44,45}},
   {{46,47,48,49,50},{51,52,53,54,55},{56,57,58,59,60}}
};

so now lets go about it.The array is in a blocks [4][3][5] so its like this:

1.)4 blocks

 i.{1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}},
  ii.{{16,17,18,19,20},{21,22,23,24,25},{26,27,28,29,30}},
   iii.{{31,32,33,34,35},{36,37,38,39,40},{41,42,43,44,45}},
   iv.{{46,47,48,49,50},{51,52,53,54,55},{56,57,58,59,60}}

2.)in each of the 4 blocks there is a block of 3.

 i.){1,2,3,4,5}  ii.){6,7,8,9,10} iii.){11,12,13,14,15}

3.)In each of the 3 blocks there are 5 values.

{1...2...3...4...5}   //5 values
{6...7...8...9...10}

so simply we have interpreted [4][3][5] means. ....Everytime you think of multidimensional arrays in majority of the languages think of grouping and blocks.The greatest compilation error one will always make with c/c++ multidimensional arrays is ....{{{{}}}}....parenthesis!. Shit! if you code 3D vectors this is the most annoying error you will have to deal with..."noob error" ::) .
Anyway its quit easy to deal with remember grouping and blocks so:


{ #this is the start of the array

{#start  {1,2,3,4,5},{6,7,8,9,10},{11,12,13,14,15}  #end},
 
#each block is grouped together and separated by a comma

{  {16,17,18,19,20},{21,22,23,24,25},{26,27,28,29,30}    },

{  {31,32,33,34,35},{36,37,38,39,40},{41,42,43,44,45}    },

{  {46,47,48,49,50},{51,52,53,54,55},{56,57,58,59,60}    }
}; #end of the array

the code will look like:
Code: (C) [Select]

# include<stdio.h>
main()
{
int i,j,k;
int MultiArray[4][3][5]={{
{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,28,29,30}},
   {{31,32,33,34,35},{36,37,38,39,40},{41,42,43,44,45}},
   {{46,47,48,49,50},{51,52,53,54,55},{56,57,58,59,60}}
};
for(i=0;i<4;i++)
         { for(j=0;j<3;j++)

             { for(k=0;k<5;k++)
                   {printf("Element[%d][%d][%d]=%d\n",i,j,k,MultiArray[i][j][k]);
                               }
                                   }
                                       }
return 0;
}

Notice that to access the array we used loops each dealing with the blocks and grouping.
Now for assessment....try coding  this:

char century[100][365][24][60][60];

unless you have the kind of RAM and CPU don't be stupid!.... this array declares an array with an element of type char for each second in a century. It requires almost 3GB RAM memory.The amount of memory needed for an array increases exponentially with each dimension.But theoretically this array groups data into 100 blocks each with 365 blocks containing 24 blocks with 60 blocks having each with 60 values.....  ??? .....Say What? yeah thats just about it, but we could ague if you like. Note i have said based on my opinion unless anybody else whats to add something....
Assigning multidimensional arrays is quite easy with loops once you get the logic.
 
Code: (C) [Select]

# include<iostream>
using namespace std;
int main()
{
int i,j,k;
int array[4][3][5]; //3D array.

for(i=0;i<4;i++)
    {
    for(j=0;j<3;j++)
         {
           for(k=0;k<5;k++)
              {array[i][j][k]=i+j+k;
              cout<<"Element["<<i<<"]["<<j<<"]["<<k<<"]="<<array[i][j][k]<<endl;
}
}
}
return 0;
}


Thats all. :)
« Last Edit: July 21, 2014, 07:16:37 AM by Kulverstukas »

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: c/c++ 3D and more Multidimensional
« Reply #1 on: July 21, 2014, 06:30:11 AM »
Sweet! learned something myself :P nice contribution and keep it up.
Have a cooke.

Oh btw - edited the title and moved to tutorials.

Offline carriers666

  • NULL
  • Posts: 1
  • Cookies: 0
    • View Profile
Re: C/C++ 3D arrays and more
« Reply #2 on: July 22, 2014, 11:08:00 AM »
Cool stuff , but why do not use std::vector to create multi dimensional arrays ?

Offline $Clone

  • Peasant
  • *
  • Posts: 86
  • Cookies: 5
  • $---Shadowalker---$
    • View Profile
Re: C/C++ 3D arrays and more
« Reply #3 on: July 24, 2014, 10:11:03 PM »
Cool stuff , but why do not use std::vector to create multi dimensional arrays ?
you can but i wanted a method where both C and C++ can be tackled together..."kill two birds with one stone...", as they say! ;)

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.