0% found this document useful (0 votes)
4 views7 pages

Rotation of 2d Matrix

The document describes a Java program for rotating a 2D matrix based on user-defined dimensions M and N, which must be between 2 and 10. It includes methods for inputting matrix elements, displaying the original and rotated matrices, and finding the highest element along with its location. The program also handles invalid input by displaying an error message if the dimensions are out of the specified range.

Uploaded by

Sohom Dutta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Rotation of 2d Matrix

The document describes a Java program for rotating a 2D matrix based on user-defined dimensions M and N, which must be between 2 and 10. It includes methods for inputting matrix elements, displaying the original and rotated matrices, and finding the highest element along with its location. The program also handles invalid input by displaying an error message if the dimensions are out of the specified range.

Uploaded by

Sohom Dutta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

ROTATION OF A 2D MATRIX

of columns such that both M and N must be greater than 2 and less than10. Allow the user to input integers into this
matrix. Display appropriate error message for an invalid input.

Perform the following tasks on the matrix.


(a) Display the input matrix
(b) Shift each row one step upwards so the first row will become the last row 2nd row will be the
1st row and so on
(c) Display the rotated matrix along with the highest element and its location in the matrix
Test your program for the following data and some random data:

Example 1
INPUT: M =3
N=4

Enter elements in the matrix:


100 90 87 76
200 500 167 998
77 567 89 254

OUTPUT: FORMED MATRIX AFTER ROTATING:


200 500 167 998
77 567 89 254
100 90 87 76

Highest element: 998 ( Row: 0 and Column: 3 )

Example 2
INPUT: M =4
N=3

Enter elements in the matrix:


54 120 187
78 55 289
134 67 89
63 341 122

OUTPUT: FORMED MATRIX AFTER ROTATING:


78 55 289
134 67 89
63 341 122
54 120 187

Highest element: 341 ( Row: 2 and Column: 1 )

Example 3
INPUT: M = 2
N= 3

OUTPUT: SIZE IS OUT OF RANGE. INVALID ENTRY


SOURCE CODE
CODE (SOFT COPY)

import java.util.Scanner;

class MATRIX_ROTATION
{
int arr[][];
int copy[][];
int row_limit;
int col_limit;

public void input()


{
Scanner sc=new Scanner(System.in);
arr=new int[row_limit][col_limit];

/*Elements are added in the Matrix*/


for(int i=0;i<row_limit;i++)
{
for(int j=0;j<col_limit;j++)
{
System.out.print("\n\tPlease enter the number : ");
arr[i][j]=sc.nextInt();
}}
sc.close();

/*Display the Original Matrix which we have taken from the User*/
System.out.println("\n\tThe Matrix is : ");
for(int i=0;i<row_limit;i++)
{
for(int j=0;j<col_limit;j++)
{
System.out.print("\t"+arr[i][j]);
}
System.out.println();
}}

public void process()


{ int i,j;
copy=new int[row_limit][col_limit];

/*Rotation Process Code*/


for(i=row_limit-1;i>0;i--)
{
for(j=0;j<col_limit;j++)
{
copy[i-1][j]=arr[i][j];
}
for(j=0;j<col_limit;j++)
copy[row_limit-1][j]=arr[0][j];
}
/*Display the Matrix after Rotation*/
System.out.println("\n\tFORMED MATRIX AFTER ROTATING: : ");
for(i=0;i<row_limit;i++)
{
for(j=0;j<col_limit;j++)
{
System.out.print("\t"+copy[i][j]);
}
System.out.println();
}

/*Display the Maximum number from the Rotation Matrix*/


int max=copy[0][0];
int r_index=0,c_index=0;
for(i=0;i<row_limit;i++)
{
for(j=0;j<col_limit;j++)
{
if(max<copy[i][j])
{
max=copy[i][j];
r_index=i;
c_index=j;
}
}
}
System.out.println("\n\tHighest element: "+max+" (Row : "+r_index+", Column : "+c_index+")");
}

public static void main()


{
Scanner sc=new Scanner(System.in);
MATRIX_ROTATION obj=new MATRIX_ROTATION();
System.out.print("\n\tEnter the total number of rows (M) : ");
obj.row_limit=sc.nextInt();
System.out.print("\n\tEnter the total number of column (N) : ");
obj.col_limit=sc.nextInt();
sc.close();
if(obj.row_limit>2 && obj.col_limit<10)
{
obj.input();
obj.process();
}
else
{
System.out.print("\n\tSIZE IS OUT OF RANGE. INVALID ENTRY");
}

}
}
OUTPUT

You might also like