# Thread: calculation always gives 0

1. ## calculation always gives 0

I have written a program to calculate the determinant of a 3x3 matrix. It all works and the values are read from a file correctly, but when i try and calculate something it always give 0. E.g. it prints a=1, b=2 a*b=0.

I dont want to post my actual program as it's for an assignment and im worried ill get in trouble! but does anyone have any ideas what could be causing it?

2. any ideas what could be causing it?
There's a bug in your code. Since I can't actually see your code, that's about all the help I can give.

You don't need to post your entire assignment, just the part that's giving you trouble.

3. I think the problem is somewhere with this external function:

Code:
```float MinDet(float matrix[3][3])
{
float  Min_Det ;
int i, j;

if (i==0 && j==0)
{
Min_Det = (matrix[1][1]) * (matrix[2][2]) - (matrix[2][1]) * (matrix[1][2]);
}
else if (i==0 && j==1)

/* etc */```
the program can print matrix[1][1] and matrix[2][2] correctly but when they're multiplied it equals 0.

4. j and i aren't initialized. But maybe you aren't showing the whole code.
Nothing wrong with the multiplication.

5. Do you have

return Min_Det;

at the end of that function?

6. nonoob - Sorry if im being stupid here, but does that mean when it reads the values from the input file? That works fine.

and yep, there's return(Min_Det) at the end of the external file.

If it's any help this is what comes out
Code:
```|1.0000 0.0000 0.0000 |
|0.0000 2.0000 0.0000 |
|0.0000 0.0000 1.0000 |

matrix[1][1] = 2.0000
matrix[2][2] = 1.0000
matrix[1][1]*matrix[2][2] is 0.000000

Min_Det = -NaN
Determinant = -254304177925759642649400163973279514624.000000```

7. i and j being uninitialized is a problem because that means that their values are undefined. What is likely happening is that i and j never hit any of the cases in your if else tree, therefore you never assign to Det_Min, and as a result Det_Min is also undefined. When printf has to print undefined floats it likes to use (type)0.

Or something else could happen since it's undefined behavior. See the link in my sig.

8. ooo, that may be it.

I now have
Code:
```if (i==0 && j==0)
{
m= (matrix[1][1]) * (matrix[2][2]);
Min_Det = (matrix[1][1]) * (matrix[2][2]) - (matrix[2][1]) * (matrix[1][2]);
}
else if (i==0 && j==1)
{
Min_Det = (matrix[1][0] * matrix[2][2]) - (matrix[2][0] * matrix[1][2]);

}
else if (i==0 && j==2)
{
Min_Det = (matrix[1][0] * matrix[2][1]) - (matrix[2][0] * matrix[1][1]);
}
else
{
Min_Det =999;
}```
and get
Code:
```|1.0000 0.0000 0.0000 |
|0.0000 2.0000 0.0000 |
|0.0000 0.0000 1.0000 |
Min_Det = -NaN

matrix[1][1]*matrix[2][2] is 0.000000
Determinant = 999.000000```

9. in the main function i have

Code:
```for(i=0, j=0; j<=2; j++)
{
/*calculate determinant */
}```
so it must be one of those first 3 if's?

10. Yeah it looks like that is the problem then. You need to fix i and j... and that really depends on what i and j really mean. If you don't have a meaningful value for i and j at the beginning of the determinant function, you should pass those values in.

11. Code:
`			fscanf(input, "%f \n", &matrix[i][j]);`

12. How is that even compiling for you? i and j are not even declared in the MinDet function.

13. sorry, i changed it when i was fiddling around! the edited one above compile!

14. Code:
`printf("matrix[1][1]*matrix[2][2] is %f \n", m);`
So, where do you calculate m?

15. in the external function at the bottom. i dont need that bit in my program, it was just to try and see where it was going wrong