Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Understanding the Problem

Course: DSA › Arrays › Multidimensional Arrays

Now that we understand what an array data structure is and how to use it, solving problems involving multiple data items of the same type may seem straightforward. However, this is not always the case. What we have learned so far is called single-dimensional arrays. To better understand situations where this approach may not be sufficient, let us examine an example.


The School Classes Problem

We’ll build on the student ages example from before. This time, consider a scenario where we need to store the ages of students for every class in a school. The school has classes ranging from 1st to 4th standard, and each class has 60 students.

The natural first instinct: create four separate integer arrays of size 60, one per class.

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart LR
    c1["class1"] --> r1["6 │ 6 │ 7 │ 7 │ 6 │ ... │ 6 │ 7 │ 6 │ 6"]
    c2["class2"] --> r2["7 │ 7 │ 8 │ 8 │ 7 │ ... │ 8 │ 8 │ 7 │ 8"]
    c3["class3"] --> r3["8 │ 8 │ 9 │ 8 │ 9 │ ... │ 9 │ 8 │ 9 │ 9"]
    c4["class4"] --> r4["9 │ 9 │ 10 │ 9 │ 9 │ ... │ 9 │ 9 │ 9 │ 10"]

Creating 4 arrays to store the ages of students in 4 classes.

Four classes, four arrays. Manageable — barely.


What If There Were 12 Classes?

This approach works for a small number of classes, but what if there were 12 classes instead of four? We’d need 12 separate arrays. While this technically solves the problem, storing and managing so many arrays across multiple variables would be cumbersome. This would bring us back to square one and undermine the very purpose of using arrays.

---
config:
  theme: base
  themeVariables:
    primaryColor: "#dbeafe"
    primaryBorderColor: "#3b82f6"
    primaryTextColor: "#1e3a5f"
    lineColor: "#64748b"
    secondaryColor: "#ede9fe"
    tertiaryColor: "#fef9c3"
---
flowchart LR
    c1["class1"] ~~~ c2["class2"] ~~~ c3["class3"] ~~~ c4["class4"] ~~~ c5["class5"] ~~~ c6["class6"]
    c7["class7"] ~~~ c8["class8"] ~~~ c9["class9"] ~~~ c10["class10"] ~~~ c11["class11"] ~~~ c12["class12"]

    style c1 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c2 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c3 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c4 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c5 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c6 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c7 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c8 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c9 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c10 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c11 fill:#dbeafe,stroke:#3b82f6,color:#1e3a5f
    style c12 fill:#fee2e2,stroke:#ef4444,color:#7f1d1d

Creating 12 arrays to store the ages of students in 12 classes — one per variable.

Each of those 12 nodes is a separate named variable holding 60 values. And what if the school grows to 50 classes next year? 100? The approach doesn’t scale.


Limitations of Single-Dimensional Arrays

A lot can be done using simple 1D arrays. But when data is naturally structured in multiple dimensions — rows and columns, classes and students, grids and matrices — single-dimensional arrays hit a wall.

Here’s why:

  • Different arrays holding the same type of data must have unique namesclass1, class2, … class12 are all just differently-named variables
  • Using multiple arrays to store related data is not scalable — adding a new class means adding a new variable and updating every loop manually
  • Having too many arrays makes the code complex and error-prone — keeping track of which array belongs to which class is entirely the programmer’s problem
  • Data relationships are harder to represent when spread across separate arrays — you lose the natural structure of “row = class, column = student”

The pattern repeats. We saw this exact problem before — when variables couldn’t scale to hold many values, arrays solved it. Now, when single arrays can’t scale to hold many related arrays, we need the next step up: multidimensional arrays.

Computers are designed to solve problems at scale, and challenges like these arise even in the simplest software. That’s why even low-level programming languages provide built-in support for adding multiple dimensions to arrays.


Key Takeaway

SituationRight tool
Store many values of the same type1D array
Store many related groups of values2D (multidimensional) array
Store a grid / matrix / table of values2D (multidimensional) array

When you find yourself naming arrays data1, data2, data3… you need a multidimensional array.