Table of Contents

    Multidimensional Array

    Programming Mastery

    Multidimensional Array

    Learn how multidimensional arrays store data in rows, columns, and higher-level structures such as tables, grids, matrices, and layers.

    What is a Multidimensional Array?

    A multidimensional array is an array that stores data in more than one dimension.

    In simple words, a multidimensional array is used when data cannot be represented properly in a single line. Instead of storing values only as a list, it stores values in structures such as rows and columns.

    A multidimensional array is an array that uses two or more indexes to access data arranged in multiple directions.

    The most common multidimensional array is the two-dimensional array, also called a 2D array. It stores data in rows and columns, like a table.

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

    Here, matrix is a two-dimensional array because it has rows and columns.

    Easy Real-Life Example

    Multidimensional Array as a Classroom Seating Chart

    Imagine a classroom where students sit in rows and columns. To find a student, you need two pieces of information: the row number and the column number.

    A two-dimensional array works in the same way. To access a value, we need a row index and a column index.

    Row 0: [Aman, Riya, Sohan]
    Row 1: [Meera, Ravi, Tina]
    Row 2: [Karan, Neha, Joy]

    To access Tina, we need row 1 and column 2.

    students[1][2] → Tina

    Why Do We Need Multidimensional Arrays?

    We need multidimensional arrays when data has a natural structure with more than one level.

    A one-dimensional array is good for a simple list:

    marks = [85, 90, 78, 88, 92]

    But if we need to store marks of multiple students in multiple subjects, a single list is not enough. We need rows and columns.

    marks = [
        [85, 90, 78],
        [88, 92, 81],
        [76, 84, 89]
    ]

    Here, each row may represent one student, and each column may represent one subject.

    Key Idea: Use a multidimensional array when data is naturally arranged as a table, grid, matrix, board, or layered structure.

    Types of Multidimensional Arrays

    Multidimensional arrays can have two, three, or more dimensions.

    1. Two-dimensional Array

    A two-dimensional array stores data in rows and columns.

    table = [
        [10, 20, 30],
        [40, 50, 60]
    ]

    This array has 2 rows and 3 columns.

    2. Three-dimensional Array

    A three-dimensional array stores data in layers, rows, and columns.

    data = [
        [
            [1, 2],
            [3, 4]
        ],
        [
            [5, 6],
            [7, 8]
        ]
    ]

    This can be imagined as multiple tables stacked together.

    3. Higher-dimensional Array

    Arrays can have more than three dimensions in some programming languages, but beginners usually focus on 2D and 3D arrays first.

    Main Parts of a Multidimensional Array

    Part Meaning Example
    Array Name The name used to identify the multidimensional array. matrix
    Row A horizontal line of elements. [1, 2, 3]
    Column A vertical position inside rows. Column 0, column 1
    Element A value stored inside the array. 5
    Indexes Positions used to access an element. matrix[row][column]
    Dimension The number of directions or levels in the array. 2D, 3D

    General Syntax of Multidimensional Array

    The exact syntax differs across programming languages, but the general idea is:

    arrayName = [
        [row1Values],
        [row2Values],
        [row3Values]
    ]

    Example:

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

    This is a 2D array with 3 rows and 3 columns.

    Indexing in a Two-dimensional Array

    In a one-dimensional array, we use one index.

    marks[index]

    In a two-dimensional array, we use two indexes:

    arrayName[rowIndex][columnIndex]

    Example:

    matrix = [
        [10, 20, 30],
        [40, 50, 60],
        [70, 80, 90]
    ]
    
    matrix[0][0] → 10
    matrix[0][2] → 30
    matrix[1][1] → 50
    matrix[2][0] → 70
    Important: In most programming languages, row and column indexes start from 0.

    Accessing Elements in a Multidimensional Array

    To access an element in a 2D array, mention its row index and column index.

    matrix = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ]
    
    DISPLAY matrix[1][2]

    Expected Output

    6

    Here, matrix[1][2] means row 1, column 2.

    Updating Elements in a Multidimensional Array

    We can update a value by using its row and column index.

    matrix = [
        [1, 2, 3],
        [4, 5, 6]
    ]
    
    SET matrix[0][1] = 20
    
    DISPLAY matrix

    Updated Array

    [
        [1, 20, 3],
        [4, 5, 6]
    ]

    The value at row 0, column 1 changed from 2 to 20.

    Traversing a Multidimensional Array

    Traversing means visiting each element one by one.

    To traverse a two-dimensional array, we usually use nested loops:

    • The outer loop controls rows.
    • The inner loop controls columns.
    matrix = [
        [1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]
    ]
    
    FOR row FROM 0 TO numberOfRows(matrix) - 1
        FOR column FROM 0 TO numberOfColumns(matrix) - 1
            DISPLAY matrix[row][column]
        END FOR
    END FOR

    Example 1: Display a 2D Array as a Matrix

    /*
    This program displays a two-dimensional array as a matrix.
    */
    
    ENTRY POINT
        DECLARE matrix AS ARRAY = [
            [1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]
        ]
    
        FOR row FROM 0 TO 2
            FOR column FROM 0 TO 2
                DISPLAY matrix[row][column] WITHOUT NEW LINE
                DISPLAY " " WITHOUT NEW LINE
            END FOR
    
            DISPLAY NEW LINE
        END FOR
    END ENTRY POINT

    Expected Output

    1 2 3
    4 5 6
    7 8 9

    Example 2: Calculate Sum of All Elements

    /*
    This program calculates the sum of all elements in a 2D array.
    */
    
    ENTRY POINT
        DECLARE matrix AS ARRAY = [
            [10, 20, 30],
            [40, 50, 60]
        ]
    
        DECLARE total AS INTEGER = 0
    
        FOR row FROM 0 TO 1
            FOR column FROM 0 TO 2
                SET total = total + matrix[row][column]
            END FOR
        END FOR
    
        DISPLAY "Total: " + total
    END ENTRY POINT

    Expected Output

    Total: 210

    Example 3: Find Highest Value in a 2D Array

    /*
    This program finds the highest value in a two-dimensional array.
    */
    
    ENTRY POINT
        DECLARE scores AS ARRAY = [
            [45, 78, 92],
            [66, 81, 59],
            [88, 73, 95]
        ]
    
        DECLARE highest AS INTEGER = scores[0][0]
    
        FOR row FROM 0 TO 2
            FOR column FROM 0 TO 2
                IF scores[row][column] > highest THEN
                    SET highest = scores[row][column]
                END IF
            END FOR
        END FOR
    
        DISPLAY "Highest Score: " + highest
    END ENTRY POINT

    Expected Output

    Highest Score: 95

    Example 4: Student Marks Table

    A 2D array can store marks of multiple students in multiple subjects.

    /*
    Rows represent students.
    Columns represent subjects.
    */
    
    ENTRY POINT
        DECLARE marks AS ARRAY = [
            [80, 75, 90],
            [85, 88, 92],
            [70, 65, 78]
        ]
    
        FOR student FROM 0 TO 2
            DECLARE total AS INTEGER = 0
    
            FOR subject FROM 0 TO 2
                SET total = total + marks[student][subject]
            END FOR
    
            DISPLAY "Student " + (student + 1) + " Total: " + total
        END FOR
    END ENTRY POINT

    Expected Output

    Student 1 Total: 245
    Student 2 Total: 265
    Student 3 Total: 213

    Understanding 3D Arrays

    A 3D array has three levels of indexing. It can be imagined as multiple 2D arrays stacked together.

    data = [
        [
            [1, 2],
            [3, 4]
        ],
        [
            [5, 6],
            [7, 8]
        ]
    ]

    A 3D array may be understood as:

    • Layer or block index.
    • Row index.
    • Column index.
    data[layer][row][column]

    Example:

    data[1][0][1] → 6

    Common Operations on Multidimensional Arrays

    Operation Meaning Example Idea
    Access Read a value using multiple indexes. matrix[1][2]
    Update Change a value at a row and column. matrix[0][1] = 20
    Traversal Visit all elements using nested loops. Print all values.
    Search Find whether a value exists. Search a seat number.
    Sum Add all numeric values. Total marks in a table.
    Row Total Add values from one row. Total marks of one student.
    Column Total Add values from one column. Total marks in one subject.

    One-dimensional vs Multidimensional Array

    Feature One-dimensional Array Multidimensional Array
    Structure Single row or list. Rows, columns, or layers.
    Indexes Required One index. Two or more indexes.
    Example marks[index] matrix[row][column]
    Best For Simple list of values. Tables, grids, matrices, boards, and layered data.

    Advantages of Multidimensional Arrays

    Benefits

    • They store structured data in rows and columns.
    • They are useful for tables, grids, matrices, and boards.
    • They keep related multi-level data organized.
    • They reduce the need for multiple separate arrays.
    • They work well with nested loops.
    • They are useful for mathematical and scientific calculations.
    • They help represent real-world structures more naturally.
    • They prepare students for matrix operations, image processing, and grid-based logic.

    Limitations of Multidimensional Arrays

    Limitations

    • They can be harder to understand than one-dimensional arrays.
    • They require multiple indexes.
    • They often need nested loops.
    • Incorrect row or column indexes may cause errors.
    • Large multidimensional arrays can use more memory.
    • Debugging can become difficult if the structure is not clear.

    Common Beginner Mistakes

    Mistakes

    • Confusing row index and column index.
    • Thinking indexes start from 1 instead of 0.
    • Using only one loop for a 2D array.
    • Accessing a row or column outside the valid range.
    • Forgetting to reset row-based totals inside the outer loop.
    • Writing unclear nested loops.
    • Not indenting nested loops properly.
    • Using a multidimensional array when a simple list is enough.

    Better Habits

    • Remember the format array[row][column].
    • Use meaningful loop variables such as row and column.
    • Use nested loops for 2D arrays.
    • Check row and column limits carefully.
    • Use clear indentation.
    • Draw the table before writing logic.
    • Test with small arrays first.
    • Use comments when the dimension meaning is not obvious.

    Best Practices for Multidimensional Arrays

    Recommended Practices

    • Use multidimensional arrays only when data naturally has multiple dimensions.
    • Use 2D arrays for tables, grids, matrices, and boards.
    • Use meaningful names such as marksTable, matrix, grid, or board.
    • Use row and column as loop variables for clarity.
    • Always check valid row and column ranges.
    • Use nested loops carefully and keep indentation clean.
    • Start with small arrays while practicing.
    • Write expected output before writing logic.
    • Use diagrams or tables to understand the data structure.
    • Avoid unnecessary dimensions if the problem can be solved with a simpler structure.

    Prerequisites Before Learning Multidimensional Arrays

    Students should already understand:

    Required Knowledge

    • Variables and data types.
    • Input and output.
    • Operators.
    • Conditions.
    • Loops and nested loops.
    • Functions and methods basics.
    • What is an array?
    • Why arrays are used.
    • One-dimensional arrays.

    Practice Activity: Identify Row and Column

    Study the following two-dimensional array:

    numbers = [
        [10, 20, 30],
        [40, 50, 60],
        [70, 80, 90]
    ]

    Questions

    1. What is the value at numbers[0][0]?
    2. What is the value at numbers[1][2]?
    3. What is the value at numbers[2][1]?
    4. How many rows are there?
    5. How many columns are there?

    Sample Answers

    1. numbers[0][0] = 10
    2. numbers[1][2] = 60
    3. numbers[2][1] = 80
    4. Rows = 3
    5. Columns = 3

    Mini Quiz

    1

    What is a multidimensional array?

    A multidimensional array is an array that stores data in more than one dimension, such as rows and columns.

    2

    What is the most common multidimensional array?

    The most common multidimensional array is the two-dimensional array.

    3

    How many indexes are needed for a 2D array?

    Two indexes are needed: one for the row and one for the column.

    4

    Why are nested loops used with 2D arrays?

    Nested loops are used because one loop handles rows and another loop handles columns.

    5

    Give one real-life use of a multidimensional array.

    A multidimensional array can be used for student marks tables, game boards, matrices, seat arrangements, or spreadsheets.

    Interview Questions on Multidimensional Arrays

    1

    Define multidimensional array.

    A multidimensional array is an array that uses two or more dimensions to store data in a structured format.

    2

    What is a two-dimensional array?

    A two-dimensional array stores data in rows and columns, similar to a table or matrix.

    3

    How do you access an element in a 2D array?

    An element in a 2D array is accessed using row and column indexes, such as array[row][column].

    4

    What is the difference between 1D and 2D arrays?

    A 1D array stores values in a single line, while a 2D array stores values in rows and columns.

    5

    Why are multidimensional arrays useful?

    They are useful because they represent structured data such as tables, grids, matrices, and layered information.

    Quick Summary

    Concept Meaning
    Multidimensional Array An array with more than one dimension.
    2D Array An array with rows and columns.
    3D Array An array with layers, rows, and columns.
    Row Index The index used to select a row.
    Column Index The index used to select a column.
    Access Format array[row][column] for 2D arrays.
    Traversal Usually done using nested loops.
    Best Use Tables, grids, matrices, game boards, and layered data.

    Final Takeaway

    A multidimensional array helps programmers store structured data using multiple indexes. The most common type is the two-dimensional array, which stores data in rows and columns. In the Programming Mastery Course, students should understand multidimensional arrays as a powerful way to represent tables, grids, matrices, game boards, and layered data using arrays and nested loops.