Monday 21 January 2013

Matrix Rotation

// Program to rotate the nxn matrix to the right by 90 degree..
#include<stdio.h>
#define max 100

/*Function to store  the matrix*/
void read_matrix(int a[][max] , int order)
{
    int i , j , n = order-1;
    for (i=0;i<=n;i++)
        for (j=0;j<=n;j++)
           scanf("%d",&a[i][j]);
}


/*Function to rotate a matrix to the right by 90 degress*/
void rotate_right( int a[][max] , int b[][max] , int order)
{
    int i , j , n = order -1;
    for (i=0;i<=n;i++)
        for (j=0;j<=n;j++)   
             b[j][n-i]=a[i][j];
}

/*Function to print the matrix*/
void print_matrix(int a[][max] , int order)
{
    int i  , j , n =  order-1;
    for (i=0;i<=n;i++)
    {
        for (j=0;j<=n;j++)
          printf("%d\t",a[i][j]);
       
        printf("\n");
    }   
}

/*main function*/
int main()
{
    int  i , j , n ;
    int a[max][max];
    int b[max][max];
   
    printf("enter the order of matrix\n");
    scanf ("%d",&n);

    printf ("enter the nxn matrix\n");
    read_matrix (a , n);
    rotate_right( a , b , n);

    printf ("before rotation: \n");
    print_matrix( a , n);
    printf ("\n");

    printf ("after rotation: \n");
    print_matrix(b , n);
    printf("\n");
   
    return 0;
}

No comments:

Post a Comment