ISC Computer Science Practical 2015 Question 2 Solved

Question 2:

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 I 0. 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:


Original matrix:

1  2  3
4  5  6
7  8  9

Rotated matrix:

7  4  1
8  5  2
9  6  3

(c) Find the sum of the elements of the four comers of the matrix. Test your program with the sample data and some random data

import java.io.*;
class practical2015q2{
int arr[][];
int arr2[][];
int arr3[][];
int M=0;
int row=0;
int col=0;
int sum = 0;
void main()throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number value of M.");
M = Integer.parseInt(br.readLine());
arr=new int[M][M];
arr2=new int[M][M];
arr3=new int[M][M];
col = M-1;
if(M>2&&M<10){
System.out.println("Enter the elements of array.");
for(int i=0;i<M;i++){
for(int j=0;j<M;j++){
arr[i][j]=Integer.parseInt(br.readLine());
arr2[j][i]= arr[i][j];
}
}
for(int i=0;i<M;i++){
row = i;
for(int j=0;j<M;j++){
arr3[row][col] = arr2[i][j];
col--;
}
col = M-1;
}
System.out.println("ORIGINAL MATRIX: ");
for(int i=0;i<M;i++){
for(int j=0;j<M;j++){
System.out.print(arr[i][j]+"  ");
}
System.out.println();
}
int sum = arr3[0][0] + arr3[0][M-1] + arr3[M-1][M-1] + arr3[M-1][0];
System.out.println("MATRIX AFTER ROTATION: ");
for(int i=0;i<M;i++){
for(int j=0;j<M;j++){
System.out.print(arr3[i][j]+"  ");
}
System.out.println();
}
System.out.println("Sum of corner elements = "+sum);
}
else
System.out.println("Invalid Input");
}
}





author

About Me:

I am passionate about programming and technology and am currently in school. I share solutions to programs online. Contact me if you have any doubts regarding ISC Computer Science and ICSE Computer Applications and I will get back to you.

0 comments:

Post a Comment