Could anyone suggest a good reference or know where you can get any good C tutorials that will help teach you how to use more than 1 array. I have tried a few resources, but nothing I've found has been very good to this point.
-Thnx
Printable View
Could anyone suggest a good reference or know where you can get any good C tutorials that will help teach you how to use more than 1 array. I have tried a few resources, but nothing I've found has been very good to this point.
-Thnx
What kind of difficulty are you having with multidimensional arrays? Perhaps we can help you to understand better.
-Prelude
Thanks for your responses. Prelude, I'm taking a class in C and they're only touching arrays at the first level, and I just want to learn more. I was just looking for any pointers, or samples I could look at. I know 2d+ arrays are probably really easy for you, but I'm just starting with arrays. How did you learn them best? Just by doing them over and over?
-Thnx Nic
A 1D array is a vector and can be declared as:
int array_1d [LENGTH];
Here LENGTH is the number of elements in the array. Each element can be retrieved by its index. Note that the indices start at 0 and therefore the last element in the array has index LENGTH-1.
first_element = array_1d [0];
last_element = array_1d [LENGTH-1];
You can put values in arrays in this way:
array_1d [index] = value;
A 2D array is a matrix and can be declared as:
int array_2d [ROWS][COLUMS];
As with 1D arrays, the indices start at 0. The last row is ROWS-1 and the last colum is COLUMS-1.
[a, b, c, d]
[e, f, g, h]
[i, j, k, l]
So element a can be retrieved as:
a = array_2d [0][0];
l = array_2d [2][3];
Hope it explains a little.
>I was just looking for any pointers, or samples I could look at.
I'll use two dimensional arrays to explain because once you understand them, adding more dimensions is easy. A one dimensional array is easy to figure out, it's a block of items with the same data type running in order:
With the previous declaration, you can easily see how the array is supposed to look, numbers from one to nine in order. A two dimensional array is an array of arrays:Code:int x[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
By carefully writing the declaration you can show how a two dimensional array looks as well. There are 4 arrays with 9 elements as opposed to the first declaration which was one array with 9 elements. To access one of the elements in a two dimensional array you simply give a subscript for which array to use, and then another subscript for which element of that array:Code:int x[4][9] =
{
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
{ 1, 2, 3, 4, 5, 6, 7, 8, 9 },
};
The value variable would now hold the item contained in the third element of the fourth array, note that array indexes start at zero. That's what a two dimensional array is, and it is used in a similar manner as a one dimensional array with added code to handle both levels. A common practice is to use nested loops to populate a 2D array:Code:value = x[3][2];
Whereas a 1D array would only require a single loop. Note that a 2D array can also be populated by a single loop, but the syntax is less intuitive:Code:int i, j;
for ( i = 0; i < 4; i++ )
for ( j = 0; j < 9; j++ )
x[i][j] = j + 1;
With this knowledge in hand, multidimensional arrays are easy, simply add another array:Code:int i, *p;
for ( i = 0, p = &x[0][0]; i < 4 * 9; i++ )
*p++ = i + 1;
This is an array of four arrays of two arrays. Each element of the first dimension is an array of nine elements where each of those elements is an array of two elements. The usage is very similar to a 2D array except for extra code to handle the new dimension. You can continue adding dimensions this way without getting confused. In case you were wondering, the most dimensions I've ever used is 8. I tried more but got a stack overflow with 9 or more. ;)Code:int x[4][9][2];
-Prelude
Thank you both. It was very helpful. That's what I wanted was a good ground to go on. You're funny Prelude. I'm not quite up to 8 yet. :)
Thnx Nic