C++ Rotating a square matrix 90 degrees

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whodgson
    Contributor
    • Jan 2007
    • 542

    C++ Rotating a square matrix 90 degrees

    This is a learning problem which is not part of an assignment or homework so there is no urgency.
    i`ve spent hours writing and ammending this rotate() function to no avail. So now i`ve obtained the correct function from the author which works. I can still not understand how the logic was arrived at. I have traced the function call and of course it works. I have looked for patterns to see how they are reflected in the function again to no avail. i cannot see the logic of how the rows are being transformed into columns or the logic of how the individual elements are being repositioned in the temp array
    The matrix is presented in main() as a one dimensional array which is printed out as a square matrix of 3 rows and 3 columns using the print () function which i understand.
    Here are the 2 functions with my 2 commented questions
    [CODE]void rotate (Matrix m)
    {Matrix temp;//1.How is this declaration legal without the [ ]?
    for(int i=0;i<SIZE;i++)
    for(int j=0;j<SIZE;j++)
    temp[i][j]=m[SIZE-j-1][i];//2.This is the purplexing line, what is its logic?
    for(int i=0;i<SIZE;i++)
    for(int j=0;j<SIZE;j++)
    m[i][j]= temp [i][j];
    }

    void print (Matrix a)
    {
    for(int i=0;i<SIZE;i++)
    {
    for(int j=0;j<SIZE;j++)
    cout<<a[i][j]<<"\t";
    cout<<endl;
    }
    cout<<endl ;}[/CODE ]
    This is the before and after output
    /*Original square matrix:
    11 22 33
    44 55 66
    77 88 99

    Matrix now rotated 90
    77 44 11
    88 55 22
    99 66 33*/

    I would appreciate some enlightenment in due course when time allows.
  • sanctus
    New Member
    • Mar 2007
    • 84

    #2
    Originally posted by whodgson
    This is a learning problem which is not part of an assignment or homework so there is no urgency.
    i`ve spent hours writing and ammending this rotate() function to no avail. So now i`ve obtained the correct function from the author which works. I can still not understand how the logic was arrived at. I have traced the function call and of course it works. I have looked for patterns to see how they are reflected in the function again to no avail. i cannot see the logic of how the rows are being transformed into columns or the logic of how the individual elements are being repositioned in the temp array
    The matrix is presented in main() as a one dimensional array which is printed out as a square matrix of 3 rows and 3 columns using the print () function which i understand.
    Here are the 2 functions with my 2 commented questions
    [CODE]void rotate (Matrix m)
    {Matrix temp;//1.How is this declaration legal without the [ ]?
    for(int i=0;i<SIZE;i++)
    for(int j=0;j<SIZE;j++)
    temp[i][j]=m[SIZE-j-1][i];//2.This is the purplexing line, what is its logic?
    for(int i=0;i<SIZE;i++)
    for(int j=0;j<SIZE;j++)
    m[i][j]= temp [i][j];
    }

    void print (Matrix a)
    {
    for(int i=0;i<SIZE;i++)
    {
    for(int j=0;j<SIZE;j++)
    cout<<a[i][j]<<"\t";
    cout<<endl;
    }
    cout<<endl ;}[/CODE ]
    This is the before and after output
    /*Original square matrix:
    11 22 33
    44 55 66
    77 88 99

    Matrix now rotated 90
    77 44 11
    88 55 22
    99 66 33*/

    I would appreciate some enlightenment in due course when time allows.

    For your first question doesn't he use GSL_matrix? Isn't there the matrix defined this way? Try to search for gsl matrix on the net.

    The second question works fine: just write a_{i,j}=b_{size-j-1,i} and make a few tries for i and j to convince that is right for example consider i=1=j so you see that input element is 55 (=b_{3-1-1,1}) exactly as the output a_{1,1}
    For i=1 j=2 you get a_{1,2} =b_{0,1} and b_{0,1}=44


    Hope this helps a little

    Comment

    • whodgson
      Contributor
      • Jan 2007
      • 542

      #3
      yes - thanks, think i`m seeing a ray of light!

      Comment

      Working...