Thread: Matrix rotation by circulating

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    3

    Matrix rotation by circulating

    i need to write a code that solve this issue...
    if the number of circulating was 4 then....
    if the matrix first was :
    1 2 3 4
    1 2 3 4
    1 2 3 4
    1 2 3 4
    Then after 4 rotation to the right the matrix will be:
    2 1 1 1
    3 2 3 1
    4 2 3 2
    4 4 4 3
    the input could be any number of circulating...


    thank Tomas

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So break it down into steps
    - shift top row right one place
    - shift right column down one place
    and so on.

    When you've got a function to do the whole matrix for one step, then call it N times.

    Take a look at the forum rules. We don't help people who just post "I want to do...." without showing any attempt at solving the problem (such as ideas or code).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    3
    i have been trying all day!!!

    actually i made it but i need to do it in a different way... u see..
    Code:
    	
    if(MatWid < MatHei)				{
    					Layers = MatWid / 2;
    				}
    				else
    					Layers = MatHei / 2;
    
    
                    /* According to the number of circulations required */
                    for (circ = 0 ; circ < numOfCir ; circ++)
                    {
                        /* According to the number of layers */
                        for (layer = 0 ; layer < Layers ; layer++)
                        {
                            int tmp1, tmp2;
                            /* Save upper right corner */
                            tmp1 = Matrix[layer][MatWid-layer-1];
                            /* Upper bound */
                            for (j = MatWid-layer-2 ; j >= layer ; j--)
                            {
                                Matrix[layer][j+1] = Matrix[layer][j];
                            }
    
    
                            /* Save bottom right corner */
                            tmp2 = Matrix[MatHei-layer-1][MatWid-layer-1];
                            /* Right bound */
                            for (i = MatHei-layer-2 ; i >= layer ; i--)
                            {
                                if (i != layer)
                                    Matrix[i+1][MatWid-layer-1] = Matrix[i][MatWid-layer-1];
                                else
                                    Matrix[i+1][MatWid-layer-1] = tmp1;
                            }
    
    
                            /* Save bottom left corner */
                            tmp1 = Matrix[MatHei-layer-1][layer];
                            /* Bottom bound */
                            for (j = layer ; j < MatWid-layer-1 ; j++)
                            {
                                if (j != MatWid-layer-2)
                                    Matrix[MatHei-layer-1][j] = Matrix[MatHei-layer-1][j+1];
                                else
                                    Matrix[MatHei-layer-1][j] = tmp2;
                            }
    
    
                            /* Left bound */
                            for (i=layer; i<MatHei-layer-1; i++)
                            {
                                if (i != MatHei-layer-2)
                                    Matrix[i][layer] = Matrix[i+1][layer];
                                else
                                    Matrix[i][layer] = tmp1;
                            }
                        }
                    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > actually i made it but i need to do it in a different way... u see..
    a) does what you have work?
    b) what "different" way?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    3
    Quote Originally Posted by Salem View Post
    > actually i made it but i need to do it in a different way... u see..
    a) does what you have work?
    b) what "different" way?
    a) yes this is what i have done!
    b) i need to do it in a deifferent way, like, doin every row and col and and middle row and col once and do it n times.. like u said...
    but still i dont know how to do that because if im trying to work on the top row, and try to save a temp value like:
    (0,0)(0,1)(0,2)(0,3)
    i = 0
    for(i = 0 ; i < MatWid ; i++)
    temp = (0,1)
    (0,0) = (0,1)

    what next??? or othe option cause it doesnt work for me...?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Tom Tomas Zahov View Post
    a) yes this is what i have done!
    b) i need to do it in a deifferent way, like, doin every row and col and and middle row and col once and do it n times.. like u said...
    but still i dont know how to do that because if im trying to work on the top row, and try to save a temp value like:
    (0,0)(0,1)(0,2)(0,3)
    i = 0
    for(i = 0 ; i < MatWid ; i++)
    temp = (0,1)
    (0,0) = (0,1)

    what next??? or othe option cause it doesnt work for me...?
    Work it out by hand - forget the computer for now! Do it on the kitchen table, with 4 coins or marbles or whatever.

    How do you swap them? Do it a few times, and before too long, you'll begin to see the pattern you use to make those swaps.
    Code:
    //begin swap 
    temp = array[0][0];        //save one of the values to be moved
    array[0][0]= array[0][1];  //swap the 2nd value into place
    array[0][1] = temp;       //place the first value into it's place
    //swap complete. Change the second dimension index to variables in the above code,
    /and you're on your way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 08:16 AM
  2. Rotation matrix
    By Drogin in forum Game Programming
    Replies: 6
    Last Post: 08-02-2008, 03:39 PM
  3. Matrix Rotation
    By disruptivetech in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2008, 01:11 PM
  4. Setting a rotation matrix to have a new Up
    By skorman00 in forum Game Programming
    Replies: 0
    Last Post: 05-23-2006, 02:31 PM
  5. rotation matrix
    By SAMSAM in forum Game Programming
    Replies: 4
    Last Post: 03-02-2003, 01:54 PM