Determinant of a matrix, pointers, recursive functions

Hi to all!!! My name's Daniele and I'm new in this blog and a C++ beginner.
In this period I'm trying to improve in pointers and recursive functions and
for this reason I wrote two codes for find the determinant of a square matrix.
But these codes do not work.
Can someone help me to understand where I'm wrong please?
Thank you very much to all!!!

/*DET_MAT*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int DET;

int fun_det (int **a, int input_row, int input_column)
{
if (input_row >= 3 && input_column >= 3) 
{
int **temp;
temp = new int*[input_row-1];
for(int i=0; i<input_row; i++)
{
temp[i] = new int[input_column-1];
}
int coeff = 0;
int BR = 0; //blocked row
int BC = 0; //blocked column
int row_index = 0;
int column_index = 0;
for (BC = 0; BC != input_column; BC++)
{
row_index = 0;
for (int i=0; i<input_row; i++)
{
column_index = 0;
for (int j=0; j<input_column; j++)
{
if (i==BR && j==BC)
{
coeff = a[i][j];
double exp = i+j;
coeff = pow(-1,exp)*coeff;
row_index--;
}
if (i!=BR && j!=BC)
{
temp[row_index][column_index] = a[i][j];
}
column_index++;  					                      
}
row_index++;	 
}
DET = DET + coeff*(fun_det (temp, (input_row-1),(input_column-1)));		                      
}
}      
if (input_row == 2 && input_column == 2)
{
return ((a[0][0]*a[1][1])-(a[0][1]*a[1][0]));
}
return DET;
}

int main()
{
int row = 0;
int column = 0;
int DETERMINANT = 0;
cout << "Insert number of rows:  ";
cin >> row;
cout << endl << endl;
cout << "Insert number of columns:  ";
cin >> column;
cout << endl << endl;
int **A;
A = new int*[row];
for(int i=0; i<row; i++)
{
A[i] = new int[column];
}
for (int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
cout << "Insert the matrix element A[" << i << "][" << j << "] =  ";
cin >> A[i][j];
}
}
DETERMINANT = fun_det (A, row, column);
cout << DETERMINANT << endl;
		  
system ("PAUSE");
return EXIT_SUCCESS;
}



/*DET_MAT_2*/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

int DET;
int fun_det (int **a, int input_row, int input_column)
{
int **temp;
temp = new int*[input_row-1];
for(int i=0; i<input_row; i++)
{
temp[i] = new int[input_column-1];
}
int coeff = 0;
int BR = 0; //blocked row
int BC = 0; //blocked column
int row_index = 0;
int column_index = 0;
for (BC = 0; BC != input_column; BC++)
{
for (int i=0; i<input_row; i++)
{
for (int j=0; j<input_column; j++)
{
if (i==BR && j==BC)
{
coeff = a[i][j];
double exp = i+j;
coeff = pow(-1,exp)*coeff;
row_index--;
}
if (i!=BR && j!=BC)
{
temp[row_index][column_index] = a[i][j];	
column_index++;
}
}
column_index = 0;	 
row_index++;	 
}
row_index = 0;
if (input_row > 3 && input_column > 3) 
{
DET = DET + coeff*fun_det (temp, (input_row-1),(input_column-1));
}
else
{
return coeff*(temp[0][0]*temp[1][1]-temp[0][1]*temp[1][0]);
}
}
}

int main()
{
DET = 0;
int row = 0;
int column = 0;
int DETERMINANT = 0;
cout << "Insert number of rows:  ";
cin >> row;
cout << endl << endl;
cout << "Insert number of columns:  ";
cin >> column;
cout << endl << endl;
int **A;
A = new int*[row];
for(int i=0; i<row; i++)
{
A[i] = new int[column];
}
for (int i=0; i<row; i++)
{
for(int j=0; j<column; j++)
{
cout << "Insert the matrix element A[" << i << "][" << j << "] =  ";
cin >> A[i][j];
}
}
DETERMINANT = fun_det (A, row, column);
cout << DETERMINANT << endl;
system ("PAUSE");
return EXIT_SUCCESS;
}
Last edited on
Please repost this and use the code tags (under Format): Your code is too hard to read.
I'm so sorry koothkeeper!!! I apologize but I have become familiar with the rules of the blog.
Is better if I write in this way or I have to modify the position of the braces? Thanks a lot for yor help!

Last edited on
What you have posted is much easier to read; however, it is not very good form. How to place your curly braces can become a "holy war" among developers. Find a style you like and stick to it!

My personal style is to indent all curly braces one tab space for functions. Any loops, ifs, etc also get their curly braces indented one more tab space. In every case, all of the code within the curly braces are indented to the same level.

Most programmers do not do it my way and that is fine. Just find a way and use it.
Is this code right?
1
2
3
4
5
6
7
8
9
10
11
12
                             for (int j=0; j<input_column; j++) {
                                if (i==BR && j==BC)     {
                                    coeff = a[i][j];
                                    double exp = i+j;
                                    coeff = pow(-1,exp)*coeff;
                                    row_index--;
                                }
                                if (i!=BR && j!=BC)     {
                                    temp[row_index][column_index] = a[i][j];
                                }
                                column_index++;
                            }

What if neither if condition is true? This can happen if i==BR and j!= BC (for example).
Ok I try to replace with an if else condition. Thank you!! But is better build the temp matrix and call the recursive function inside the if condition (first case) or is better build the temp matrix outside the if condition and then make an if-else condition to control the matrix dimension and then call the function?
dhayden I saw better the code, I believe that when happen the situation where

i==BR and j!= BC

the code don't have to do anything because only when

i!=BR && j!=BC

the elements must be inserted in the submatrix temp. I think that when i==BR, those elements should not be considered. Or am I wrong?
Sorry, I don't remember the algorithm for determinant well enough to know the answer.
Don't worry don't worry no problem!!! But can someone help me to solve this doubt please?
I want to write this algorithm for the determinant, but for skills that involves.
DET should not be a global. It should be a local variable initialized to zero.

You aren't deleting the temp matrix so it leaks a lot of memory.

Indent your code to match the block structure. It's really hard to see what's going on right now.

Referring to your original post, line 29 should be coeff = a[0][j];

You don't need row_index at all. You can do the two loops like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
                    for (int i=1; i<input_row; i++)
                        {
                            column_index = 0;
                            for (int j=0; j<input_column; j++) {
                                if (j==BC)      {
                                    coeff = a[0][j];
                                    double exp = i+j;
                                    coeff = pow(-1,exp)*coeff;
                                } else {
                                    temp[i-1][column_index] = a[i][j];
                                    column_index++;
                                }
                            }
                        }

pow() is a very expensive way to change sign. I'd do use a variable that alternates between -1 and 1.

Hi to all!!! dhayden thank you so much for your advice!! I did put the DET variable inside the function and I corrected the last if-else control for the recursive call of the function. Here the entire code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <cstdlib>
#include <cmath>

using namespace std;

double funz_det (int **a, int dyn_dim) // dyn_dim: the dynamic dimension created for sub-matrix
              {
               double DET = 0;
	       /*Create dynamically the new sub-matrix*/
	      	int **temp;
	      	temp = new int*[dyn_dim-1];
	      	for(int i=0; i<dyn_dim; i++)
	       	     {
	                temp[i] = new int[dyn_dim-1];
	             }
	       	//////////////////////////////////////
	           		      		
	      	int coeff = 0;
	       	int BR = 0;
                int BC = 0;
	       	int row_index = 0;
                int column_index = 0;
                for (BC = 0; BC != dyn_dim; BC++)
                      {
	              	for (int i=0; i<dyn_dim; i++)
	                     {
	               	      	for (int j=0; j<dyn_dim; j++)
	               	              {
	              	              	if (i==BR && j==BC)
	               	      	            {
	              	      	       	      coeff = a[i][j];
	                       	       	      int esp = i+j;
	        	       	      	      coeff = pow(-1,esp)*coeff;
	        	       	       	      row_index--;
	                       	      	    }
	                       	      	if (i!=BR && j!=BC)
	             	              	    {
	                       	              temp[row_index][column_index] = a[i][j];	
                               	      	      column_index++;
	                                    }
	            		      }
	               	      	column_index = 0;	 
                                row_index++;	 
	                     }
                        row_index = 0;
	         	if (dyn_dim > 3) 
                            {
	         	      DET = DET + coeff*funz_det (temp, (dyn_dim-1));
                            }
	                else
                            {
                              DET = DET + coeff*(temp[0][0]*temp[1][1]-temp[0][1]*temp[1][0]); 
                            }
                      }
                return DET;
              }

int main()
         {
          int dimension = 0;
                    
          cout << "Please insert the matrix dimension:  ";
          cin >> dimension;
                    		  
          cout << endl << endl;
                          		  
          int **A;
          A = new int*[dimension];
          for(int d=0; d<dimension; d++)
               {
                 A[d] = new int[dimension];
               }
            		  
          for (int i=0; i<dimension; i++)
                {
		  for(int j=0; j<dimension; j++)
		       {
		         cout << "Please insert the element A[" << i << "][" << j << "] =  ";
		       	 cin >> A[i][j];
		       }
                }
		  
          cout << endl << endl;
	  	  	   
	  for (int display_row=0; display_row<dimension; display_row++)
		{
		  for(int display_column=0; display_column<dimension; display_column++)
		       {
		       	 cout << A[display_row][display_column] << "  ";
		       }
                  cout << endl << endl;
                } 
		  
          cout << endl << endl;
          cout << "Determinant Value = " << funz_det(A,dimension)  <<  endl;
		  
          system ("PAUSE");
          return EXIT_SUCCESS;
         }

Now this code seems to do its job but sometimes crash (for example with an 5x5 identity matrix and I don't know why).
A question for the statement
return DET
In another code that wrote for test myself with recursive function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdlib>

using namespace std;

int funz_fatt(int n)
                    {
                      int fact=0;
                      if (n>1)
                          {
                            fact = n*funz_fatt(n-1);
                          }
                      //return fact;  
                    }

int main()
              { 
                int factorial = 0;
                int integer = 0;
                cout << "Insert the integer for the factorial computing: ";
                cin >> integer;
                cout << endl;
                factorial = funz_fatt(integer);
                cout << factorial << endl << endl;
                
                system ("PAUSE");
                return EXIT_SUCCESS;
              }

if I remove the comment to the statement return fact, the entire code returns 0 as result...in the code for the determinant of a matrix happen instead the opposite thing.
Is there something that I wrong? Or is there (probably) something of the return statement that I don't know?
(PS: I did't modify the statement removing row_index because now for me it's easier manage the code adding some variables without manage external inces. The same thing for pow())
Thank you a lot for all
Last edited on
The return statement at like 13 is correct. The problem is line 8. If n==1, the if statement at line 9 is false and the function returns 0. If n==2, it returns 2*funz_fatt(1), but funz_fatt(1) return 0, so funz_fatt(2) returns 2*0 = 0.
Topic archived. No new replies allowed.