Software developer working on programming cod

C++ program to generate Pascal’s Triangle

by Amit Kumar

A Pascal’s Triangle in C++ can be represented using a 2D vector and generated by following a specific numerical pattern where each number is the sum of the two numbers directly above it. Here’s a brief explanation of what a Pascal’s Triangle is and how you might implement it in C++:

What is Pascal’s Triangle?

Pascal’s Triangle is a triangular array of binomial coefficients. It is named after the French mathematician Blaise Pascal, although other mathematicians studied it centuries before him in India, Persia, China, Germany, and Italy.

The rows of Pascal’s Triangle are conventionally enumerated starting with row n=0 at the top. The entries in each row are numbered from the left beginning with k=0 and are usually staggered relative to the numbers in the adjacent rows. The triangle may be constructed in the following manner:

  • In row 0 (the topmost row), there is a unique nonzero entry 1.
  • Each entry of each subsequent row is constructed by adding the number above and to the left with the number above and to the right, treating blank entries as 0.

Pascal’s Triangle in C++

Here’s a simple example of how you could represent Pascal’s Triangle in C++:

#include <iostream>
#include <vector>

std::vector<std::vector<int>> generatePascalTriangle(int numRows) {
    std::vector<std::vector<int>> pascal(numRows);

    for (int i = 0; i < numRows; i++) {
        pascal[i].resize(i + 1);    // Resize the current row to the correct size
        pascal[i][0] = pascal[i][i] = 1;  // Set the first and last element of each row to 1

        // Fill in the middle elements of the row
        for (int j = 1; j < i; j++) {
            pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
        }
    }

    return pascal;
}

int main() {
    int numRows = 5;
    auto triangle = generatePascalTriangle(numRows);

    // Print the Pascal's Triangle
    for (const auto &row : triangle) {
        for (int num : row) {
            std::cout << num << " ";
        }
        std::cout << std::endl;
    }

    return 0;
}

Explanation:

  1. Headers and Namespaces:
    • #include <iostream> includes the IO stream library to use std::cout.
    • #include <vector> includes the vector library to use std::vector.
  2. generatePascalTriangle Function:
    • This function takes an integer numRows as an argument, representing the number of rows in the triangle.
    • It initializes a vector of vector of integers to store the numbers of Pascal’s Triangle.
  3. Filling the Triangle:
    • The outer for loop iterates over each row.
    • pascal[i].resize(i + 1) ensures that each row has the correct number of elements.
    • pascal[i][0] = pascal[i][i] = 1; sets the first and last elements of each row to 1.
    • The inner for loop calculates the inner numbers of the triangle by summing the two numbers directly above the current position.
  4. main Function:
    • int numRows = 5; sets the number of rows to be generated.
    • auto triangle = generatePascalTriangle(numRows); calls the function and stores the triangle.
    • The nested for loops iterate over the triangle and print it to the console.

When you run this program, it will generate and print Pascal’s Triangle with the number of rows specified by numRows.

Related Posts

Leave a Comment