Lesson 14 • Advanced

Multidimensional Arrays

30 minutes
Advanced Data Structures

📊 What are Multidimensional Arrays?

Multidimensional arrays are arrays of arrays - they store data in a grid or table format. The most common are 2D arrays, which have rows and columns like a spreadsheet.

Understanding 2D Arrays
Think of a 2D array like:
  • Spreadsheet: Rows and columns of data
  • Chessboard: 8×8 grid of squares
  • Seating Chart: Rows and columns of seats
  • Image: Grid of pixels
Instead of one index array[i], you use two: array[row][col]

📐 2D Array Declaration

# Declare 2D array
var grid[rows][columns]

# Example: 3×4 array (3 rows, 4 columns)
var matrix[3][4]
Example: Basic 2D Array
Algorithm TwoDArrayBasics

# Create 3×3 grid
var grid[3][3]

# Fill with values
grid[0][0] = 1
grid[0][1] = 2
grid[0][2] = 3

grid[1][0] = 4
grid[1][1] = 5
grid[1][2] = 6

grid[2][0] = 7
grid[2][1] = 8
grid[2][2] = 9

# Display grid
Print "3×3 Grid:"
Print ""

For row = 0 To 2
    For col = 0 To 2
        Print grid[row][col], " "
    Endfor
    Print ""
Endfor

Endalgorithm
Output
3×3 Grid: 1 2 3 4 5 6 7 8 9

Matrix Operations

Fill Matrix with Pattern

Example: Fill Matrix
Algorithm FillMatrix

var rows = 4
var cols = 5
var matrix[rows][cols]

# Fill with sequential numbers
var value = 1

For row = 0 To rows - 1
    For col = 0 To cols - 1
        matrix[row][col] = value
        value = value + 1
    Endfor
Endfor

# Display matrix
Print "Matrix (4×5):"
Print ""

For row = 0 To rows - 1
    For col = 0 To cols - 1
        Print matrix[row][col], " "
    Endfor
    Print ""
Endfor

Endalgorithm

Matrix Sum

Example: Sum All Elements
Algorithm MatrixSum

var matrix[3][3]

# Fill matrix with input
Print "Enter 9 numbers for 3×3 matrix:"
Print ""

For row = 0 To 2
    For col = 0 To 2
        matrix[row][col] = Input "Row " + row + ", Col " + col + ":"
    Endfor
Endfor

# Calculate sum
var total = 0

For row = 0 To 2
    For col = 0 To 2
        total = total + matrix[row][col]
    Endfor
Endfor

Print ""
Print "Sum of all elements:", total

Endalgorithm

🎮 Real-World Examples

Seating Chart

Example: Theater Seating
Algorithm TheaterSeating

var rows = 5
var cols = 8
var seats[rows][cols]

# Initialize all seats as available (0 = available, 1 = taken)
For row = 0 To rows - 1
    For col = 0 To cols - 1
        seats[row][col] = 0
    Endfor
Endfor

# Mark some seats as taken
seats[0][3] = 1
seats[1][2] = 1
seats[2][5] = 1

# Display seating chart
Print "=== THEATER SEATING CHART ==="
Print "[O] = Available  [X] = Taken"
Print ""

For row = 0 To rows - 1
    Print "Row ", row + 1, ": "
    
    For col = 0 To cols - 1
        If seats[row][col] == 0 Then
            Print "[O] "
        Else
            Print "[X] "
        Endif
    Endfor
    Print ""
Endfor

# Count available seats
var available = 0
For row = 0 To rows - 1
    For col = 0 To cols - 1
        If seats[row][col] == 0 Then
            available = available + 1
        Endif
    Endfor
Endfor

Print ""
Print "Available seats:", available

Endalgorithm

Grade Book System

Example: Student Grade Book
Algorithm GradeBook

var students = 3
var tests = 4
var grades[students][tests]

var names = ["Alice", "Bob", "Charlie"]

Print "=== GRADE INPUT ==="
Print ""

# Input grades
For s = 0 To students - 1
    Print "Grades for", names[s], ":"
    For t = 0 To tests - 1
        grades[s][t] = Input "  Test " + (t + 1) + ":"
    Endfor
    Print ""
Endfor

# Calculate and display averages
Print "=== STUDENT AVERAGES ==="
Print ""

For s = 0 To students - 1
    var sum = 0
    
    For t = 0 To tests - 1
        sum = sum + grades[s][t]
    Endfor
    
    var average = sum / tests
    Print names[s], ":", average
Endfor

Endalgorithm

Tic-Tac-Toe Board

Example: Tic-Tac-Toe
Algorithm TicTacToe

var board[3][3]

# Initialize empty board
For row = 0 To 2
    For col = 0 To 2
        board[row][col] = "-"
    Endfor
Endfor

# Make some moves
board[0][0] = "X"  # Player X
board[1][1] = "O"  # Player O
board[0][2] = "X"
board[2][1] = "O"
board[1][0] = "X"

# Display board
Print "=== TIC-TAC-TOE ==="
Print ""

For row = 0 To 2
    For col = 0 To 2
        Print board[row][col], " "
    Endfor
    Print ""
Endfor

Endalgorithm
Output
=== TIC-TAC-TOE === X - X X O - - O -

Key Takeaways

  • ✅ 2D arrays store data in rows and columns
  • ✅ Access elements with array[row][col]
  • ✅ Use nested loops to traverse 2D arrays
  • ✅ Perfect for grids, matrices, tables, and boards
  • ✅ Both row and column indices start at 0
  • ✅ Can represent spreadsheets, game boards, images, and more

💪 Practice Exercises

Try These Exercises

Exercise 1: Matrix Transpose
Write a program that transposes a matrix (swap rows and columns). Example: [1,2,3][4,5,6] becomes [1,4][2,5][3,6].
Exercise 2: Battleship Grid
Create a 10×10 battleship board. Place ships and implement a guessing system.
Exercise 3: Matrix Multiplication
Implement matrix multiplication for two 2×2 matrices (advanced math operation).
Exercise 4: Sudoku Grid
Create a 9×9 Sudoku grid, fill it with numbers, and display it with proper formatting.
Fantastic Progress!
You now understand how to work with multidimensional data! In the next lesson, we'll explore recursion - functions that call themselves!