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!
... 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:
# 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.
# 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.