Much real data is a table: marks of several students in several subjects, a chessboard, a spreadsheet. A two-dimensional array stores such data with one index for the row and one for the column.
Two-dimensional arrays
Syntax
data-type array-name[rows][columns];
int table[2][3]; has 2 rows and 3 columns, 2 × 3 = 6 elements, with row indices 0–1 and column indices 0–2. An m × n array holds m × n elements and needs
m×n×size of one element
bytes. A chessboard is int board[8][8]; with 64 elements. Arrays with more dimensions are declared the same way, for example int cube[3][4][5];.
Row-major storage
C stores a multi-dimensional array row by row in consecutive memory, called row-major order: all of row 0, then all of row 1, and so on. The last subscript changes fastest.
Inner braces group the values of one row; missing values in a row become 0. Only the first dimension may be left empty; the number of columns must always be given.
Key takeaways
type a[rows][cols]; holds rows × cols elements; both indices start at 0.
Storage is row-major: row 0 first, then row 1, and so on.
Inner braces initialise one row each; missing values are 0.
Process with nested loops: rows outside, columns inside.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Write nested loops to add two 2 × 3 integer matrices a and b into c, and compute c[1][2] when a[1][2] is 6 and b[1][2] is 9.
Show worked answer
for (i = 0; i < 2; i++) for (j = 0; j < 3; j++) c[i][j] = a[i][j] + b[i][j];. The outer loop visits the 2 rows and the inner loop the 3 columns, so each of the 6 element pairs is added once. For row 1, column 2: c[1][2] = 6 + 9 = 15.
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-201/arrays-and-strings/multi-dimensional-arrays/ to read this page.