Exforsys

Home arrow Technical Training arrow C Tutorials

C Multidimensional Arrays

Page 2 of 2
Author:      Published on: 13th Apr 2006    |   Last Updated on: 28th Mar 2011

C Programming - Arrays

Multidimensional Arrays

Arrays can have more than one dimension. Two dimensional arrays are widely used for tables, neural networks and etc. You can have as many dimensions as you would like but you have to consider the complexity of the code as you add new dimension. Multidimensional arrays allow you to store data in a spreadsheet or matrix like format.

Ads

Declaring Multidimensional Arrays

To declare a multidimensional array:

Sample Code
  1. <data type> array_name[size_of_first_dimension][size_of_second_dimension] ...
Copyright exforsys.com


A very good example is the creation of 2D maps for games.

Sample Code
  1. char game_map[10][10];
Copyright exforsys.com


This would declare a 2 dimensional array of chars that is 10 columns by 10 rows in size.

You could even do a 3D map for a game using a 3 dimensional array.

Sample Code
  1. char game_map[10][10][5];
Copyright exforsys.com


You can consider above array as being 10 tiles by 10 tiles on the horizontal plane, and 5 tiles high on the vertical plane.

Initializing Multidimensional Arrays

Just like single dimension array, you can initialize the multidimensional array also upon declaration as well as initialize by assignment.

Sample Code
  1. <data type> array_name[size_of_dimension1][size_of_second_dimension2] ... = {
  2. {element 1, element 2, element 3, …},
  3. .
  4. .
  5. . }
  6. };
Copyright exforsys.com


Sample Code
  1. char game_map[2][4];
Copyright exforsys.com


array [Y][X]
game_map[2][4];
X direction
Y direction game_map[0][0] game_map[0][1] game_map[0][2] game_map[0][3]
game_map[1][0] game_map[2][1] game_map[3][2] game_map[4][3]

Sample Code
  1. char game_map[2][4] = { {'x', 'b', 'f', '&#65533;'} , {'b', 't', '&#65533;' } };
Copyright exforsys.com


array [Y][X]
game_map[2][4];
X direction
Y direction x b f
b t empty

Sample Code
  1. int game_map[2][4];
  2. game_map[0][0]=1; game_map[0][1]=2; game_map[0][2]=3; game_map[0][3]=4
  3. game_map[1][0]=6; game_map[1][1]=7; game_map[1][2]=8;
Copyright exforsys.com


array [Y][X]
game_map[2][4];
X direction
Y direction 1 2 3 4
6 7 5 0

Here is what the above 2D game map array will look like if you decided to initialize it during declaration.

Sample Code
  1. char game_map[10][10] = {
  2. {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
  3. {'X', 'H', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  4. {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  5. {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  6. {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  7. {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  8. {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  9. {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  10. {'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  11. {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'}
  12. };
Copyright exforsys.com


The first index accesses the rows in the array, the second one accesses the columns. Dimensions more than 2 gets difficult to work with. As far as initializing a 3 or more dimensional array goes, you usually only see them assigned values using a series of nested for loops. Here is a simple example.

Sample Code
  1. //loop through first dimension
  2. for (i = 0; i < 10; i++) {
  3.    //loop through second dimension
  4.    for (j = 0; j < 10; j++) {
  5.      //loop through third dimension
  6.      for (k = 0; k < 10; k++)
  7.           int_array[i][j][k]) = i * j * k;
  8.      }
  9. }
Copyright exforsys.com


Accessing Elements in Multidimensional Arrays

To access elements in a multidimensional array, all you have to do is provide a combination of index values for each dimension to reach the desired element, just like a bunch of cells in a spreadsheet or coordinates in a n dimensional matrix or graph.

Sample Code
  1. array_name[dim1_index][dim2_index] ... [dimn-1_index];
Copyright exforsys.com


Setting values in a multidimensional array is just as simple as in a basic array, you need to add another index per dimension.

Sample Code
  1. array_name[index1][index2] ... [index s-1] = value;
Copyright exforsys.com


Let's return to our game_map 2d array for an example of setting a value in a multidimensional array. We could position our hero character in the game_map array using this snippet:

Sample Code
  1. game_map[1][1] = 't';
Copyright exforsys.com


That would insert the character 'H' at the intersection of the cell found at coordinate pair (1, 1) in the array.
Here we will look at retrieving a value from a multidimensional array using the above syntax. If we wanted to print out our game level map we declared earlier so that it gets displayed in the console we would use the following group of nested for loops to go through both dimensions and print every value we encounter.

Sample Code
  1. for (i = 0; i < 10; i++) {
  2.      for (j = 0; j < 10; j++) {
  3.           printf("%c", game_map[i][j]);
  4.      }
  5. }
Copyright exforsys.com


The first for loop goes through the index values from 0 to 9 of the first dimension of the 2d array. The second for loop does the same for the second dimension. We use the stdio built-in printf function to print the char (“%c”) found at the coordinate pair (i, j). because of the nested loops, this code will print every value in the 2d array game_map out to the console from indexes (0, 0) through (9, 9).

Ads

Sample Program

The code provided below defines a level in a treasure hunting game. It shows how to declare and initialize both single and multi-dimensional array. The code shows how to access and index of each element using a for loop.

Sample Code
  1. //Treasure Hunter
  2. //Very simple example C programming game.
  3. //Example snippet for illustrating use of arrays.
  4.  
  5. #include <stdio.h>
  6. void main(void) {
  7.   int i, j;
  8.   char hero = 'H';
  9.  
  10.   //Declares an array with 2 elements and initializes it.
  11.  
  12.   char obstacles[3] = {'X', ' ', 'T'};
  13.  
  14.   //Declare and initialize a 2 dimensional array of 10 rows and 10 columns.
  15.  
  16.   //The use of obstacles array in the first column is just as an example of how
  17.   //to access the value of an array.
  18.   char game_map[10][10] = {
  19.     {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
  20.     {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  21.     {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  22.     {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  23.     {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  24.     {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  25.     {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  26.     {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  27.     {obstacles[0], ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'X'},
  28.     {'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X', 'X'},
  29.   };
  30.  
  31.   //Place our hero at position 1,1 on the map.
  32.   game_map[1][1] = hero;
  33.  
  34.   //Print the whole 2d array.
  35.   //loop through first dimension
  36.   for (i = 0; i < 10; i++) {
  37.     //loop through second dimension
  38.     for (j = 0; j < 10; j++) {
  39.       //print each array element.
  40.       printf("%c", game_map[i][j]);
  41.     }
  42.  
  43.     //Add newline at end to ensure each row gets printed correctly.
  44.     printf("n");
  45.   }
  46. }
Copyright exforsys.com


When you compile and run the above program, you will see output as shown below:



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments