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!!!
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!
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.
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?
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.
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:
#include <iostream>
#include <cstdlib>
#include <cmath>
usingnamespace 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 = newint*[dyn_dim-1];
for(int i=0; i<dyn_dim; i++)
{
temp[i] = newint[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 = newint*[dimension];
for(int d=0; d<dimension; d++)
{
A[d] = newint[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:
#include <iostream>
#include <cstdlib>
usingnamespace 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
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.