Skip to main content
Logo image

Section 24.2 Matrices

Just like we can have a 2-dimensional array of characters (or array of strings), it is also possible to have multidimensional arrays of other datatypes. Here is an example of a 2-dimensional array of integers (also known as a matrix):
int matrix[2][3];
This declares (reserves space for) a 2-dimensional array of integers by the name of matrix, which has 2 rows and 3 columns. More generally, the following code could be used to declare a 2-dimensional array of integers with NROWS rows and NCOLS columns:
int numbers[NROWS][NCOLS];
This of course assumes that somewhere in your code you have told the computer what values NROWS and NCOLS take, for example via
#define NROWS 3
#define NCOLS 2
at the top of your code.
The following video illustrates how to work with 2-dimensional arrays.

If you cannot see this codecast, please click here.

Check Your Understanding Check Your Understanding

1.

Suppose that integers are stored using 4 bytes, and you are declaring a variable matrix using
int matrix[7][9];
How much space (in bytes) is reserved in memory by this declaration? Please enter just a number, do not enter the word ’bytes’.