473,479 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

C++ Rotating a square matrix 90 degrees

542 Contributor
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 13499
sanctus
84 New Member
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 Contributor
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
5442
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
10987
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
13281
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
5633
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
1826
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
7432
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
8065
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
3859
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
3391
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...
0
7027
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6719
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5312
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
166
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.