### calculate determinte

i wrot this program to calculate determinte of matrix(n*n).plz tell me my mistake and how can i fix them?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124`` `````` #include using namespace std; double ** matriskhan(int n) { double * * a; a=new double * [n]; a[0]=new double [n*n]; for(int i=0;i>a[i][j]; } } return a; } double matris2(double a,double b ,double c,double d) { double e; e=a*d-b*c; return e; } int alamat(int i,int j) { int d; d=i+j%2; int m; switch (d) { case(0) : { m=1; break; } case(1): { m=-1; break; } } return m; } double * * mat(int w,int z,int n,double * * a) { double * * m; m=new double * [n-1]; m[0]=new double[(n-1)*(n-1)]; for(int i=0;iz) { m[i][j-1]=a[i][j]; } else if(i>w&&jw&&j>z) { m[i-1][j-1]=a[i][j]; } } } } double det(double * * a,int n) { double sum; if( n==2) { sum=matris2(a[0][0],a[0][1],a[1][0],a[1][1]); return sum; } else { for(int i=0;i>n; a=new double * [n]; a[0]=new double [n*n]; for(int i=0;i
somebody help me, i need to know my mistakes
@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.

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:

 ``12345`` ``````// Index an element in a square matrix unsigned index( unsigned size, unsigned row, unsigned col ) { return (row * n) + col; }``````

 ``12345`` ``````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):

 ``12`` ``````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:

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````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:

 ``12345678`` ``````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!
Topic archived. No new replies allowed.