C Programs Tutorials | IT Developer
IT Developer

Java Programs - Solved 2015 Practical Paper ISC Computer Science



Share with a Friend

Solved 2015 Practical Paper ISC Computer Science

Class 12 - ISC Computer Science Solved Practical Papers

Rotate Matrix 90 Degrees Program - ISC 2015 Practical

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 user to input integers into this matrix. Perform the following tasks:

(a) Display the original matrix.
(b) Rotate the matrix 90o clockwise as shown below:

Original matrix        Rotated matrix
1 2 3 7 4 1
4 5 6 8 5 2
7 8 9 9 6 3

(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

3    4    9
2 5 8
1 6 7

OUTPUT:
ORIGINAL MATRIX

3    4    9
2 5 8
1 6 7

MATRIX AFTER ROTATION

1    2    3
6 5 4
7 8 9

Sum of the corner elements = 20

Example 2:
INPUT:
M = 4

1    2    4    9
2 5 8 3
1 6 7 4
3 7 6 5

OUTPUT:

ORIGINAL MATRIX

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 the corner elements = 18

Example 3:
INPUT:
M = 14
OUTPUT:
SIZE OUT OF RANGE

import java.io.*; class Rotate{ public static void main(String args[])throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("M = "); int m = Integer.parseInt(br.readLine()); if(m < 3 || m > 9){ System.out.println("SIZE OUT OF RANGE"); return; } int a[][] = new int[m][m]; System.out.println("Enter matrix elements:"); for(int i = 0; i < m; i++){ for(int j = 0; j < m; j++){ a[i][j] = Integer.parseInt(br.readLine()); } } 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] + "\t"); } System.out.println(); } int r[][] = new int[m][m]; int row = 0; int col = m - 1; for(int i = 0; i < m; i++){ row = 0; for(int j = 0; j < m; j++){ r[row++][col] = a[i][j]; } col--; } System.out.println("MATRIX AFTER ROTATION"); for(int i = 0; i < m; i++){ for(int j = 0; j < m; j++){ System.out.print(r[i][j] + "\t"); } System.out.println(); } int last = m - 1; int sum = a[0][0] + a[0][last] + a[last][0] + a[last][last]; System.out.println("Sum of the corner elements = " + sum); } }

Output

 
OUTPUT 1:
M = 3
Enter matrix elements:
1
2
3
4
5
6
7
8
9
ORIGINAL MATRIX
1   2   3   
4   5   6   
7   8   9   
MATRIX AFTER ROTATION
7   4   1   
8   5   2   
9   6   3   
Sum of the corner elements = 20

OUTPUT 2:
M = 4
Enter matrix elements:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ORIGINAL MATRIX
1   2   3   4   
5   6   7   8   
9   10  11  12  
13  14  15  16  
MATRIX AFTER ROTATION
13  9   5   1   
14  10  6   2   
15  11  7   3   
16  12  8   4   
Sum of the corner elements = 34