@
mancool1112
Why not? Why did you bother posting if you aren't going to help? OP's whole program is shorter than some of the usual code snippets.
@
miladam
First problem: your indentation is wildly off, and I'm surprised you haven't confused yourself with it yet. Whenever you type a {, you should immediately put another } underneath it, and add code in between.
void myfoo()_ | void myfoo() | void myfoo() | void myfoo() | void myfoo() | void myfoo()
| {_ | { | { | { | {
| | }_ | _ | int x = 12;_ | int x = 12;
| | | } | } | _
| | | | | }
|
Second problem: you are lost with the 2D array... The problem is that you are required to handle a 2D array of any
input size, and this is a horrible problem for most newbies. (And currently a common one on the forum here.)
Since you must use arrays, do it like this -- first, a function:
1 2 3 4 5
|
// Index an element in a square matrix
unsigned index( unsigned size, unsigned row, unsigned col )
{
return (row * n) + col;
}
|
Now create your matrix:
1 2 3 4 5
|
double* m = NULL; // This will be the square matrix
unsigned n; // This is the size of the matrix, n*n
cin >> n; // Get the matrix size from the user, and
m = new double[n*n]; // create the matrix
|
Index its elements using the function.
|
m[ index( n, r, c ) ] = 0; // m[ r ][ c ] = 0
|
Don't forget to free the data when you are done with it (before your program ends):
1 2
|
delete [] m;
m = NULL;
|
Third problem: your code is scattered. For example, you try to initialize your matrix in more than one place in the code. Try to keep it to one spot. For example, your main function might look like this:
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
|
double* create_matrix( unsigned n )
{
return new double[n*n];
}
double* destroy_matrix( double* m )
{
delete [] m;
return NULL;
}
void get_matrix_from_user( double* m, unsigned n )
{
...
}
double determinant( double* m, unsigned n )
{
...
}
int main()
{
unsigned n = 0;
double* m = NULL;
cin >> n;
m = create_matrix( n );
get_matrix_from_user( m, n );
cout << determinant( m, n ) << endl;
m = destroy_matrix( m );
}
|
Fourth problem: You correctly calculate the determinant of a 2x2 matrix, but you goof up on a NxN, because you are not summing terms. Your
alamat() function is a little too complex also...
Fifth problem: the
mat() function needs a little rethinking -- though you are close on the right track. Don't farm it out like that though. To create a new matrix from the appropriate pieces of the parent matrix, write yourself a function to do it:
1 2 3 4 5 6 7 8
|
double* create_sub_matrix( double* m, unsigned n, unsigned i )
{
double* result = create_matrix( n - 1 );
// Copy every element except those in row 0 and column i to the result
// This will be a little tricky -- make sure you use two indices -- one into
// matrix 'm' and one into matrix 'result'.
return result;
}
|
Once you've created the sub-matrix, call the determinant function again, sum the answer into the running result, and delete_matrix() the sub-matrix, and on to the next loop until you have finished. This is a recursive function.
If you want to avoid all the sub-matrices it will take you a little more effort. The first option is to write a recursive routine that tracks the sub-matrices and the indices into them. The other option is to check out something like the
Cholesky method:
http://en.wikipedia.org/wiki/Cholesky_decomposition#Matrix_inversion
(I'm sorry, I'm not sure why CS professors like to assign this particular problem to beginning programmers -- it is difficult enough to scare anyone away from programming.)
Good luck!