Submit your m-file and a diary that shows how you tested the code. Create an m-f
ID: 3811090 • Letter: S
Question
Submit your m-file and a diary that shows how you tested the code. Create an m-file mydet.m to compute the determinant of a square matrix using recursion. The function should take as input a matrix A and return its determinant d. Test your code on the following matrix and use det (.) to check your answer. B = [-7 4 -2 -8 6 8 7 2 3 -1 6 -6 6 0 -7 -6 2 -9 2 0 -9 6 7 5 0] Computing the determinant using recursion: The determinant of an n times n square matrix A can be calculated in the following way: det (A) = a_11C_11 + a_12C_12 + .... + a_1nC_1n where C_1j = (-1)^1+j det(M_1j) and the (n - 1) times (n - 1) submatrix M_1j of A throws out row 1 and column j.Explanation / Answer
#include<conio.h>
#include<stdio.h>
int a[20][20],m;
int determinant(int f[20][20],int a);
int main()
{
int i,j;
printf(" Enter order of matrix : ");
scanf("%d",&m);
printf(" Enter the elements of matrix ");
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
printf("a[%d][%d] = ",i,j);
scanf("%d",&a[i][j]);
}
}
printf(" ------ Matrix A is ------- ");
for(i=1;i<=m;i++)
{
printf(" ");
for(j=1;j<=m;j++)
{
printf(" %d ",a[i][j]);
}
}
printf(" ");
printf(" Determinant of Matrix A is %d .",determinant(a,m));
getch();
}
int determinant(int f[20][20],int x)
{
int pr,c[20],d=0,b[20][20],j,p,q,t;
if(x==2)
{
d=0;
d=(f[1][1]*f[2][2])-(f[1][2]*f[2][1]);
return(d);
}
else
{
for(j=1;j<=x;j++)
{
int r=1,s=1;
for(p=1;p<=x;p++)
{
for(q=1;q<=x;q++)
{
if(p!=1&&q!=j)
{
b[r][s]=f[p][q];
s++;
if(s>x-1)
{
r++;
s=1;
}
}
}
}
for(t=1,pr=1;t<=(1+j);t++)
pr=(-1)*pr;
c[j]=pr*determinant(b,x-1);
}
for(j=1,d=0;j<=x;j++)
{
d=d+(f[1][j]*c[j]);
}
return(d);
}
}