Task: Pascal matrix generation

C: #include <stdio.h>
#include <stdlib.h>

void pascal_low(int **mat, int n) {
    int i, j;

    for (i = 0; i < n; ++i)
        for (j = 0; j < n; ++j)
            if (i < j)
                mat[i][j] = 0;
            else if (i == j || j == 0)
                mat[i][j] = 1;
            else
                mat[i][j] = mat[i - 1][j - 1] + mat[i - 1][j];
}

void pascal_upp(int **mat, int n) {
    int i, j;

    for (i = 0; i < n; ++i)
        for (j = 0; j < n; ++j)
            if (i > j)
                mat[i][j] = 0;
            else if (i == j || i == 0)
                mat[i][j] = 1;
            else
                mat[i][j] = mat[i - 1][j - 1] + mat[i][j - 1];
}

void pascal_sym(int **mat, int n) {
    int i, j;

    for (i = 0; i < n; ++i)
        for (j = 0; j < n; ++j)
            if (i == 0 || j == 0)
                mat[i][j] = 1;
            else
                mat[i][j] = mat[i - 1][j] + mat[i][j - 1];
}

int main(int argc, char * argv[]) {
    int **mat;
    int i, j, n;

    
    n = 5;

    
    mat = calloc(n, sizeof(int *));
    for (i = 0; i < n; ++i)
        mat[i] = calloc(n, sizeof(int));

    
    printf("=== Pascal upper matrix ===\n");
    pascal_upp(mat, n);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            printf("%4d%c", mat[i][j], j < n - 1 ? ' ' : '\n');

    printf("=== Pascal lower matrix ===\n");
    pascal_low(mat, n);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            printf("%4d%c", mat[i][j], j < n - 1 ? ' ' : '\n');

    printf("=== Pascal symmetric matrix ===\n");
    pascal_sym(mat, n);
    for (i = 0; i < n; i++)
        for (j = 0; j < n; j++)
            printf("%4d%c", mat[i][j], j < n - 1 ? ' ' : '\n');

    return 0;
}


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));
}