I am trying to implement some code to calculate Stirling numbers. The code shown below provides the correct calculation but throws a Segmentation fault: 11 once it is done running. I suspect there is something with the way I am setting up the multidimensional array.


import std.stdio;
import std.datetime ;
import std.conv ;
import std.file;
import std.string;
import std.regex;
import std.bigint ;
import std.range : enumerate;


int stirling1(int n, int k)
{
        auto matrix = new int[][](n+1,k+1) ;
        for(int i = 0; i <= n; i++)
        {
            matrix[i][0] = 0 ;
        }
        for(int i = 0; i <= k; i++)
        {
            matrix[0][k] = 0 ;
        }
        for(int i = 1; i <= n ; i++)
        {
            for(int q = 1; q <= i ; q++)
            {
                if(q == 1 || i == q)
                {
                    matrix[i][q] = 1 ;
                }
                else
                {
matrix[i][q] = q*matrix[i-1][q] + matrix[i-1][q-1] ;
                }
            }
        }
        return(matrix[n][k]) ;
}

void main()
{
    writeln("s(n,k) for s(7,2)") ;
    writeln(stirling1(7,2)) ;
}


Reply via email to