Sunday 25 June 2017

Rotating a NXN Mratrix

Hello friends !!
Today's code for life is,
Imagine a square pattern divided into smaller squares as shown below. As you can  see seven of the smaller squares are filled in. If this is rotated clockwise by 90 degrees and placed on top of the original pattern then more squares are filled in.

The total number of filled in squares is now 13. Continuing in this way you can again rotate and add the original pattern to the combination giving 19 black squares and one more rotation + addition gives 25 black squares.


You must write a program that will read in a square pattern and output the original number of black squares and the number that result from each rotation.

Input:

-  an integer N on a single line
-  the next N lines consist of the NxN square,
(where 1 represents a black square and 0 represents a white square)

Output:

-      four lines containing the number of black squares before each rotation



CODE:

package sanjay😊;
import java.util.*;
public class matrixrotate {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("Enter the matrix size: ");
        int arr[][]=new int[100][100];          //2d matrix array
        int i,j,k,m,l;
         int old_arr[][]=new int[100][100];     //initial array
          int add_arr[][]=new int[100][100];    // after rotatong to add new array
         int no_rotation=0;
         int rotation;
        int n = input.nextInt();
        System.out.println("Enter the matrix elements as 1 and 0: ");
        for( i=0;i<n;i++)
        {
            for( j=0;j<n;j++)
            {
                arr[i][j]=input.nextInt();
            }
        }
         System.out.println("The Entered matrix is:");
        for(i=0;i<n;i++)
        {
             for( j=0;j<n;j++)
            {
            if(arr[i][j]>0)
            {
                no_rotation++;
            }
            }
             System.out.println("");
        }
        for( k=1;k<5;k++)
        {
            rotation=0;
             System.out.println("Rotation:"+k);
            for ( i = 0; i < n / 2; i++)
            {
            for ( j = i; j < n - i - 1; j++)
            {
            int temp= arr[i][j];
            arr[i][j] = arr[n - j - 1][i];
            arr[n - j - 1][i] = arr[n - i - 1][n - j - 1];
            arr[n - i - 1][n - j - 1] = arr[j][n - i - 1];
            arr[j][n - i - 1] = temp;
            }
            }
            for(m=0;m<n;m++)
            {
                for(l=0;l<n;l++)
                {
                                add_arr[m][l]=old_arr[m][l]+arr[m][l];
                                  if(add_arr[m][l]>0)
                                  {
                                      rotation++;
                                  }
                                  arr[m][l]=add_arr[m][l];
                                  old_arr[m][l]=add_arr[m][l];
                }
             }
            System.out.println("Rotation values grater than 1:"+rotation);
            }  
      }
}

No comments:

Post a Comment

BlockChain Company Infographic

Hello All,      This post talks about the impact of blockchain technology in various standards and industrial uses. Kindly do read to know i...