Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C program to calculate the determinant of a NxN matrix

Tags:

arrays

c

I'm trying to write a program that would calculate the determinant for me, and this is what I've done so far. But it's not working it just prints 6356918 for every matrix I throw at it. I've even compared my code to some other codes on the internet but that didn't work.

And I don't know anything about pointers so I cannot use them. I tried debugging which I don't know much about it either, but there seems to be something wrong with the first 'if' in the second function and the last part of the code which calculates the determinant. I'm coding in code::blocks.

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

main()
{
    int A[100][100];
    int i,j,k,n,res;
    printf("Enter the order of the matrix: \n");
    scanf("%d",&n);
    printf("\nEnter the elements of the matrix one by one: \n");
    for(i = 0 ; i < n ; i++)
    {
        for(j = 0 ; j < n ; j++)
        {
            scanf("%d",&A[i][j]);
        }
    }
    for(i = 0 ; i < n ; i++)
    {
        for(j = 0 ; j < n ; j++)
        {
            printf("%5d",A[i][j]);
        }
        printf("\n");
    }
    res = det(A,n);
    printf("%d",res);
}
int det(int A[100][100], int n)
{
    int Minor[100][100];
    int i,j,k,c1,c2;
    int determinant;
    int c[100];
    int O=1;

    if(n == 2)
    {
        determinant = 0;
        determinant = A[0][0]*A[1][1]-A[0][1]*A[1][0];
        return determinant;
    }
    else
    {
        for(i = 0 ; i < n ; i++)
        {
            c1 = 0, c2 = 0;
            for(j = 0 ; j < n ; j++)
            {
                for(k = 0 ; k < n ; k++)
                {
                    if(j != 0 && k != i)
                    {
                        Minor[c1][c2] = A[j][k];
                        c2++;
                        if(c2>n-2)
                        {
                            c1++;
                            c2=0;
                        }
                    }
                }
            }
            determinant = determinant + O*(A[0][i]*det(Minor,n-1));
            O=-1*O;
        }
    }
    return determinant;
}
like image 646
Edward Avatar asked Oct 29 '22 14:10

Edward


2 Answers

In function det() you have initialised determinant only when it was not necessary

determinant = 0;
determinant = A[0][0]*A[1][1]-A[0][1]*A[1][0];

but when it was needed

determinant = determinant + O*(A[0][i]*det(Minor,n-1));

there was no previous initialisation. So move

determinant = 0;

to above if(n == 2) near the start of the function.

like image 168
Weather Vane Avatar answered Nov 15 '22 07:11

Weather Vane


I made a recursive but efficient and easy algorithm to calculate the determinant of a NxN matrix:

int determinantOrderN(int **m, int n)
{
    int i,j,k, factor=1, det=0; int **newm;
    if(m==NULL) return -1;
    if (n==1) return **m; //when matrix is a number, determinant is the number
    for(i=0; i<n; i++) 
    {
        if(NULL == (newm = malloc((n-1) * sizeof(int *)))) return -1;
        for(j=0; j<n-1; j++) if (NULL == (newm[j] = malloc((n-1) * sizeof(int)))) return -1;
        for(j=1; j<n; j++) 
        {
            //skip first row as we dont need it for the adjunt matrixes
            for (k=0; k<n; k++)
            {
                if(k==i) continue; //skip same column
                nuevam[j-1][k<i?k:(k-1)]=m[j][k]; //asign the number to new matrix
            }
        }
        det+= factor*m[0][i]*determinantOrderN(newm, n-1); //recursivity, determinant of the adjunt matrix
        factor*=-1;
        for(j=0;j<orden-1;j++) free(newm[j]);
        free(newm);
    }
    return det;
}

Use would be something like:

int **matrix, i;
matrix = malloc(3*sizeof(int *));
for(i=0; i<3;i++) matrix[i]=malloc(3*sizeof(int));
//asign all the numbers
printf("%d\n", determinantOrderN(matrix, 3));
like image 25
S_Rollan Avatar answered Nov 15 '22 06:11

S_Rollan