Formula Logic Help

• 03-11-2011, 03:35 AM
sehudson
Formula Logic Help
Hello,

I was given the following formula to calculate a determinant:

Code:

```  public double calculateDeterminant() {         double det = 0;         System.out.println("RowCount: " + RowCount);         if (RowCount == 1) {             det = CalculateBaseCase("1");             return det;         }         //base case         if (RowCount == 2) {             det = CalculateBaseCase("2");             return det;         }         for (int i = 0; i < RowCount; i++) {             det = Math.pow(-1, i) * getValAt(i, 0) * minor(i,0).calculateDeterminant();         }         return det;     }```
I have some println statements in place, and it seems like my i for the 'minor(i,0)' method stays at 0. Anybody able to tell me where the formula is wrong?
• 03-11-2011, 04:02 AM
Junky
Code:

```int val; for(int i = 1; i <= 10; i++) {     val = i; } System.out.println(val);```
The output is 10. The above code can be simplified to
Code:

```int val = 10; System.out.println(val);```
Do you understand what I'm getting at?
• 03-11-2011, 04:24 AM
sehudson
partially..since in your example the println is outside the loop, the only thing that gets printed is the last loop value, which is 10.
• 03-11-2011, 04:25 AM
sehudson
not exactly sure.
• 03-11-2011, 05:17 AM
Junky
The print statement is irrelevant. It was included in case the code was compiled and run to see the output.

The point I was trying to make is that your loop is pointless only the last value is retained. When i is 1, perform calculation, assign to det. Then i is 2, perform calculation, assign to det, throw away first result. Then i is 3, perform calculation, assign to det, throw away second result, etc. Your loop can be replaced with:
Code:

`det = Math.pow(-1, RowCount - 1) * getValAt(RowCount - 1, 0) * minor(RowCount - 1,0).calculateDeterminant();`