473,378 Members | 1,592 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 13493
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

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

### Similar topics

 4 by: Roozbeh GHolizadeh | last post by: Hi there... i think it is a very common problem but i dont know why i didnt find anything good for me on internet... i want to simply rotate an array with 90,180,270 degrees.... here is how... 8 by: lovecreatesbeauty | last post by: I write a function to rotate a matrix by 90 degrees clockwise, this function works on a matrix of specific size, for example, it rotates a 4*4 matrix of integers in the following code. The function... 2 by: leelaramtenneti | last post by: hello I need a program to find a Inverse of a square complex matrix in java... . The program should calculate the inverse of a matrix in which each element has both real and imaginary parts. Or... 1 by: Carl1 | last post by: I have a question regarding vertical text labels in the Reports area of Access 2000. In the properties area, selecting vertical text to "yes" rotates the text 90 degrees so that the text reads... 1 by: Johnny Jörgensen | last post by: I need to rotate (transform) a two dimensional matrix (or more precisely an array of objects - Dim MyArray(X,X) as object ) 45 degrees counterclockwise. I used to be a wizz at math, but I haven't... 6 by: lovecreatesbea... | last post by: Hello experts, I code an function to rotate a matrix by 90 degrees clockwise. The matrix can be in any size provided its length equals to width. The one minor limitation is that this requires an... 4 by: krishnai888 | last post by: I had already asked this question long back but no one has replied to me..I hope someone replies to me because its very important for me as I am doing my internship. I am currently writing a code... 3 by: crazygrey | last post by: Hello, I'm a newbie to C++ so excuse me if my question was trivial but it is important to me. I'm implementing a simple code to find the forward kinematics of a robot: #include "stdafx.h"... 4 by: inferi9 | last post by: Hi, I am working in a program caals magic square and it must be done only with loops and user definied funcations,I will tell you about my code and where my problem, the main is only calls the other... 1 by: CloudSolutions | last post by: Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now... 0 by: Faith0G | last post by: I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be... 0 by: isladogs | last post by: The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former... 0 by: ryjfgjl | last post by: In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import... 0 by: ryjfgjl | last post by: If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming... 0 by: ryjfgjl | last post by: In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files... 0 by: emmanuelkatto | last post by: Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel 1 by: nemocccc | last post by: hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions? 1 by: Sonnysonu | last post by: This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.