find the determinant of the matrix using C

Jan 19th, 2015
HelloWorld
Category:
Computer Science
Price: $10 USD

Question description

Can anyone please debug it. It's supposed to find the determinant of the matrix and the problem is in the second function. I'm a beginner and it's due soon. I have spent 12+ hours straight on just debugging. TBH I just want someone to give me the fixed code and give a quick and simple how. Whoever can do that will be rewarded $10. The answers are supposed to be "The determinant of the matrix in matrix2.txt is 6080 The determinant of the matrix in matrix1.txt is -413 " heres the assignment lab1(5).pdf . assume that the header is fine as well as all the other parts cause i tested them. #include "lab1.h"

void findDeterminant(char filename[STRINGSIZE])

{

        int det = 0;

        int i, j;

        int matrix[MATSIZE][MATSIZE];

        FILE *fp3=fopen(filename, "r");

        for (i = 0; i<MATSIZE; i++)

                {

                for (j = 0; j < MATSIZE; j++)

                {

                fscanf(fp3,"%d",&matrix[i][j]);

                }

        }

        fclose(fp3);

        det = determinant (MATSIZE, matrix);

        printf("The determinant of the matrix in %s is %d\n", filename, det);

}

int determinant(int n, int matrix[n][n])

{

        int det = 0;

        int power, i, j, l;

        int newMAT[n-1][n-1];

        if (n == 2)

        {

                det = (matrix[0][0]* matrix [1][1]) - (matrix[1][0]*matrix[0][1]);

                printf("the determinant of the 2x2 is %d\n", det);

                return det;

        }

        else if (n == 1){

                det = matrix [0][0];

                return  det;

        }

        else {

                for(l=0;l<n;l++)

                {

                        int h = 0,k = 0;

                        for(i=1;i<n;i++)

                        {

                                for(j=0;j<n;j++)

                                {

                                        if(j==l)

                                          continue;

                                        newMAT[h][k] = matrix[i+1][j];

                                        k++;

                                        if(k == n-1)

                                                                                                                                                                 

                                        if(k == n-1)

                                        {

                                                h++;

                                                k=0;

                                        }

                                }

                        }

if (l%2 == 0)

{

det += matrix[0][l] * determinant ((n-1), newMAT);

return det;

}

else

{

det -= matrix[0][l] * determinant ((n-1), newMAT);

return det;

}

                }

}

}


Tutor Answer

(Top Tutor) Daniel C.
(997)
School: UCLA
PREMIUM TUTOR

Studypool has helped 1,244,100 students

8 Reviews


Summary
Quality
Communication
On Time
Value
Five Star Tutor
Dec 10th, 2016
" Outstanding Job!!!! "
kpcutie
Nov 27th, 2016
" Excellent job "
Joemoe
Nov 18th, 2016
" <3 it, thanks for saving me time. "
Hemapathy
Nov 13th, 2016
" all I can say is wow very fast work, great work thanks "
pmallory
Nov 4th, 2016
" Totally impressed with results!! :-) "
kevin12622
Oct 22nd, 2016
" Goes above and beyond expectations ! "
kiln82
Oct 13th, 2016
" awesome work thanks "
likeplum4
Sep 29th, 2016
" Excellent work as usual "
Ask your homework questions. Receive quality answers!

Type your question here (or upload an image)

1827 tutors are online

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors