# Multi-Dimensional Arrays, Matrices and 2D Arrays

Intro to 2-Dimensional Arrays
ðŸ”—
Representation of Multi Dimensional Arrays in Memory
ðŸ”—

Arrays are fundamental data structures in programming, offering a convenient way to organize, manipulate and access data. But sometimes, simple arrays aren’t enough. For example, if you need to represent more complex structures like spreadsheets, tables, matrices, or collections of collections we need to go beyond 1 dimensional linear arrays. This is where arrays of arrays, also known as multi-dimensional arrays, come into picture. In this introductory article, we will go through the basics of multi-dimensional arrays, explore their structure and applications.

## Understanding 2 Dimensional Arrays

Two-dimensional arrays offer a structured way to programmatically solve problems that involve grids, matrices, or any data that naturally fits into rows and columns. For example, if you need to simulate a chess board, programmatically solve sudoku puzzle, work with image pixels data, or organize student grades, 2D arrays are a good fit.

If you’re familiar with one-dimensional arrays, think of 2D arrays as an array of arrays. Instead of simply holding a list of values, each element within a 2D array is another one-dimensional array.

For example, below is a basic one-dimensional array containing numbers:

``````[1, 2, 3]
``````

Below is an example of a 2-Dimensional Array where each element in the main array is a one-dimensional array:

``````[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
``````

The same 2D array can be reformatted in a different way:

``````[
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]
``````

The above format is more intuitive to visualize. The first element `[1,4,7]` in the 2-Dimensional array corresponds to the first row, `[2,5,8]` corresponds to the second row and so on. Similarly `[1,2,3]` corresponds to the first column, `[3,6,9]` corresponds to the second column and so on.

### Practical Example

Assume that there is a class with `4` students. All students have written exams for a total of `3` subjects. The goal is to store individual marks obtained by each student in each of the subject.

Below is how is how we can store marks of all students in all the subjects:

``````[
[81, 75, 92], # First student's marks
[86, 94, 79], # Second student's marks
[84, 90, 75], # Third student's marks
[79, 75, 89]  # Fourth student's marks
]
``````

We declared a 2-dimensional array containing `4` rows where each row contains `3` columns. Each row corresponds to marks obtained in all the subjects for a particular student. For example the marks obtained by first student is `81, 75, 92` in first, second and third subjects respectively. Similarly the second student had obtained `86, 94, 79` marks respectively and so on.

For details on initializing, accessing and traversing a 2-dimensional array, refer to the next articles in this section.

## Understanding Multi Dimensional Arrays

Multi-dimensional arrays take the concept of a 2D array and extend it further. Just like a 2D array can be thought of as an array of arrays, a 3D array can be thought of as an array of arrays of arrays! You can have even higher dimensions as needed.

While 2D arrays are great for grids and tables, multi-dimensional arrays come in handy when you need to model structures that extend into additional dimensions. For example, consider a use case where we need to track history of temperatures across various coordinates on earth. For this a good fit would be a 3D array where the first dimension represents latitude, the second dimension represents longitude, and the third dimension represents time. Each element in the array could store the temperature, pressure, or other weather data for a specific location at a specific time.

## Practical Applications

Arrays of arrays find applications in a wide range of real-world scenarios:

1. Matrices: In mathematics and computer graphics, matrices are essential for transformations, rotations, and more. You can represent a matrix as an array of arrays.
2. Tables and Grids: When working with tabular data or grids, like in spreadsheets or game boards, Multi-dimensional arrays offer an intuitive way to model the structure.
3. Images: Images are often represented as two-dimensional arrays of pixels, with each containing color information.
4. Chessboards: Chessboards can be simulated using two dimensional arrays of arrays, with each square holding information about the piece on it.

In the next tutorial, we will go through details of creating multi-dimensional arrays in different programming languages along with how to access and work with them.