MCS-011 Problem Solving and Programming

First year, Semester 1

Chapters

Newsletter

Multi-dimensional Arrays

Multi-dimensional arrays are arrays with more than one dimension, used to represent data in a grid or table format.

Syntax:

datatype array_name[size1][size2]...[sizeN];

where datatype is the type of elements (e.g., int, double), and size1, size2, ..., sizeN are the sizes of each dimension.

Example:

An 8x8 grid can be represented using a two-dimensional array, similar to algebraic notation in chess.

Note: Arrays can have multiple dimensions, but higher dimensions increase memory usage. For instance, a 20x20x20x20 array of double-precision numbers uses about 1.28 MB.


Two-Dimensional Arrays

Two-dimensional arrays use two indices, e.g., array[rows][columns]. The upper left element is array[0][0], the next is array[0][1], etc. Multi-dimensional arrays are stored linearly in memory, with the last index varying fastest.

Syntax for a two-dimensional array: datatype array_name[size1][size2];. Example for an 8x8 integer array: int chessboard[8][8];

Example: 

int table[2][3] = { {1, 2, 3}, {4, 5, 6} };


Results in:

table[0][0] = 1;

table[0][1] = 2;

table[0][2] = 3;

table[1][0] = 4;

table[1][1] = 0;

table[1][2] = 0;


No Bounds Checking: C does not check array bounds, so accessing out-of-bounds elements can cause errors. It is the programmer's responsibility to ensure valid indices are used.

Report an issue

Reporting: Multi-dimensional Arrays (topic)

Related Posts