468,512 Members | 1,423 Online

# C++ Rotating a square matrix 90 degrees 542 512MB
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.
Mar 6 '08 #1
2 13193 sanctus
84 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
Mar 6 '08 #2
whodgson
542 512MB
yes - thanks, think i`m seeing a ray of light!
Mar 8 '08 #3

### Post your reply

Sign in to post your reply or Sign up for a free account.

### Similar topics

 4 posts views Thread by Roozbeh GHolizadeh | last post: by 8 posts views Thread by lovecreatesbeauty | last post: by 2 posts views Thread by leelaramtenneti | last post: by 1 post views Thread by Carl1 | last post: by 1 post views Thread by Johnny Jörgensen | last post: by 6 posts views Thread by lovecreatesbea... | last post: by 4 posts views Thread by krishnai888 | last post: by 3 posts views Thread by crazygrey | last post: by 4 posts views Thread by inferi9 | last post: by reply views Thread by ravipankaj | last post: by reply views Thread by NPC403 | last post: by reply views Thread by slotstar | last post: by reply views Thread by captainhaddock | last post: by reply views Thread by sakshijain10388 | last post: by 1 post views Thread by sonnet | last post: by reply views Thread by jimatqsi | last post: by 1 post views Thread by hwsilver | last post: by reply views Thread by yuyenews | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.