1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Matrix rotation by circulating

Discussion in 'C' started by tomas7470, May 5, 2012.

  1. tomas7470

    tomas7470 New Member

    Joined:
    May 5, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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. skr1986

    skr1986 New Member

    Joined:
    Aug 2, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Hope this is what u asked...
    Code:
    /*
     *author SHIBIN K.REENY(skr1986.wordpress.com)
     */
    
    #include <stdio.h>
    main()
    {         int a[4][4], i, j, k, e1, e2, e3, e4, rotate;
        for(i = 0; i < 4; i++)for(j = 0; j < 4; j++)
            scanf("%d", &a[i][j]);
                 printf("Enter the rotation number : ");
                 scanf("%d", &rotate);
                 printf("Orginal matrix :-\n");
                 for(i = 0; i < 4; i++){           // displaying the original matrix
                           for(j = 0; j < 4; j++)
                                       printf("%d  ", a[i][j]);
                           printf("\n");
                     }
                 printf("\n");
                 k = 0;
                 while(k < rotate){
                          // saving the edges...   
                          e1 = a[0][0];
                          e2 = a[0][3];
                          e3 = a[3][3];
                          e4 = a[3][0];
                          for(i = 3; i > 0; i--)         // top edge moving by one position
                                 a[0][i] = a[0][i - 1];
                          for(i = 3; i > 1; i--)         // rigth edge moving by one position
                                 a[i][3] = a[i - 1][3];
                          a[1][3] = e2;
                          for(i = 0; i < 2; i++)         // bottom edge moving by one position 
                                 a[3][i] = a[3][i + 1];
                          a[3][2] = e3;  
                          for(i = 0; i < 2; i++)         // left edge moving by one position
                                 a[i][0] = a[i + 1][0];
                          a[2][0] = e4;
                          k++;
                     }
                 printf("NEW Matrix :-\n");
                 for(i = 0; i < 4; i++){            // final output after rotation
                           for(j = 0; j < 4; j++)
                                       printf("%d  ", a[i][j]);
                           printf("\n");
                     }
                 return 1;
        }
     

Share This Page