Tuesday 27 October 2015

Rotate Matrix 90° Clockwise

Write a program to declare a square matrix A[ ][ ] of order MxM where ‘M’ is the number of rows and the number of columns, such that M must be greater than 2 and less than 10. Accept the value of M as user input. Display an appropriate message for an invalid input. Allow the user to input integers into this matrix. Perform the following tasks:
(a) Display the original matrix.
(b) Rotate the matrix 90° clockwise as shown below:
\textrm{Original Matrix}\begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix}\textrm{Rotated Matrix}\begin{bmatrix} 7 & 4 & 1\\ 8 & 5 & 2\\ 9 & 6 & 2\end{bmatrix}
(c) Find the sum of the elements of the four corners of the matrix.
Test your program for the following data and some random data:
Example 1
INPUT :
M = 3
\begin{matrix} 3 & 4 & 9 \\ 2 & 5 & 8 \\ 1 & 6 & 7 \end{matrix}

OUTPUT :
ORIGINAL MATRIX
\begin{matrix} 3 & 4 & 9 \\ 2 & 5 & 8 \\ 1 & 6 & 7 \end{matrix}
MATRIX AFTER ROTATION
\begin{matrix} 1 & 2 & 3 \\ 6 & 5 & 4 \\ 7 & 8 & 9 \end{matrix}
Sum of the corner elements = 20
Example 2
INPUT :
M = 4
\begin{matrix} 1 & 2 & 4 & 9 \\ 2 & 5 & 8 & 3 \\ 1 & 6 & 7 & 4 \\ 3 & 7 & 6 & 5 \end{matrix}

OUTPUT :
ORIGINAL MATRIX
\begin{matrix} 1 & 2 & 4 & 9 \\ 2 & 5 & 8 & 3 \\ 1 & 6 & 7 & 4 \\ 3 & 7 & 6 & 5 \end{matrix}
MATRIX AFTER ROTATION
\begin{matrix} 3 & 1 & 2 & 1 \\ 7 & 6 & 5 & 2 \\ 6 & 7 & 8 & 4 \\ 5 & 4 & 3 & 9 \end{matrix}
Sum of the corner elements = 18
Example 3
INPUT :M = 14
OUTPUT :SIZE OUT OF RANGE
Example 4
INPUT :M = 112
N = 130
OUTPUT :INVALID INPUT

import java.util.*;
class Q2_ISC2015
{
    public static void main(String args[])throws Exception
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the size of the matrix : ");
        int m=sc.nextInt();
         
        if(m<3 || m>9)
            System.out.println("Size Out Of Range");
        else
        {
            int A[][]=new int[m][m];
 
            /* Inputting the matrix */
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<m;j++)
                {
                    System.out.print("Enter an element : ");
                    A[i][j]=sc.nextInt();
                }
            }
             
            /* Printing the original matrix */
            System.out.println("*************************");
            System.out.println("The Original Matrix is : ");
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<m;j++)
                {
                    System.out.print(A[i][j]+"\t");
                }
                System.out.println();
            }
            System.out.println("*************************");
             
            /*Rotation of matrix begins here */
            System.out.println("Matrix After Rotation is : ");
            for(int i=0;i<m;i++)
            {
                for(int j=m-1;j>=0;j--)
                {
                    System.out.print(A[j][i]+"\t");
                }
                System.out.println();
            }
            System.out.println("*************************");
 
            int sum = A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1]; // Finding sum of corner elements
            System.out.println("Sum of the corner elements = "+sum);
        }
    }
}



Alternate Way (Creating a new Matrix for storing rotated matrix):

If you want, you can also save the rotated matrix in a separate array and then print it.

import java.util.*;
class Q2_ISC2015
{
    public static void main(String args[])throws Exception
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the size of the matrix : ");
        int m=sc.nextInt();
         
        if(m<3 || m>9)
            System.out.println("Size Out Of Range");
        else
        {
            int A[][]=new int[m][m];
 
            /* Inputting the matrix */
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<m;j++)
                {
                    System.out.print("Enter an element : ");
                    A[i][j]=sc.nextInt();
                }
            }
             
            /* Printing the original matrix */
            System.out.println("*************************");
            System.out.println("The Original Matrix is : ");
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<m;j++)
                {
                    System.out.print(A[i][j]+"\t");
                }
                System.out.println();
            }
            System.out.println("*************************");
             
            int B[][]=new int[m][m];
            int x;
             
             /*Rotation of matrix begins here */
            for(int i=0;i<m;i++)
            {
                x = m-1;
                for(int j=0;j<m;j++)
                {
                    B[i][j]=A[x][i];
                    x--;
                }
            }
             
            /* Printing the rotated matrix */
            System.out.println("Matrix After Rotation is : ");
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<m;j++)
                {
                    System.out.print(B[i][j]+"\t");
                }
                System.out.println();
            }
            System.out.println("*************************");
 
            int sum = A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1]; // Finding sum of corner elements
            System.out.println("Sum of the corner elements = "+sum);
        }
    }
}

Output:

Enter the size of the matrix : 4
Enter an element : 1
Enter an element : 2
Enter an element : 4
Enter an element : 9
Enter an element : 2
Enter an element : 5
Enter an element : 8
Enter an element : 3
Enter an element : 1
Enter an element : 6
Enter an element : 7
Enter an element : 4
Enter an element : 3
Enter an element : 7
Enter an element : 6
Enter an element : 5
*************************
The Original Matrix is :
1 2 4 9
2 5 8 3
1 6 7 4
3 7 6 5
*************************
Matrix After Rotation is :
3 1 2 1
7 6 5 2
6 7 8 4
5 4 3 9
*************************
Sum of the corner elements = 18

No comments:

Post a Comment