RosettaCodeData/Task/Pascal-matrix-generation/FreeBASIC/pascal-matrix-generation.basic

56 lines
1.4 KiB
Plaintext

sub print_matrix( M() as integer )
'displays a matrix
for row as integer = 0 to ubound(M, 1)
for col as integer = 0 to ubound(M, 2)
print using "#### ";M(row, col);
next col
print
next row
return
end sub
function fact( n as uinteger ) as uinteger
'quick and dirty factorial
if n<2 then return 1 else return n*fact(n-1)
end function
function nCp( n as uinteger, p as uinteger ) as uinteger
'quick and dirty binomial
if p>n then return 0 else return fact(n)/(fact(p)*fact(n-p))
end function
sub make_pascal( M() as integer, typ as const ubyte )
'allocate the matrix first
'typ 0 = jCi, 1=iCj, 2=(j+i)Ci
for i as uinteger = 0 to ubound(M,1)
for j as uinteger = 0 to ubound(M,2)
select case typ
case 0
M(i,j) = nCp(j, i)
case 1
M(i,j) = nCp(i, j)
case 2
M(i,j) = nCp(i + j, j)
case else
M(i, j) = 0
end select
next j
next i
return
end sub
dim as integer M(0 to 4, 0 to 4)
print "Upper triangular"
make_pascal( M(), 0 )
print_matrix( M() )
print "Lower triangular"
make_pascal( M(), 1 )
print_matrix( M() )
print "Symmetric"
make_pascal( M(), 2 )
print_matrix( M() )
print "Technically the matrix needn't be square :)"
dim as integer Q(0 to 4, 0 to 9)
make_pascal( Q(), 2 )
print_matrix( Q() )