liveBook · Manning

Pascal's triangle

As explained in Learn C++ by Example

Pascal’s triangle is a triangular arrangement of numbers in which each interior entry is the sum of the two entries above it and the n-th row contains n numbers.

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

How Learn C++ by Example covers it

How deep it goes
Explains construction, combinations, vector-based generation, numeric limits, and property tests without developing deeper mathematics.
Examples
Uses coin-toss outcomes, adjacent-row calculations, C++ generation functions, and assertions over row properties.
What it assumes
Assumes a compiler and editor or IDE, while introducing vector storage, range-based output, and property-oriented testing.
What you can do after
Build it from scratch in C++ using vectors, generate rows, display results, and diagnose errors through assertions.
How it connects
Connects Pascal’s triangle with combinations, vectors, ranges, algorithms, assertions, and unit testing.

Also explained in

Compare how 2 books explain Pascal's triangle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn C++ by Example. The section links point to the parts of the book it draws on.