C++ determinant calculator - follow-up After following some suggestions you can find here, I'd like to show you the result:

#include &lt;iostream&gt; 
#include &lt;string&gt;
#include &lt;vector&gt;

double getDeterminant(std::vector&lt;std::vector&lt;double&gt;&gt; vect, int dimension);

int main() { 


    //First, the user has to enter the dimension of the matrix
    int dimension;
    std::cout &lt;&lt; "Please enter dimension of Matrix: ";
    std::cin &gt;&gt; dimension;
    std::cout &lt;&lt; std::endl;

    if(dimension &lt; 0) {
        std::cout &lt;&lt; "ERROR: Dimension cannot be &lt; 0." &lt;&lt; std::endl;
        return -1;
    }

    //Now, the user has to enter the matrix line by line, seperated by commas
    std::vector&lt;std::vector&lt;double&gt;&gt; vect(dimension, std::vector&lt;double&gt; (dimension));
    std::string str;
    for(int i = 1; i &lt;= dimension; i++) {
        std::cout &lt;&lt; "Enter line " &lt;&lt; i &lt;&lt; " only seperated by commas: ";
        std::cin &gt;&gt; str;
        std::cout &lt;&lt; std::endl;
        str = str + ',';
        std::string number;
        int count = 0;
        for(int k = 0; k &lt; str.length(); k++) {
            if(str[k] != ',') {
                number = number + str[k];
            }
            else if(count &lt; dimension) {
                if(number.find_first_not_of("0123456789.-") != std::string::npos) {
                    std::cout &lt;&lt; "ERROR: Not only numbers entered." &lt;&lt; std::endl;
                    return -1;
                }
                vect[i - 1][count] = std::stod(number);
                number = "";
                count++;
            }
            else {
                std::cout &lt;&lt; "ERROR: Too many numbers entered." &lt;&lt; std::endl;
                return -1;
            }
        }
    }

    //Output
    for(int i = 0; i &lt; dimension; i++) {
        for(int j = 0; j &lt; dimension; j++) {
            std::cout &lt;&lt; vect[i][j] &lt;&lt; " ";
        }
        std::cout &lt;&lt; std::endl;
    }

    std::cout &lt;&lt; "Determinant of the matrix is : " &lt;&lt; getDeterminant(vect, dimension) &lt;&lt; std::endl; 
    return 0;
} 

double getDeterminant(std::vector&lt;std::vector&lt;double&gt;&gt; vect, int dimension) {

    if(dimension == 0) {
        return 0;
    }

    if(dimension == 1) {
        return vect[0][0];
    }

    //Formula for 2x2-matrix
    if(dimension == 2) {
        return vect[0][0] * vect[1][1] - vect[0][1] * vect[1][0];
    }

    double result = 0;
    int sign = 1;
    for(int i = 0; i &lt; dimension; i++) {

        //Submatrix
        std::vector&lt;std::vector&lt;double&gt;&gt; subVect(dimension - 1, std::vector&lt;double&gt; (dimension - 1));
        for(int m = 1; m &lt; dimension; m++) {
            int z = 0;
            for(int n = 0; n &lt; dimension; n++) {
                if(n != i) {
                    subVect[m-1][z] = vect[m][n];
                    z++;
                }
            }
        }

        //recursive call
        result = result + sign * vect[0][i] * getDeterminant(subVect, dimension - 1);
        sign = -sign;
    }

    return result;
}


Do you have any more suggestions to improve the code?



You can find the follow-up question here.
if(dimension == 0) {
    return 0;
}


Mathematically, that is not correct. The determinant of an empty (i.e. zero-dimensional) matrix is one, see for example What is the determinant of []? on Mathematics Stack Exchange.



With respect to efficiency: Your program computes the determinant recursively using the Laplace formula, which requires \$ O(n!) \$ arithmetic operations, and the creation of many temporary “submatrices”.

A better method (at least for larger matrices) is Gaussian elimination which requires \$ O(n^3) \$ arithmetic operations, and can operate on a single copy of the original matrix.