# Function works one time, and not another...

• 10-05-2002
Captain Penguin
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
• 10-05-2002
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
• 10-05-2002
Captain Penguin
Quote:

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

:eek:

Ah thats nice!

Why does 1/2 = 0, btw?

Thanks!
• 10-05-2002
Cshot
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);