C++ determinant calculator I've wrote a C++ program to calculate the determinant of a matrix:

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

double getDeterminant(double arr[], 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;
    double matrix[dimension][dimension];

    //Now, the user has to enter the matrix line by line, seperated by commas
    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 {
                matrix[i - 1][count] = std::stod(number);
                number = "";
                count++;
            }
        }
    }

    //Conversion to a onedimensional matrix to be able to give it over as a parameter
    double array[dimension * dimension];
    int k = 0;
    for(int i = 0; i &lt; dimension; i++) {
        for(int j = 0; j &lt; dimension; j++) {
            array[k] = matrix[i][j];
            k++;
        }
    }

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

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

double getDeterminant(double array[], int dimension) {

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

    //Conversion back to 2D-array
    double matrix[dimension][dimension];
    int k = 0;
    for(int i = 0; i &lt; dimension; i++) {
        for(int j = 0; j &lt; dimension; j++) {
            matrix[i][j] = array[k];
            k++;
        }
    }

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

        //Submatrix
        double subMatrix[dimension - 1][dimension -1];
        for(int m = 1; m &lt; dimension; m++) {
            int z = 0;
            for(int n = 0; n &lt; dimension; n++) {
                if(n != i) {
                    subMatrix[m-1][z] = matrix[m][n];
                    z++;
                }
            }
        }

        //Conversion of the submatrix to 1D-array
        double array2[(dimension - 1) * (dimension - 1)];
        int k = 0;
        for(int x = 0; x &lt; dimension - 1; x++) {
            for(int y = 0; y &lt; dimension - 1; y++) {
                array2[k] = subMatrix[x][y];
                k++;
            }
        }

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

    return result;
}


I would appreciate any suggestions on improving the code!



You can find the follow-up question here.
double matrix[dimension][dimension];

  
  

double array[dimension * dimension];

  
  

double matrix[dimension][dimension];

  
  

    double subMatrix[dimension - 1][dimension -1];

  
  

    double array2[(dimension - 1) * (dimension - 1)];



None of those are legal C++, because dimension isn't a constant-expression.



We have a signed/unsigned comparison here:


    for(int k = 0; k &lt; str.length(); k++) {



We can easily eliminate the compiler warning by using a more appropriate type:

    for (std::size_t k = 0;  k &lt; str.length();  ++k) {


But a better fix, given we only use k to index str, is to use a range-based loop:

    for (auto const c: str) {




When using operator&gt;&gt; on a stream, we must always check that it succeeded, before we depend on the result:

std::size_t dimension;
std::cin &gt;&gt; dimension;
if (!std::cin) {
    std::cerr &lt;&lt; "Input failed\n";
    return EXIT_FAILURE;   // needs &lt;cstdlib&gt;
}
if (dimension == 0) {
    std::cout &lt;&lt; "1\n";    // empty matrix determinant
}