|
||||||
| C Tutorials All C Tutorials and Code |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
||||
|
Multi-dimensional arrays
Earlier,I talked about 1-dimensional arrays, arrays with one index. There is nothing that says an array has to be limited to 1 dimension. In fact, you can have an arbitrary number of dimensions in an array. 2 dimensional arrays are quite common, especially for storing things like screen coordinates or tables of data. Higher dimensions are used when you have more aspects that are required to specify a unique item (3-D coordinates, for example).
An important thing to understand about arrays is this: technically, there is no such thing as a multi-dimensional array. All arrays are 1-dimensional, but some of them contain arrays, or arrays of arrays. What this means is that when you see Code:
int multiarray[4][2] Code:
0 1 0 [0][0] [0][1] 1 [1][0] [1][1] 2 [2][0] [2][1] 3 [3][0] [3][1] If you think this is getting messy, you're right. If you think there's a lot of room for errors in code based on this, you're very right. Let's add to the mess. I said that the array cannot be stored as a grid, so let's look at how it is store. multiarray has three elements, so it looks something like this: Code:
| whatever's in 0 || whatever's in 1 || whatever's in 2 || whatever's in 3 |
multiarray[0] multiarray[1] multiarray[2] multiarray[3]
Code:
memory: |[int0] [int1]||[int0] [int1]||[int0] [int1]||[int0] [int1]| multiarray: [0] [1] [2] [3] multiarray: [0][0] [0][1] [1][0] [1][1] [2][0] [2][1] [3][0] [3][1] Let's look at the two ways we can access this array with functions. Code:
#include <iostream>
void setarray(int ma[][2],int size)
{
for (int i=0;i<size;i++)
for (int j=0;j<2;j++)
ma[i][j] = 10*i+j;
}
void printarray(int* ma,int dim1, int dim2)
{
for (int i=0;i<dim1;i++)
{
for (int j=0;j<dim2;j++)
std::cout<<*(ma+(i*dim2+j))<<" ";
std::cout<<"\n";
}
}
int main()
{
int multiarray[4][2];
setarray(multiarray,4);
printarray(&multiarray[0][0],4,2);
}
Code:
0 1 10 11 20 21 30 31 !
__________________
CodeCall Blog | CodeCall Wiki | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
|
|||
|
Re: Multi-dimensional arrays
Some things that you may consider adding to this tutorial or a part two (Maybe I should write a tutorial, I always get half way done and say **** it).
Explain that the way multidimensional arrays are stored in memory changes on a language to language basis. In the case of C/C++ they use row major ordering. Which you give an example above. However other languages use column major ordering which is a bit different (go down the columns listing the data and then return to the top of the next column). Another thing, Bjarne explicitly says that multidimensional arrays above 2 dimensions should be avoided whenever possible. |
|
||||
|
Re: Multi-dimensional arrays
@Methodz, you're right that implementation of arrays is language specific. Using 3+ dimensional arrays is generally a sign that you're doing something wrong. One problem is the rate at which memory is consumed. int[100] uses 100 ints, int[100][100] uses 10,000 ints, int[100][100][100] uses 1,000,000 ints. In general, you're looking at 100^n, where n is the number of dimensions. That can very quickly consume ridiculous amounts of memory.
The only time I can think of to legitimately use a 3-dimensional array is for a board game like 3D chess (8x8x3) or something similar. I'd love to see you write some tutorials.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Programming is a branch of mathematics. My CodeCall Blog | My Personal Blog |
|
||||
|
Re: Multi-dimensional arrays
The thread is alive!
![]() You might try typecasting and seeing if that works. I know if it's just a variable you can initialize it with that, I don't see that as an issue with C++.
__________________
![]() Google - I'm Feeling Lucky | Youtube Classes! My Portfolio | Email Me | QuestionBin |
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Char arrays in C | outsid3r | C Tutorials | 4 | 07-28-2009 04:38 PM |
| C++ How to use arrays, and why they're important | WingedPanther | C Tutorials | 9 | 12-10-2008 11:40 AM |
| What arrays and pointers have in common - or - Why arrays start at zero | Kernel | News | 0 | 05-07-2008 08:36 AM |
| Dynamic Arrays | Fedex | C and C++ | 3 | 12-02-2007 05:45 PM |
| Arrays | clookid | PHP Tutorials | 1 | 01-11-2007 09:30 PM |
All times are GMT -5. The time now is 11:00 AM.
Amrosama.cc
Arekbulski.cc
Debtboy.cc
Guest.cc
Jaan.cc
James.cc
Mathx.cc
Tsz.cc
Vswe.cc