Nexus Learn

Lecture 21: 2D Arrays Memory

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

↓ Download notes (Word)

On this page
  1. Learning Outcomes
  2. 1. Why Tables? Rows and Columns
  3. 2. Declaring and Initialising
  4. 3. Using Elements and Printing the Table
  5. 4. Reading a Table From the Keyboard
  6. 5. Memory Layout (Row-Major Order)
  7. 6. Common Mistakes
  8. 7. Word Bank (Key Terms)
  9. 8. Self-Check Questions

UNIVERSITY OF AGRICULTURE, FAISALABAD

Department of Computer Science

LECTURE 21 · STUDENT NOTES

Multi-Dimensional Lists (2D Arrays) & Their Memory Layout

CS-307 Programming Fundamentals · BSCS Semester 1

Week

Slides

Linked practical

Reference ID

9

Lecture21_2D_Arrays_Memory.pptx

Practical 15

N21

Learning Outcomes

SLO

By the end of this lecture you can…

Builds CLO

Bloom level

SLO 21.1

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

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

C2

SLO 21.2

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

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

C3

SLO 21.3

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

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

C2

How to use these notes

Read these notes after the lecture and before the practical. Section numbers (for example §3.2) are used in the question bank, so you can always find where an answer comes from. Type every code example yourself — do not copy-paste.

1. Why Tables? Rows and Columns

A one-dimensional array is a single row of boxes. Many real data sets are tables: the marks of each student in each subject, the sales of each shop on each day. A two-dimensional (2D) array keeps a whole table under one name. (Slide L21-S05)

Table in real life

Rows

Columns

Result sheet

students

subjects

Class timetable

days

periods

Daewoo bus seat map

seat rows

seats A–D

Excel sheet

rows 1, 2, 3 …

columns A, B, C …

Usually the rows say 'who' (students, shops, cities) and the columns say 'what' (subjects, days).

2. Declaring and Initialising

Syntax: type name[ROWS][COLS]; — rows first, then columns. int marks[3][4]; has 3 × 4 = 12 elements. (Slide L21-S06)

From table2d.cpp

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

const int ROWS = 3, COLS = 4;

int marks[ROWS][COLS] = {

{72, 85, 64, 90}, // row 0: Ali

{58, 77, 81, 69}, // row 1: Sana

{91, 66, 74, 88} // row 2: Bilal

};

...

init2d.cpp

#include <iostream>

using namespace std;

int main() {

int g[2][3] = {{1}, {4, 5}}; // missing values become 0

int t[][3] = {1, 2, 3, 4, 5, 6}; // rows counted: 2

int z[2][3] = {}; // all 0

for (int r = 0; r < 2; r++) {

for (int c = 0; c < 3; c++) cout << g[r][c] << " ";

cout << "| ";

for (int c = 0; c < 3; c++) cout << t[r][c] << " ";

cout << "| ";

for (int c = 0; c < 3; c++) cout << z[r][c] << " ";

cout << endl;

}

return 0;

}

Output (g | t | z)

1 0 0 | 1 2 3 | 0 0 0

4 5 0 | 4 5 6 | 0 0 0

  • Give one brace list per row. Missing values become 0; {} makes every element 0.
  • The row count may be left out (C++ counts it), but the column size is always required.
  • int t[][] = {{1, 2}, {3, 4}}; gives: error: declaration of 't' as multidimensional array must have bounds for all dimensions except the first.

3. Using Elements and Printing the Table

marks[r][c] is the element in row r, column c. Both indexes start at 0. Printing uses a nested loop (Lecture 15): the outer loop goes through the rows, the inner loop through the columns, and endl comes after the inner loop. (Slide L21-S07)

Figure 21.1 – marks[1][2] is row 1, column 2 (Slide L21-S07)

table2d.cpp

#include <iostream>

#include <iomanip>

using namespace std;

int main() {

const int ROWS = 3, COLS = 4;

int marks[ROWS][COLS] = {

{72, 85, 64, 90}, // row 0: Ali

{58, 77, 81, 69}, // row 1: Sana

{91, 66, 74, 88} // row 2: Bilal

};

for (int r = 0; r < ROWS; r++) { // outer loop: rows

for (int c = 0; c < COLS; c++) { // inner loop: columns

cout << setw(5) << marks[r][c];

}

cout << endl; // end of a row

}

cout << "marks[1][2] = " << marks[1][2] << endl;

return 0;

}

Output

72 85 64 90

58 77 81 69

91 66 74 88

marks[1][2] = 81

4. Reading a Table From the Keyboard

Put cin >> sales[s][d]; inside the nested loop. One prompt per row; the user can type the whole row on one line because cin skips spaces. (Slide L21-S08)

input2d.cpp

#include <iostream>

using namespace std;

int main() {

const int SHOPS = 2, DAYS = 3;

int sales[SHOPS][DAYS];

for (int s = 0; s < SHOPS; s++) {

cout << "Sales of shop " << s + 1 << " for " << DAYS << " days: ";

for (int d = 0; d < DAYS; d++)

cin >> sales[s][d];

}

cout << "Day 2 sales of shop 2: " << sales[1][1] << endl;

cout << "Table:" << endl;

for (int s = 0; s < SHOPS; s++) {

for (int d = 0; d < DAYS; d++) cout << sales[s][d] << "\t";

cout << endl;

}

return 0;

}

Session

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

Loop limits

The outer loop runs to the number of rows, the inner loop to the number of columns. Mixing them up reads outside the table.

5. Memory Layout (Row-Major Order)

Memory is one long line of bytes (Lecture 5), so the table must be flattened. C++ stores row 0 first, then row 1, then row 2, all side by side. This is called row-major order. (Slide L21-S09)

Figure 21.2 – int a[2][3] stored row after row: offsets 0, 4, 8 | 12, 16, 20 (Slide L21-S09)

memory2d.cpp

#include <iostream>

using namespace std;

int main() {

int a[2][3] = {{10, 20, 30}, {40, 50, 60}};

cout << "sizeof(a) = " << sizeof(a) << " bytes" << endl;

cout << "sizeof(a[0]) = " << sizeof(a[0]) << " bytes (one row)" << endl;

cout << "sizeof(a[0][0]) = " << sizeof(a[0][0]) << " bytes" << endl;

for (int r = 0; r < 2; r++)

for (int c = 0; c < 3; c++) {

long gap = (char*)&a[r][c] - (char*)&a[0][0];

cout << "a[" << r << "][" << c << "] = " << a[r][c]

<< " starts " << gap << " bytes after a[0][0]" << endl;

}

return 0;

}

Output (lab PC, g++)

sizeof(a) = 24 bytes

sizeof(a[0]) = 12 bytes (one row)

sizeof(a[0][0]) = 4 bytes

a[0][0] = 10 starts 0 bytes after a[0][0]

a[0][1] = 20 starts 4 bytes after a[0][0]

a[0][2] = 30 starts 8 bytes after a[0][0]

a[1][0] = 40 starts 12 bytes after a[0][0]

a[1][1] = 50 starts 16 bytes after a[0][0]

a[1][2] = 60 starts 20 bytes after a[0][0]

5.1 The 2D address formula

address of a[r][c] = start + (r × COLS + c) × element size (Slide L21-S10). To reach row r we skip r whole rows of COLS elements, then c more. This is why the column size is required: without COLS, C++ cannot work out where a row starts.

Array

Element

r × COLS + c

Bytes from start

int a[2][3]

a[1][2]

1 × 3 + 2 = 5

20

int marks[40][4]

marks[10][3]

10 × 4 + 3 = 43

172

double t[7][3]

t[4][2]

4 × 3 + 2 = 14

112

Total size = ROWS × COLS × element size: int marks[40][4] uses 640 bytes; double t[7][3] uses 168 bytes.

6. Common Mistakes

(Slide L21-S12)

Mistake

Result

Fix

int t[][] = {…};

compile error: bounds needed

give the column size

marks[c][r] instead of marks[r][c]

wrong cell, or outside a non-square table

[row][column]

Outer loop r < COLS, inner c < ROWS

reads outside the table

rows outside, columns inside

endl inside the inner loop

one value per line

endl after the inner loop

Writing a[1, 2]

not a 2D index in C++

a[1][2]

7. Word Bank (Key Terms)

Word

Simple meaning

2D array

A table of values of one type: rows × columns

Row / column

A horizontal / vertical line of the table

a[r][c]

The element in row r, column c

Row-major order

Stored row after row in memory

Flatten

Lay a table out as one line

8. Self-Check Questions

1. int g[3][3] = {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}}; What is g[2][1]?

2. What is the sum of row 1 of g? Of the main diagonal?

3. sizeof(g)? How many bytes after g[0][0] does g[2][1] start?

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

5. Which size can be left out when initialising a 2D array?

6. In what order are 2D array elements stored?

Answers: 3; 15 and 15 (a magic square); 36 bytes, 28 bytes; 160 elements, 640 bytes; the row count (§2); row-major (§5). Homework is on Slide L21-S14.

← All CS-307 material