Below, i have shown the code to calculate the determinant value of a matrix.mat is the pointer to the single dimensional array everywhere and dimension is the dimension of a matrix. if 3x3 then dimension=3.
The code shows no compilation errors. But when it is run, i get an error saying some block of memory ( i dun remember which block) cannot be written.
what could possibly be the problem?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int det_recursive(int* mat,int dimension){
short int iter;
int * temp_mat;
int det_mat=0;
for (iter=0;iter<dimension;iter++){
temp_mat=(int *) malloc(pow(dimension-1,2)*sizeof(int));
form_new_mat(mat,iter,temp_mat,dimension);
det_mat=det_mat+((*(mat+iter))*det_recursive(temp_mat,dimension-1)*pow(-1,iter));
}
free(temp_mat+iter);
return det_mat;
}
int form_new_mat(int * mat,int iter,int * temp_mat,int dimension){
int temp_iter=dimension;
int new_iter=0;
while(temp_iter<pow(dimension,2)){
if (iter== temp_iter % dimension) temp_iter++;
else{
*(temp_mat+new_iter)=*(mat+temp_iter);
new_iter++;
}
}
return 0;
}
int main(){
int dimension=3;
int mat[9]={1,0,0,0,1,0,0,0,1},i,j;
for (i=0;i<0;i++) printf("\nhello world");
//printf("%d",det_recursive(mat,dimension));
system("pause");
return 0;
}