Task: Pascal matrix generation



C#: using System;

public static class PascalMatrixGeneration
{
    public static void Main() {
        Print(GenerateUpper(5));
        Console.WriteLine();
        Print(GenerateLower(5));
        Console.WriteLine();
        Print(GenerateSymmetric(5));
    }

    static int[,] GenerateUpper(int size) {
        int[,] m = new int[size, size];
        for (int c = 0; c < size; c++) m[0, c] = 1;
        for (int r = 1; r < size; r++) {
            for (int c = r; c < size; c++) {
                m[r, c] = m[r-1, c-1] + m[r, c-1];
            }
        }
        return m;
    }

    static int[,] GenerateLower(int size) {
        int[,] m = new int[size, size];
        for (int r = 0; r < size; r++) m[r, 0] = 1;
        for (int c = 1; c < size; c++) {
            for (int r = c; r < size; r++) {
                m[r, c] = m[r-1, c-1] + m[r-1, c];
            }
        }
        return m;
    }

    static int[,] GenerateSymmetric(int size) {
        int[,] m = new int[size, size];
        for (int i = 0; i < size; i++) m[0, i] = m[i, 0] = 1;
        for (int r = 1; r < size; r++) {
            for (int c = 1; c < size; c++) {
                m[r, c] = m[r-1, c] + m[r, c-1];
            }
        }
        return m;
    }

    static void Print(int[,] matrix) {
        string[,] m = ToString(matrix);
        int width = m.Cast<string>().Select(s => s.Length).Max();
        int rows = matrix.GetLength(0), columns = matrix.GetLength(1);
        for (int row = 0; row < rows; row++) {
            Console.WriteLine("|" + string.Join(" ", Range(0, columns).Select(column => m[row, column].PadLeft(width, ' '))) + "|");
        }
    }

    static string[,] ToString(int[,] matrix) {
        int rows = matrix.GetLength(0), columns = matrix.GetLength(1);
        string[,] m = new string[rows, columns];
        for (int r = 0; r < rows; r++) {
            for (int c = 0; c < columns; c++) {
                m[r, c] = matrix[r, c].ToString();
            }
        }
        return m;
    }
    
}









Ada: 
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Float_Text_IO; use Ada.Float_Text_IO;


with Ada.Numerics.Generic_Elementary_Functions;

procedure PascalMatrix is

  type Matrix is array (Positive range <>, Positive range <>) of Natural;

  
  package Math is new Ada.Numerics.Generic_Elementary_Functions(Float_Type => Float);
  use Math;
  
  procedure Print(m: in Matrix) is
    
    w: Float := Log(Float(m'Length(1)**(m'Length(1)/2)), 10.0);
    width: Positive := Natural(Float'Ceiling(w)) + 1;
    begin
      for i in m'First(1)..m'Last(1) loop
        Put("( ");
        for j in m'First(2)..m'Last(2) loop
          Put(m(i,j), width);
        end loop;
        Put(" )"); New_Line(1);
      end loop;
    end Print;
  
  function Upper_Triangular(n: in Positive) return Matrix is
    result: Matrix(1..n, 1..n) := (
                                    1 => ( others => 1 ),
                                    others => ( others => 0 )
                                  );
    begin
      for i in 2..n loop
        result(i,i) := 1;
        for j in i+1..n loop
          result(i,j) := result(i,j-1) + result(i-1,j-1);
        end loop;
      end loop;
      return result;
    end Upper_Triangular;
  
  function Lower_Triangular(n: in Positive) return Matrix is
    result: Matrix(1..n, 1..n) := (
                                    others => ( 1 => 1, others => 0 )
                                  );
    begin
      for i in 2..n loop
        result(i,i) := 1;
        for j in i+1..n loop
          result(j,i) := result(j-1,i) + result(j-1,i-1);
        end loop;
      end loop;
      return result;
    end Lower_Triangular;
  
  function Symmetric(n: in Positive) return Matrix is
    result: Matrix(1..n, 1..n) := (
                                   1 => ( others => 1 ),
                                   others => ( 1 => 1, others => 0 )
                                  );
    begin
      for i in 2..n loop
        for j in 2..n loop
          result(i,j) := result(i,j-1) + result(i-1,j);
        end loop;
      end loop;
      return result;
    end Symmetric;
  
  n: Positive;
  
  begin
    Put("What dimension Pascal matrix would you like? ");
    Get(n);
    Put("Upper triangular:"); New_Line(1);
    Print(Upper_Triangular(n));
    Put("Lower triangular:"); New_Line(1);
    Print(Lower_Triangular(n));
    Put("Symmetric:"); New_Line(1);
    Print(Symmetric(n));
  end PascalMatrix;