calculate determinte

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


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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
  #include <iostream>

using namespace std;
double ** matriskhan(int n)
{
    double * * a;
    a=new double * [n];
    a[0]=new double [n*n];
    for(int i=0;i<n;i++)
        {
            a[i]=&a[0][n];
        }

    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>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;i<n-1;i++)
    {
        m[i]=&m[0][n-1];
    }
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            if(i<w&&j<z)
        {
            m[i][j]=a[i][j];
        }
        else if(i<w&&j>z)
        {
            m[i][j-1]=a[i][j];
        }
        else if(i>w&&j<z)
        {
            m[i-1][j]=a[i][j];
        }
        else if(i>w&&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;i++)
    {int nn;
        nn=alamat(0,i);n=n-1;
        return nn*a[0][i]* det(mat(0,i,n,a),n);
    }

    }

}

int main()
{
    int n;
    double k;
    double * * a;
    cin>>n;
    a=new double * [n];
    a[0]=new double [n*n];
    for(int i=0;i<n;i++)
        {
            a[i]=&a[0][n];
        }

   a= matriskhan(n);
    cout<<det(a,n);
    return 0;
}




somebody help me, i need to know my mistakes
No one is going to help you when you post your whole program
@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!
Topic archived. No new replies allowed.