Thread: another dumb question - loop not working?

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

    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!

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> 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!

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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.

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    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.

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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!

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


    OK doing what you suggested magically made the function work!

    Great!

    But, care to explain?

  8. #8
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Loop control variables are usually declared locally in methods or functions.
    Mr. C: Author and Instructor

  9. #9
    Registered User
    Join Date
    Aug 2001
    Posts
    244
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "try again" loop not working
    By scwizzo in forum Game Programming
    Replies: 5
    Last Post: 04-01-2007, 09:56 PM
  2. Help Please, Beginner Question, rewrite loop
    By office888 in forum C Programming
    Replies: 4
    Last Post: 12-11-2006, 10:07 AM
  3. question about for loop
    By panfilero in forum C Programming
    Replies: 3
    Last Post: 09-27-2005, 05:59 AM
  4. Question... do/while loop
    By Akaii in forum C Programming
    Replies: 2
    Last Post: 08-10-2003, 04:52 AM
  5. Question about restarting a loop
    By librab103 in forum C Programming
    Replies: 10
    Last Post: 07-19-2003, 12:31 PM