Pascal’s triangle arranges numbers in rows: each row starts and ends with 1, while each interior entry sums adjacent values from the preceding row. The n-th row contains n numbers. section 2.0 section 2.2.1
The entries count combinations. For coin tosses, the row 1, 3, 3, 1 counts the ways to obtain two heads, one head, or none across three tosses. section 2.2.1
A program can construct and examine the triangle by:
- Representing each row with a vector<int> and calculating a new row from the previous one. section 2.2.2
- Returning data from generation functions and separating display from calculation so the results remain testable. section 2.2.2
- Checking properties such as boundary 1s, row length, and expected sums with assertions. section 2.3 section 2.3.2
Generating the next row
Given 1, 2, 1, start the next row with 1, add adjacent pairs to obtain 3 and 3, and finish with 1: 1, 3, 3, 1. Repeating this process generates several rows. section 2.2.1 section 2.2.2
Listing 2.2 The next row of Pascal’s triangle using the previous row
std::vector<int> get_next_row(const std::vector<int> & last_row)
{
std::vector next_row{ 1 }; #1
if (last_row.empty())
{
return next_row;
}
for (size_t idx = 0; idx+1 < last_row.size(); ++idx)
{
next_row.emplace_back(last_row[idx] + last_row[idx + 1]); #2
}
next_row.emplace_back(1);
return next_row;
}
Inspect the C++ function that creates a row, including its boundary values, adjacent additions, and vector construction. section 2.2.2
Listing 2.11 Ensuring each row has the expected number of elements
size_t row_number = 1; #1
for (const auto & row : triangle)
{
assert(row.front() == 1);
assert(row.back() == 1);
assert(row.size() == row_number++); #2
}
Inspect the assertions that verify boundary values and the number of entries in each row. section 2.3.2

