Task: Pascal matrix generation


C++: #include <iostream>
#include <vector>

typedef std::vector<std::vector<int>> vv;

vv pascal_upper(int n) {
    vv matrix(n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
                if (i > j) matrix[i].push_back(0);
                else if (i == j || i == 0) matrix[i].push_back(1);
                else matrix[i].push_back(matrix[i - 1][j - 1] + matrix[i][j - 1]);
            }
        }
        return matrix;
    }

vv pascal_lower(int n) {
    vv matrix(n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (i < j) matrix[i].push_back(0);
            else if (i == j || j == 0) matrix[i].push_back(1);
            else matrix[i].push_back(matrix[i - 1][j - 1] + matrix[i - 1][j]);
        }
    }
    return matrix;
}

vv pascal_symmetric(int n) {
    vv matrix(n);
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if (i == 0 || j == 0) matrix[i].push_back(1);
            else matrix[i].push_back(matrix[i][j - 1] + matrix[i - 1][j]);
        }
    }
    return matrix;
}


void print_matrix(vv matrix) {
    for (std::vector<int> v: matrix) {
        for (int i: v) {
            std::cout << " " << i;
        }
        std::cout << std::endl;
    }
}

int main() {
    std::cout << "PASCAL UPPER MATRIX" << std::endl;
    print_matrix(pascal_upper(5));
    std::cout << "PASCAL LOWER MATRIX" << std::endl;
    print_matrix(pascal_lower(5));
    std::cout << "PASCAL SYMMETRIC MATRIX" << std::endl;
    print_matrix(pascal_symmetric(5));
}









Fortran: module pascal

implicit none

contains
    function pascal_lower(n) result(a)
        integer :: n, i, j
        integer, allocatable :: a(:, :)
        allocate(a(n, n))
        a = 0
        do i = 1, n
            a(i, 1) = 1
        end do
        do i = 2, n
            do j = 2, i
                a(i, j) = a(i - 1, j) + a(i - 1, j - 1)
            end do
        end do
    end function
    
    function pascal_upper(n) result(a)
        integer :: n, i, j
        integer, allocatable :: a(:, :)
        allocate(a(n, n))
        a = 0
        do i = 1, n
            a(1, i) = 1
        end do
        do i = 2, n
            do j = 2, i
                a(j, i) = a(j, i - 1) + a(j - 1, i - 1)
            end do
        end do
    end function

    function pascal_symmetric(n) result(a)
        integer :: n, i, j
        integer, allocatable :: a(:, :)
        allocate(a(n, n))
        a = 0
        do i = 1, n
            a(i, 1) = 1
            a(1, i) = 1
        end do
        do i = 2, n
            do j = 2, n
                a(i, j) = a(i - 1, j) + a(i, j - 1)
            end do
        end do
    end function

    subroutine print_matrix(a)
        integer :: a(:, :)
        integer :: n, i
        n = ubound(a, 1)
        do i = 1, n
            print *, a(i, :)
        end do
    end subroutine
end module

program ex_pascal
    use pascal
    implicit none
    integer :: n
    integer, allocatable :: a(:, :)
    print *, "Size?"
    read *, n
    print *, "Lower Pascal Matrix"
    a = pascal_lower(n)
    call print_matrix(a)
    print *, "Upper Pascal Matrix"
    a = pascal_upper(n)
    call print_matrix(a)
    print *, "Symmetric Pascal Matrix"
    a = pascal_symmetric(n)
    call print_matrix(a)
end program