#include <stdio.h>
#include <stdlib.h>
int det(int);
int minor(int);
int main()
{
int mat[10][10],dim,a,b;
printf("Please enter an integer for matrix dimensions(1-10):");
scanf("%d",&dim);
for (a=0;a<=dim-1;a++)
{
for (b=0;b<=dim-1;b++)
{
mat[a][b]=rand()%10+1;
printf(" %d",mat[a][b]);
}
printf("\n\n");
}
printf("Determinant for the matrix is: %d",det(mat));
return 0;
}
int det(int mat[10][10])
{
int a,b,temp,length;
length=0;
for (a=0;a<10;a++)
{
if mat[a][0]!=0 length++;
}
if (mat[1][1]==0)
{
temp=mat[0][0];
return temp;
}
a=rand()%10;
b=rand()%10;
return ((-1)^(a+b))*mat[a][b]*det(minor(mat,a,b));
}
int minor(int mat[10][10], int x, int y)
{
int a,k,i,length,minor[10][10];
length=0;
for (a=0;a<10;a++)
{
if mat[a][0]!=0 length++;
}
for (i=0;i<length;i++)
{
for (k=0;k<length;k++)
{
if (i<x && k<y)
minor[i][k]=mat[i][k];
else if (i>=x && k<y)
minor[i][k]=mat[i+1][k];
else if (i<x && k>=y)
minor[i][k]=mat[i][k+1];
else if (i>=x && k>=y)
minor[i][k]=mat[i+1][k+1];
}
}
return minor;
}
This is a homework about calculating determinant of a matrix recursively, i don't think it is finished now so i know that there are some errors. But my problem is i cannot use mat[][] matrix while i try to calculate the length of the matrix. I know that i have a problem with passing the matrix to det but i can't figure it out.
Thanks for any help