calculating n-order determinant

even after rewriting the algorithm from the scratch twice i cant get this write

can someone point out my fallacy?
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
#include<stdio.h>
#include<math.h>
#define xplus  (x= x >= n ? 0 : x+1)
#define yminus (y= x <= 0 ? n : y-1)
#define yplus  (y= y >= n ? 0 : y+1)

int A[5][5],D,n;


int determinant_n(int n,int x,int y,int flag){
	 int i=0;
	 xplus;
	 if(n==1){
		  if(flag%2==0)
			yplus;
		  else
			yminus;
		  D=A[x][y];
        printf("[%d]",D);
		  }
	 else{
	  yplus;
	  if(n%2==0)
			for(D=0;i<n;i++,yplus){
				D+=pow(-1,i)*A[x][y]*determinant_n(n-1,x,y,i);
				printf("*%d\t\tSent(e) (%d,%d)\n",A[x][y],x,y);
			}
	  else{
			for(D=0;i<n;i++,yplus){
				D+=A[x][y]*determinant_n(n-1,x,y,i);
				printf("*%d\t\tSent(o) (%d,%d)\n",A[x][y],x,y);
			 }
			}
	 }
    return D;
}

    





int getdata(int n){
 int x,y;
 for(x=0;x<n;x++)
	  for(y=0;y<n;y++){
		  printf("A[%d][%d] = %d\n",x,y,n*x+y+1);
		  A[x][y]=n*x+y+1;			//scanf("%d",&A[x][y]); 
		  }
 printf("\nthe Square Matrix is:\n");
 for(x=0;x<n;x++){
	  for(y=0;y<n;y++)
		  printf("\t%d",A[x][y]);
	  printf("\n");
 }
 printf("__________________________________\n");
 return 0;
}





int main(){
      printf("Size of Square Matrix = ");
      scanf("%d",&n);
      getdata(n);
		if(n>1)
		  D=determinant_n(n,-1,-1,0);
		else return 0;
      printf("Determinant of the Square Matrix = %d",D);
		scanf("%d",&n);
		return 0;
}

macros are evil. They just make a replacement, so you don't have the compile check as in a function
Is this correct? #define yminus (y= x <= 0 ? n : y-1) Also it is obfuscated, the ?: operator could be easily replaced by modular arithmetic. (what are the limits, 0->n or 0->n-1?)

pow(-1,i) That is a waste of resources. The expected results are 1 if i is even an -1 if odd. ((i%2==0)?1:-1)

I didn't check the algorithm. All I can remember about determinants is that you should not calculate it for matrix bigger than 2x2. (maybe 3x3)
Last edited on
i used macros coz i figured functions wud be too resource intensive for the task

thanks for pointing out
#define yminus (y= x <= 0 ? n : y-1)
and
pow(-1,i)

limit shud be 0->n-1
how do i use modular arithmetic?
I have not really looked into your code but as far as I remember. If a matrix can be reduced to row-echelon form , meaning the determination is non_zero. The determinant would be equal to the product of the diagonal. This seems easier to implement rather than the recursive method. However this may force you to copy the original matrix which may be costly if the matrix itself is huge.
Your macros ussed 0->n
1
2
x = (x+1)%n; //increment
x = (x+n-1)%n; //decrement 

With the method proposed by CppSpartan you already solve the system, so why you will need the determinant?
@CppSpartan: i didn't remember that
i'll rewrite the program using row-echelon form right after i develop an algo to reduce a matrix to that form
thanks 4 the tips

@ne555: thanks again


@ne555: The determinant can be used for many things. For example you could use it calculate the volume of a linear system in 3 space =)
can somebody please tell me how to correct error 1083 in visual studio 2010....after including the header file #include<iostream.h> and compiling it the error is "cannaot find iostream.h...pls help....code is 100% correct..
Try #include<iostream>
Topic archived. No new replies allowed.