How to Use Multidimensional Arrays in C Programming

When you need to describe items in the second or third dimension, you can use C programming to conjure forth a multidimensional type of array.

How to make a two-dimensional array

It helps to think of a two-dimensional array as a grid of rows and columns. An example of this type of array is a chess board — a grid of 8 rows and 8 columns. Though you can declare a single 64-element array to handle the job of representing a chess board, a two-dimensional array works better. Such a thing would be declared this way:

int chess[8][8];

The two square brackets define two different dimensions of the chess array: 8 rows and 8 columns. The square located at the first row and column would be referenced as chess[0][0]. The last square on that row would be chess[0][7], and the last square on the board would be chess[7][7].

In Tic-Tac-Toe, a simple tic-tac-toe board is created using a two-dimensional matrix: 3-by-3. Lines 9 through 11 fill in the matrix. Line 12 adds an X character in the center square.

TIC-TAC-TOE

#include <stdio.h>
int main()
{
 char tictactoe[3][3];
 int x,y;
/* initialize matrix */
 for(x=0;x<3;x++)
 for(y=0;y<3;y++)
 tictactoe[x][y]='.';
 tictactoe[1][1] = 'X';
/* display game board */
 puts("Ready to play Tic-Tac-Toe?");
 for(x=0;x<3;x++)
 {
 for(y=0;y<3;y++)
 printf("%c\t",tictactoe[x][y]);
 putchar('\n');
 }
 return(0);
}

Lines 14 through 21 display the matrix. As with its creation, the matrix is displayed by using a nested for loop.

Exercise 1: Create a new project using the source code shown in Tic-Tac-Toe. Build and run.

A type of two-dimensional array that’s pretty easy to understand is an array of strings, as shown in An Array of Strings.

AN ARRAY OF STRINGS

#include <stdio.h>
#define SIZE 3
int main()
{
 char president[SIZE][8] = {
 "Clinton",
 "Bush",
 "Obama"
 };
 int x,index;
 for(x=0;x<SIZE;x++)
 {
 index = 0;
 while(president[x][index] != '\0')
 {
 putchar(president[x][index]);
 index++;
 }
 putchar('\n');
 }
 return(0);
}

Line 7 in An Array of Strings declares a two-dimensional char array: president. The first value in square brackets is the number of items (strings) in the array. The second value in square brackets is the maximum size required to hold the largest string. The largest string is Clinton with seven letters, so eight characters are required, which includes the terminating \0 or null character.

Because all items in the array’s second dimension must have the same number of elements, all strings are stored using eight characters. Yep, that’s wasteful, but it’s the way the system works.

image0.jpg

Exercise 2: Type the source code from An Array of Strings into your editor; build and run the program.

The statements basically plod through the president array’s second dimension, spitting out one character at a time.

Exercise 3: Replace Lines 15 through 23 in An Array of Strings with a single puts() function to display the string. Here’s how that statement looks:

puts(president[x]);

When working with string elements in an array, the string is referenced by the first dimension only.

Exercise 4: Modify your source code from Exercise 3 so that three more presidents are added to the array: Washington, Adams, and Jefferson.

How to create three-dimensional arrays

Two-dimensional arrays are pretty common in the programming realm. Multidimensional is insane!

Well, maybe not. Three- and four-dimensional arrays have their place. The big deal is that your human brain has trouble keeping up with the various possible dimensions.

Going 3D illustrates code that works with a three-dimensional array. The declaration is found at Line 5. The third dimension is simply the third set of square brackets, which effectively creates a 3D tic-tac-toe game board.

GOING 3D

#include <stdio.h>
int main()
{
 char tictactoe[3][3][3];
 int x,y,z;
/* initialize matrix */
 for(x=0;x<3;x++)
 for(y=0;y<3;y++)
 for(z=0;z<3;z++)
 tictactoe[x][y][z]='.';
 tictactoe[1][1][1] = 'X';
/* display game board */
 puts("Ready to play 3D Tic-Tac-Toe?");
 for(z=0;z<3;z++)
 {
 printf("Level %d\n",z+1);
 for(x=0;x<3;x++)
 {
 for(y=0;y<3;y++)
 printf("%c\t",tictactoe[x][y][z]);
 putchar('\n');
 }
 }
 return(0);
}

Lines 8 through 12 fill the array with data, using variables x, y, and z as the three-dimensional coordinates. Line 13 places an X character in the center cube, which gives you an idea of how individual elements are referenced.

The rest of the code from Lines 15 through 26 displays the matrix.

Exercise 5: Create a three-dimensional array program using the source code from Going 3D. Build and run.

Lamentably, the output is two-dimensional.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com