Sei sulla pagina 1di 6

QUESTION

Write a program to declare a square matrix A[][] of order M*M 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 users to input integers into this matrix. Perform the
following tasks:a) Display the original matrix.
b) Rotate the matrix 90 degree clockwise.
c) Find the sum of the elements of the four corners of the matrix.
Example 1
INPUT :
MATRIX
3 4 9
2 5 8
1 6 7

M=3

OUTPUT: ORIGINAL
3 4 9
2 5 8
1 6 7
MATRIX AFTER ROTATION
1 2 3
6 5 4
7 8 9
Sum of corner

elements=20
Example 2
INPUT :M=4
MATRIX
1 2 4 9
2 5 8 3
1 6 7 4
3 7 6 5

OUTPUT: ORIGINAL
1 2 4 9
2 5 8 3
1 6 7 4
3 7 6 5
MATRIX AFTER ROTATION
3 1 2 1
7 6 5 2
6 7 8 4
5 4 3 9
Sum of corner

elements=18
Example 3
-______________________________________________________-

INPUT : M=14
OUTPUT:SIZE OUT OF RANGE

ALGORITHM
Step-1- Start
Step-2-A class Rotate is created.
Step-3-An instance variable M=0 is created.
Step-4-A main() function is created
Step-5-display M=
Step-6-store M
Step-7-check if (M>10)
Step-8-if true then display OUT OF RANGE
Step-9-if false then {
Step-10-An integer array a[][] of size M*M is created.
Step-11-for(int i=0 to M-1;i++) {
Step-12-for(int j=0 to M-1;j++) {
Step-13-store a[i][j] } }
Step-11-An object ob of class Rotate is created
Step-12-pass a,M to display(a,M) using ob object
Step-13-end of main() function }
Step-14-A function display(int a[][],int M) is created
Step-15-display ORIGINAL MATRIX
Step-16-for(inti=0 to M-1;i++) {
Step-17-for(int j=0 to M-1;j++) {
Step-18-display a[i][j]+" " }
Step-19-change line }
Step-20-display "MATRIX AFTER ROTATION"
Step-21-for(inti=0 to M-1;i++) {
Step-22-for(int j=0 to M-1;j++) {
Step-23-display a[i][j]+" " }
Step-24-change line }
Step-25-store a[0][0]+a[M-1][0]+a[0][M-1]+a[M-1][M-1] in s
Step-26-display "Sum of corner elements="+s
Step-27-End of display() function
Step-28-Stop

-______________________________________________________-

PROGRAM
import java.util.Scanner;
import java.io.*;
class Rotate
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
System.out.print("M=");
int M=obj.nextInt();
if(M>10)
System.out.println("OUT OF RANGE");
else
{
int a[][]=new int[M][M];
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
a[i][j]=obj.nextInt();
}
}
Rotate ob=new Rotate();
ob.display(a,M);
}
}
void display(int a[][],int M)
{
System.out.println("ORIGINAL MATRIX");
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
System.out.print(a[i][j]+" "); }
System.out.println();
}
System.out.println("MATRIX AFTER ROTATION");
for(int i=0;i<M;i++)
{
for(int j=M-1;j>=0;j--)
{
System.out.print(a[j][i]+" ");
}
-______________________________________________________-

System.out.println();
}
int s=a[0][0]+a[M-1][0]+a[0][M-1]+a[M-1][M-1];
System.out.println("Sum of corner elements="+s);
}
}

VARIABLE DESCRIPTION
-______________________________________________________-

Data Type
int
int
int
int
int

Variable name
M
a[][]
i
j
s

Description
To store size of array a[][]
Double Dimension array
Looping Variable
Looping variable
store sum of diagonals

INPUT AND OUTPUT


1.Input/Output
M=3
349
258
167
-______________________________________________________-

ORIGINAL MATRIX
349
258
167
MATRIX AFTER ROTATION
123
654
789
Sum of corner elements=20
2.Input/Output
M=14
INVALID ENTRY
3.Input/Output
M=4
1249
2583
1674
3765
ORIGINAL MATRIX
1249
2583
1674
3765
MATRIX AFTER ROTATION
3121
7652
6784
5439
Sum of corner elements=18

-______________________________________________________-

Potrebbero piacerti anche