Matrix Rotations (+ 90 / -90 Degrees)


import java.util.Scanner;

class Matrix
{
    int a[][] =new int[5][5];
    int b[][] =new int[5][5];
    int i=0,j=0, row,col,temp,count=0;
    Scanner s=new Scanner(System.in);

    void get()
    {
        System.out.println("Enter the no. of rows & columns");
        row=s.nextInt();
        col=s.nextInt();
        System.out.println("Eneter the Matrix values");
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
                a[i][j]=s.nextInt();
        }
    }




   void transpose()
    {      
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
                b[i][j]=a[j][i];
        }       
    }

       void rowrev()
    {
         for(i=0;i<row;i++)
        {
            for(j=0;j<col/2;j++)
            {
                temp=b[i][j];
            b[i][j]=b[i][row-1-j];
            b[i][row-1-j]=temp;
            }
        }
    }



  
 void colrev()
    {
         for(i=0;i<row/2;i++)
        {
            for(j=0;j<col;j++)
            {
                temp=b[i][j];
            b[i][j]=b[col-1-i][j];
            b[col-1-i][j]=temp;
            }
        }
    }

    void display()
    {
        for(i=0;i<row;i++)
        {
            for(j=0;j<col;j++)
                System.out.print(" "+b[i][j]);
            System.out.println();
        }
    }
}

public class MatrixRotation
{

  public static void main(String  SoftwareDevelopmentLab2015[])
  {
      int ch=0;
       Matrix m=new Matrix();
       Scanner s=new Scanner(System.in);
       while(ch<3)
       {
       System.out.println("Matrix Rotation operations");
       System.out.println("**************************");
       System.out.println("1. +90 degree Rotation");
       System.out.println("2. -90 degree Rotation");
       System.out.println("3.Exit");
       System.out.println("Enter your choice");
       ch=s.nextInt();
       switch(ch)
       {
           case 1:
               m.get();
               m.transpose();
               m.rowrev();
               System.out.println(" +90 Degree Rotation");
               m.display();
               break;
           case 2:
               m.get();
               m.transpose();
               m.colrev();
               System.out.println(" -90 Degree Rotation");
               m.display();
               break;
         }
     }
  }
}



OUTPUT:-

Matrix Rotation operations
**********************
1. +90 degree Rotation
2. -90 degree Rotation
3.Exit
Enter your choice          1
Enter the no. of rows & columns
3
3
Enter the Matrix values
1 2 3
4 5 6
7 8 9
 +90 Degree Rotation
 7 4 1
 8 5 2
 9 6 3

Matrix Rotation operations
**********************
1. +90 degree Rotation
2. -90 degree Rotation
3.Exit
Enter your choice          2
Enter the no. of rows & columns
3
3
Enter the Matrix values
1 2 3
4 5 6
7 8 9
 -90 Degree Rotation
 3 6 9
 2 5 8
 1 4 7

Matrix Rotation operations
**************************
1. +90 degree Rotation
2. -90 degree Rotation
3.Exit
Enter your choice          3


No comments:

Post a Comment

Thank you for using this blog.