Nexus Learn

Lecture 21: 2D Arrays Memory

Lecture Slides CS-307 Programming Fundamentals 6 min read Updated 8 Nov 2026

↓ Download slides (PowerPoint)

Slide 2

Quick Recap of Lecture 20

4 minutes · homework check

  • Quick-fire
  • Bubble pass 1 → largest at the end
  • Selection → minimum position
  • Swap → needs temp
  • Today
  • So far a list was one row of boxes. Many real data sets are tables: marks of each student in each subject. Today: the 2D array.

Recap

Programming Fundamentals · Lecture 21

Slide 3

Today's Plan

50 minutes · 10 parts

Recap & homework check

4 min

Learning outcomes

2 min

Why tables? Rows and columns

5 min

Declaring & initialising

8 min

Using a[r][c]; printing with nested loops

8 min

Reading a table from the keyboard

4 min

Memory layout (row-major)

8 min

Class activity

6 min

Mistakes & recap

4 min

Homework & next lecture

1 min

Programming Fundamentals · Lecture 21

Slide 4

Learning Outcomes (SLOs)

By the end of this lecture, you will be able to: (tag = course outcome it builds · Bloom level)

Explain a two-dimensional array as a table of rows and columns

SLO 21.1 → CLO4 · C2

Declare, initialise, read and print a 2D array with nested loops

SLO 21.2 → CLO4 · C3

SLO 21.3 → CLO4 · C2

Describe row-major memory layout and find where a[r][c] is stored

CLO4 Describe the syntax and semantics of the C++ programming language.

Programming Fundamentals · Lecture 21

Slide 5

Why Tables? Rows and Columns

3 students × 4 subjects = 12 marks

[0]

[1]

[2]

[3]

  • Without a table
  • int ali[4], sana[4], bilal[4];
  • … and 40 names for 40 students?

Ali

[0]

Sana

[1]

Bilal

[2]

columns = PF · Calc · Eng · Phy

  • With a 2D array
  • int marks[40][4];
  • one name · row = student · column = subject
  • Tables around you
  • Timetable · bus seat map · Excel sheet · cricket scorecard

Module 1 · Tables

Programming Fundamentals · Lecture 21

Slide 6

Declaring and Initialising a 2D Array

type name[ROWS][COLS];

Declarations (table2d.cpp, init2d.cpp)

init2d.cpp: g | t | z = {}

  • const int ROWS = 3, COLS = 4;
  • int marks[ROWS][COLS] = {
  • {72, 85, 64, 90}, // row 0
  • {58, 77, 81, 69}, // row 1
  • {91, 66, 74, 88} // row 2
  • };
  • int g[2][3] = {{1}, {4, 5}}; // 1 0 0 / 4 5 0
  • int t[][3] = {1, 2, 3, 4, 5, 6}; // 2 rows counted
  • 1 0 0 | 1 2 3 | 0 0 0
  • 4 5 0 | 4 5 6 | 0 0 0
  • Rows first
  • marks[3][4] = 3 rows, 4 columns = 12 elements

dims_bug.cpp

  • g++ says
  • error: declaration of 't' as multidimensional array must have bounds for all dimensions except the first

int t[][] = {{1, 2}, {3, 4}}; // BUG

Module 2 · Declare & Initialise

Programming Fundamentals · Lecture 21

Slide 7

Using Elements and Printing the Table

marks[r][c] → row r, column c · outer loop rows, inner loop columns

[0]

[1]

[2]

[3]

table2d.cpp (main part)

[0]

  • for (int r = 0; r < ROWS; r++) { // rows
  • for (int c = 0; c < COLS; c++) { // columns
  • cout << setw(5) << marks[r][c];
  • }
  • cout << endl; // end of row
  • }

[1]

[2]

marks[1][2] = 81

Output

  • Order matters
  • [row][column] — not [column][row]
  • 72 85 64 90
  • 58 77 81 69
  • 91 66 74 88

Module 3 · Using a[r][c]

Programming Fundamentals · Lecture 21

Slide 8

Reading a Table From the Keyboard

cin >> sales[s][d] inside the nested loop

input2d.cpp (main part)

Session

  • const int SHOPS = 2, DAYS = 3;
  • int sales[SHOPS][DAYS];
  • for (int s = 0; s < SHOPS; s++) {
  • cout << "Sales of shop " << s + 1 << " for 3 days: ";
  • for (int d = 0; d < DAYS; d++)
  • cin >> sales[s][d];
  • }
  • Sales of shop 1 for 3 days:
  • 4500 6200 3800
  • Sales of shop 2 for 3 days:
  • 5100 9300 7000
  • Day 2 sales of shop 2: 9300
  • Table:
  • 4500 6200 3800
  • 5100 9300 7000
  • Remember
  • Rows = SHOPS, columns = DAYS. Loop limits must match: r < ROWS outside, c < COLS inside. Mixing them reads outside the table.

Module 3 · Using a[r][c]

Programming Fundamentals · Lecture 21

Slide 9

How a 2D Array Sits in Memory

Row-major order: row 0, then row 1, then row 2 — all in one line

[0]

[1]

[2]

[0]

→

[1]

byte offset from a[0][0] · row 0 | row 1

Output (part)

memory2d.cpp (main part)

  • cout << sizeof(a); // 24
  • cout << sizeof(a[0]); // 12 (one row)
  • cout << sizeof(a[0][0]); // 4
  • long gap = (char*)&a[r][c] - (char*)&a[0][0];
  • a[0][2] = 30 starts 8 bytes …
  • a[1][0] = 40 starts 12 bytes …
  • a[1][2] = 60 starts 20 bytes …

Module 4 · Memory Layout

Programming Fundamentals · Lecture 21

Slide 10

The 2D Address Formula

address of a[r][c] = start + (r × COLS + c) × element size

  • Why COLS is needed
  • To skip r rows, C++ must know how long a row is.
  • Total size
  • sizeof(table) = ROWS × COLS × element size. marks[40][4] → 640 bytes; double t[7][3] → 168 bytes.
  • The real addresses change every run; the offsets do not.

Module 4 · Memory Layout

Programming Fundamentals · Lecture 21

Slide 11

CLASS ACTIVITY · 6 MIN · IN PAIRS

The 15 Puzzle

int g[3][3] = {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}}; Draw the grid, then answer.

Questions

Hints

  • 1. What is g[2][1]?
  • 2. What is the sum of row 1?
  • 3. What is the sum of the main diagonal g[0][0] + g[1][1] + g[2][2]?
  • 4. sizeof(g)? How many bytes after g[0][0] does g[2][1] start?
  • 1: row 2, column 1
  • 2: 9 + 5 + 1
  • 3: same row and column
  • 4: (2 × 3 + 1) × 4

Slide 12

Common Mistakes With 2D Arrays

Most bugs: swapped indexes and swapped loop limits

Say it aloud: 'row r, column c' — and keep ROWS and COLS as named constants.

Module 5 · Mistakes

Programming Fundamentals · Lecture 21

Slide 13

Quick Recap

Answer aloud

Q1

How many elements does int marks[40][4] have, and how many bytes?

Q2

Which size can be left out when you initialise a 2D array, and why not the other?

Q3

In which order are the elements of a 2D array stored in memory?

Q4

Where does a[1][2] of int a[2][3] start, in bytes from a[0][0]?

Module 5 · Mistakes

Programming Fundamentals · Lecture 21

Slide 14

Homework

In the 3 × 4 marks table, find the highest mark with its row and column, and count the marks above 80.

Declare double t[7][3] for 7 days (rows) × 3 cities (columns). How many bytes? Where does t[4][2] start?

Read a 3 × 3 grid of numbers and print it with rows and columns swapped (row 0 printed as column 0).

Explain why int t[][] = {{1, 2}, {3, 4}}; does not compile, and correct it.

Next lecture: 2D array applications — row & column totals, matrices and a bus seat map

← All CS-307 material