Recursive algorithm to find the determinant of a matrix

This is a discussion on Recursive algorithm to find the determinant of a matrix within the C Programming forums, part of the General Programming Boards category; Below, i have shown the code to calculate the determinant value of a matrix.mat is the pointer to the single ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Recursive algorithm to find the determinant of a matrix

    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;
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Split from dead thread Determinant Calculation Program.... HELP PLZ
    Please see the forum rules.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    > free(temp_mat+iter);
    You're not freeing what you malloc'ed.
    You can't free part of a block of memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    2

    Recursive algorithm to find the determinant of a matrix

    Actually thats what i felt and thats what i had put in the first place. but i was getting the same error message. so i thought, since i had allocated memory in one shot, it was enough if i free it once. but for both i got the same error message.
    am sorry i dun have that much patience to go through the rules. i cant read something which is really long. I am actually facing that mental problem since my childhood. I cant read text books so i listen to what my teacher is sayin. excuse me for not going through it.i am not doing it intentionally!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. matrix class
    By shuo in forum C++ Programming
    Replies: 2
    Last Post: 07-13-2007, 01:03 AM
  2. Matrix Reloaded Questions (SPOILERS_
    By Xei in forum A Brief History of Cprogramming.com
    Replies: 73
    Last Post: 10-19-2003, 02:21 PM
  3. Recursive Matrix Chain
    By Stewdent in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2002, 05:55 PM
  4. Won't Return pointer, i can't find why...
    By ss3x in forum C++ Programming
    Replies: 2
    Last Post: 02-28-2002, 07:50 PM
  5. recursive matrix traversal
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2001, 10:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21