Thread: calculation always gives 0

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    12

    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. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    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.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    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. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    j and i aren't initialized. But maybe you aren't showing the whole code.
    Nothing wrong with the multiplication.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Do you have

    return Min_Det;

    at the end of that function?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    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. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    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. #9
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    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. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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. #11
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    Code:
    			fscanf(input, "%f \n", &matrix[i][j]);
    Last edited by Emma K; 11-24-2010 at 06:01 PM.

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    How is that even compiling for you? i and j are not even declared in the MinDet function.
    bit∙hub [bit-huhb] n. A source and destination for information.

  13. #13
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    sorry, i changed it when i was fiddling around! the edited one above compile!

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    printf("matrix[1][1]*matrix[2][2] is %f \n", m);
    So, where do you calculate m?

  15. #15
    Registered User
    Join Date
    Nov 2010
    Posts
    12
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 07:24 AM
  2. Problem with modulo calculation
    By manutdfan in forum C Programming
    Replies: 6
    Last Post: 01-12-2009, 03:37 PM
  3. C# calculation problem
    By Diablo02 in forum C# Programming
    Replies: 13
    Last Post: 10-25-2007, 08:42 PM
  4. Help with calculation and loop
    By Betty in forum C Programming
    Replies: 7
    Last Post: 04-10-2006, 05:37 AM
  5. help with pay calculation program
    By shugstone in forum C++ Programming
    Replies: 2
    Last Post: 02-17-2003, 09:38 AM