Thread: 2d arrays in C

  1. #1
    Unregistered
    Guest

    2d arrays in C

    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

  2. #2

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What kind of difficulty are you having with multidimensional arrays? Perhaps we can help you to understand better.

    -Prelude
    My best code is written with the delete key.

  4. #4
    Unregistered
    Guest
    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

  5. #5
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    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.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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:
    Code:
    int x[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    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[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 },
    };
    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:
    value = x[3][2];
    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:
    int i, j;
    for ( i = 0; i < 4; i++ )
      for ( j = 0; j < 9; j++ )
        x[i][j] = j + 1;
    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, *p;
    for ( i = 0, p = &x[0][0]; i < 4 * 9; i++ )
      *p++ = i + 1;
    With this knowledge in hand, multidimensional arrays are easy, simply add another array:
    Code:
    int x[4][9][2];
    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.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Unregistered
    Guest
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with 2d arrays
    By thamiz in forum C Programming
    Replies: 25
    Last Post: 05-25-2008, 05:06 AM
  2. 2D Array's, assigning chars.
    By gman89 in forum C Programming
    Replies: 9
    Last Post: 04-26-2008, 11:03 PM
  3. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM
  4. Reading 2d arrays from file?
    By Ringhawk in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2002, 09:05 PM
  5. how can i pass 2d arrays
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 11-02-2001, 07:33 AM