d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Rotate A Matrix (array Of Arrays) In C
Add Reply New Topic New Poll
Member
Posts: 3,861
Joined: Aug 18 2004
Gold: 4,680.00
Apr 29 2013 11:40pm
I have been staring at this for a bit and can't come up with a good way to program "rotate a 4x4 matrix 90 degrees to the right". The array I'm using is 4x4 but I feel like there is a scale able solution that I am missing. Thanks ahead of time.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 30 2013 12:13am
what have you come up with so far?

i have a working solution but i want to see what your logic process is so i can try to guide you through it so you understand it.
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Apr 30 2013 01:40am
can't you just rewrite it with column 1 as row 1, but in reverse order, rinse repeat?
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 30 2013 02:00am
Quote (Azrad @ Apr 30 2013 03:40am)
can't  you just rewrite it with column 1 as row 1, but in reverse order, rinse repeat?


pretty much.

you take 2 arrays of the same size lets say

1 2 3 4
4 5 6 7
8 9 10 11

in the new array you would take array[0][0] and place it to newarray[3][0]
then you would increment your collum and set array[0][1] to newarray[3][1]

or something similiar to that. to tired to explain it thoroughly
Member
Posts: 3,861
Joined: Aug 18 2004
Gold: 4,680.00
Apr 30 2013 10:01am
Quote (AbDuCt @ Apr 30 2013 03:00am)
pretty much.

you take 2 arrays of the same size lets say

1 2  3  4
4 5  6  7
8 9 10 11

in the new array you would take array[0][0] and place it to newarray[3][0]
then you would increment your collum and set array[0][1] to newarray[3][1]

or something similiar to that. to tired to explain it thoroughly


This is right. I was also too tired last night (and frustrated!) to give it a valid explanation. I'll give you what I have so far, but being stumped it is not working. I think I could come up with a "working" but rather bulky solution. I have googled and found examples where they do it with 2 for loops but I can't seem to replicate it properly.

Here is my code: (I drew a picture in there to try to help me figure it out. The "DIMENSION" is a global constant which would be 4 in this case though the array is indexed 0-3. Another note is that my current work is based off of an example that doesn't appear correct at all "DIMENSION - col - 1" to generate the new row value.
Code
* Scrambles the grid by rotating it 90 degrees clockwise, whereby
* grid[0][0] rotates to grid[0][DIMENSION - 1].
*
*ex.
*           1   2   3   4           1   2   3   4
*
*      1:  1,1 1,2 1,3 1,4         4,1 3,1 2,1 1,1
*      2:  2,1 2,2 2,3 2,4         4,2 3,2 2,2 1,2
*      3:  3,1 3,2 3,3 3,4         4,3 3,3 2,3 1,3
*      4:  4,1 4,2 4,3 4,4         4,4 3,4 2,4 1,4
*                                      column -> row
                                       row -> (DIMENSION - column - 1)? [ 1,1 ] -> [ 4, 1]
                                       
*
*/
void scramble(void)
{
   char tempGrid[DIMENSION][DIMENSION];
   
   for(int row = 0; row < DIMENSION; ++row)
   {
       for(int col = 0; col < DIMENSION; ++col)
       {
           
           tempGrid[row][col] = grid[DIMENSION - col - 1][row];
           
       }
   }
   
   grid = tempGrid;
   
}



Here is where I was looking for examples previously:
http://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array
http://stackoverflow.com/questions/2893101/how-to-rotate-a-n-x-n-matrix-by-90-degrees

The second one takes a bit of a different approach. I saw one example where they said each "layer" further away from the center would need some different math and I think that is the case, I just have to find the relation and have failed thus far. I am about to take another look at it today and will update when I figure it out, so till then help!
Member
Posts: 3,861
Joined: Aug 18 2004
Gold: 4,680.00
Apr 30 2013 11:34am
LOOOOL Figured it out :P. It was very close to what I posted but it is actually DIMENSION-1-row not col. IDK what I was smokin yesterday I think my brain was just fried. To be fair, I still had to use 2 sheets of paper today to draw pretty pictures. Christ sometimes I think I have issues.

Code
void scramble(void)
{
   char tempGrid[DIMENSION][DIMENSION];
   
   //transpose onto tempGrid
   for(int row = 0; row < DIMENSION; ++row)
   {
       for(int col = 0; col < DIMENSION; ++col)
       {
           
           tempGrid[col][(DIMENSION-1-row)] = grid[row][col];
           
       }
   }
   
   //Copy the tempgrid onto main grid
   for(int row = 0; row < DIMENSION; ++row)
   {
       for(int col = 0; col < DIMENSION; ++col)
       {
           
           grid[row][col] = tempGrid[row][col];
           
       }
   }
   
}


This post was edited by Snickerfritzer on Apr 30 2013 11:35am
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 30 2013 12:12pm
Quote (Snickerfritzer @ Apr 30 2013 01:34pm)
LOOOOL Figured it out :P. It was very close to what I posted but it is actually DIMENSION-1-row not col. IDK what I was smokin yesterday I think my brain was just fried. To be fair, I still had to use 2 sheets of paper today to draw pretty pictures. Christ sometimes I think I have issues.


grats. sometimes you just need to rest and come back to it from a clear mind.

also if you know how it should be working in your head you could of went to a debugger and went through your code line by line and watch your array change each step. you would of most likely realized right away that something was/where it was wrong.

This post was edited by AbDuCt on Apr 30 2013 12:12pm
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Apr 30 2013 11:42pm
Now you're thinking with portals.
Member
Posts: 15,338
Joined: Sep 17 2011
Gold: 34.88
Jul 9 2013 02:30am
or you could go really simple and make a batch file out of this


@echo off
color 0a
:top
echo %random% %random% %random% %random% %random% %random% %random% %random% %random% %random% %random%
goto top
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 9 2013 10:55am
Quote (frankbrown1193 @ Jul 9 2013 04:30am)
or you could go really simple and make a batch file out of this


@echo off
color 0a
:top
echo %random% %random% %random% %random% %random% %random% %random% %random% %random% %random% %random%
goto top


thank you for posting in an old thread which you clearly cannot comprehend. this benefited everyone here. go learn to program and try again in another old thread.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll