PDA

Click to See Complete Forum and Search --> : I need help on 2 dimensonal arrays!, any good sites?


struntz
Jul 22nd, 2001, 12:40 PM
Hello everyone!!

I was wondering if anyone here knew any good sites that explain two-dimensional arrays well? I've read 2 books(well not the whole thing but the 2 dimensional array part) one was Teach Yourself C++ in 21 days :eek: and the other was a book i bought. For some reason i can't get the hang of it!! Can someone explain to me what they are all about or have another good site that explains them?


THanks for your time!! :D

Vlatko
Jul 22nd, 2001, 12:52 PM
To best see the use of a 2D array imagine a chess board. It is like a 2D coordinate system. To mark a field you need two values.
The filed [8][8] would be in the corner. It can also be represented as field[64] but [8][8] is far more unsderstandable.

parksie
Jul 22nd, 2001, 12:57 PM
Following on from the whole "indirection" theme, a 2D array is basically an array of arrays. For example int x[8] is an array of 8 ints. int x[8][8] is an array of 8 int [x]s.

Am I being clear? I hope so, but probably not because this is nasty stuff :(

struntz
Jul 22nd, 2001, 01:05 PM
in this example:


void read(int a[] [5]);

void print(const int a[] [5]);

main()
{
int a[3][5];
read(a);
print(a);
}

void read(int a[][5])
{
//code
}
void print(const int a[][5])
{
//code
}


why don';t you have to say what the first element [ ] is? but u must say waht the 2nd one is.

THe part i don't understand of 2 dimensional arrays is what each means....like for instance..what does this create?


int a[4][5];

does it create 4 groups of arrays with 5 elements in each array?

like
[][][][][]
[][][][][]
[][][][][]
[][][][][]

can u explain it with boxes? :rolleyes:

kedaman
Jul 22nd, 2001, 04:54 PM
I'm not quite sure what your sample means, i usually don't use arrays, especially not multidimensional. If i'm correct you'd need to specify each but first dimension since to count the distance to an element from the first, you need all but the dimension which is wrapping the others, that is the first.

To understand dimensions you need to understand what a scale is. A dimension is a scale with a unit, and if you have sets of elements (as you call them boxes) they are arrays of that dimension, the scale unit is an element. In a multidimensioned array the elements of the earlier dimension is containing all elements of the next dimension, so if the element you need to access is within the second element of the first dimension, the element is after the last element of the last dimension in the first element of the first dimension. IN a two dimensioned array, you could see it like this:

[1][2][3][4][5]
[6][7][8][9][10]

where [1,0] is the 6'th element, just after the last element in the first.