# another dumb question - loop not working?

• 10-06-2002
Captain Penguin
another dumb question - loop not working?
The following function finds the inverse of a matrix (actually its not complete at all):

Code:

```void Matrix::Inverse() {   float det;   det = this->Determinant();   if(det == 0)   {     cout << "The matrix does not have an inverse.";     return;   }     Matrix *identity = new Matrix(a,b); // creates nxn matrix   identity->Identity(); // make it an identity matrix   Matrix *temp = new Matrix(a,b);   temp = this; // so we don't change the original matrix   // forward elimination steps   for(x = 0; x < a; x++) // a is the number of rows   {       y = 0;       temp->elimination(((float)(temp->Array[x+1][y]/temp->Array[y][y])), (y+1), (x+2));       temp->showMatrix();   }    }```

elimination is defined as:

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;   } }```

Anyways, if I have the matrix A:

2 1 3
1 2 3
4 2 1

if I do A.Inverse() it SHOULD do this:

2 1 3
0 1.5 1.5
4 2 1

2 1 3
0 1.5 1.5
0 0 -5

But it only does ONE step - it should display a second matrix, even if it isn't right, shouldn't it?

here's rest of the code for reference:

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

Thanks!
• 10-06-2002
The Dog
>> temp = this; // so we don't change the original matrix

You didn't overload the '=' operator to handle object assignment for you. Doing that will allow you to change the original matrix!
• 10-06-2002
Captain Penguin
Quote:

Originally posted by The Dog
>> temp = this; // so we don't change the original matrix

You didn't overload the '=' operator to handle object assignment for you. Doing that will allow you to change the original matrix!

Hmmm I was unsure of that one myself. I did it and then had it print out temp and it was the same as this, so I thought it worked.

Are you saying that by doing that, altering one alters the other?

And how does that affect the for loop?

Thanks
• 10-06-2002
The Dog
Quote:

Originally posted by Captain Penguin
Are you saying that by doing that, altering one alters the other?
Yes!

>> And how does that affect the for loop?
I haven't had a look at it, I just wanted to point out the previous error. I'll see if I can figure it out.
• 10-06-2002
The Dog
Shame on you Capt. Penguin. You are using class members as loop variables! Don't do that.

In all the methods for the class, Declare temporary variables for loop variables.

I found one error (Actually all over). In your showMatrix() method, you use x and y as loop variables. Declare local variables and use them instead.
Code:

```void Matrix::showMatrix() {     int tmpX, tmpY;         for(tmpX = 0; tmpX < a; tmpX++)     {           cout << "| ";           for(tmpY = 0; tmpY < b; tmpY++)           {                 cout << Array[tmpX][tmpY] << "\t";           }           cout << " |\n";     }     cout << "\n"; }```
...and the newline at the end helps with the printing of the matrix.
• 10-06-2002
Captain Penguin
Quote:

Originally posted by The Dog
Shame on you Capt. Penguin. You are using class members as loop variables! Don't do that.

In all the methods for the class, Declare temporary variables for loop variables.

I found one error (Actually all over). In your showMatrix() method, you use x and y as loop variables. Declare local variables and use them instead.
Code:

```void Matrix::showMatrix() {     int tmpX, tmpY;         for(tmpX = 0; tmpX < a; tmpX++)     {           cout << "| ";           for(tmpY = 0; tmpY < b; tmpY++)           {                 cout << Array[tmpX][tmpY] << "\t";           }           cout << " |\n";     }     cout << "\n"; }```
...and the newline at the end helps with the printing of the matrix.

Well I guess I have to plead ignorance on this one! I didn't realize that was a bad idea - I figured that since I'd use them constantly, why not make them member variables instead of redeclaring them over and over again?

I can see why NOT to do it I suppose, but does it really affect my program? I don't see how it would.

Anyways I'll do that up when I get a chance, as well as overloading the = operator.

I still don't know why my for loop in Inverse doesn't work! Does it have to do with the fact that I declared x and y as member variables or is that just another issue entirely?

Thanks for the tips!
• 10-06-2002
Captain Penguin
:eek:

OK doing what you suggested magically made the function work!

Great!

But, care to explain? :)
• 10-06-2002
Mister C
Loop control variables are usually declared locally in methods or functions.
• 10-06-2002
Captain Penguin
Quote:

Originally posted by Mister C
Loop control variables are usually declared locally in methods or functions.
Yes, I know that now, but how could it have affected my program? I suppose it doesn't really matter , since its just a loop control, but it would be nice to know.