Function works one time, and not another...

This is a discussion on Function works one time, and not another... within the C++ Programming forums, part of the General Programming Boards category; OK I have a funtion in my matrix program to multiply one row by a multiplie and then subtract that ...

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    244

    Function works one time, and not another...

    OK I have a funtion in my matrix program to multiply one row by a multiplie and then subtract that row from another row. here is the definition:

    Code:
    void Matrix::elimination(float multiple, int row1, int row2)
    {
      // subtracts multiple*row1 from row2
    
      float temp;
      
      for(y = 0; y < b; y++)
      {
        temp = Array[row2-1][y] - (multiple*Array[row1-1][y]);
        Array[row2-1][y] = temp;
      }
    }
    where Array[][] represents the elements of the matrix.

    The problem: I've tried two matrices - in one it works, in the other it doesn't do anything.

    With the matrix:

    1 1 3 -1 0 0
    -1 1 1 1 1 -4
    0 1 2 2 -1 0

    I put in the parameters for elimination: (-1, 1, 2), which subtracts the -1 times the first row from the second row. (adds row 1 to row 2).

    and I get the following matrix as output:

    1 1 3 -1 0 0
    0 2 4 0 1 -4
    0 1 2 2 -1 0

    which is what I wanted to get (to make the first entry of row 2 a zero)

    but if I use this matrix:

    2 1 3
    1 2 3
    4 2 1

    and use elimination(1/2, 1, 2) to subtract 1/2 the first row from the second, it gives the exact same matrix as output!

    it SHOULD give this:

    2 1 3
    0 1.5 1.5
    4 2 1

    What could be causing this?

    here is the rest of the code for reference (case 'a' is where I tested the elimination method)

    http://dydx.no-ip.com:8080/matrix.h
    http://dydx.no-ip.com:8080/matrix.cpp

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    That function works fine. The only problem is in your main

    >> A.elimination(1/2, 1, 2);
    should be
    A.elimination(0.5, 1, 2);
    because 1/2 = 0
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    Originally posted by Cshot
    That function works fine. The only problem is in your main

    >> A.elimination(1/2, 1, 2);
    should be
    A.elimination(0.5, 1, 2);
    because 1/2 = 0


    Ah thats nice!

    Why does 1/2 = 0, btw?

    Thanks!

  4. #4
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Because they're integer types. 1/2 = 0, in integer operations.
    If you cast it to a float then you would be fine.

    A.elimination((float)1/2, 1, 2);
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21