Wednesday , 28 June 2017
Latest Posts
Home » Array Related Programs » [Question 2] ISC 2015 Computer Practical Paper Solved – Rotate Matrix 90° Clockwise

# [Question 2] ISC 2015 Computer Practical Paper Solved – Rotate Matrix 90° Clockwise

#### Question:

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 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 90° clockwise as shown below:

$\textrm{Original Matrix}\begin{bmatrix} 1 & 2 & 3\\ 4 & 5 & 6\\ 7 & 8 & 9 \end{bmatrix}\textrm{Rotated Matrix}\begin{bmatrix} 7 & 4 & 1\\ 8 & 5 & 2\\ 9 & 6 & 2\end{bmatrix}$

(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

$\begin{matrix} 3 & 4 & 9 \\ 2 & 5 & 8 \\ 1 & 6 & 7 \end{matrix}$

OUTPUT :

ORIGINAL MATRIX

$\begin{matrix} 3 & 4 & 9 \\ 2 & 5 & 8 \\ 1 & 6 & 7 \end{matrix}$

MATRIX AFTER ROTATION

$\begin{matrix} 1 & 2 & 3 \\ 6 & 5 & 4 \\ 7 & 8 & 9 \end{matrix}$

Sum of the corner elements = 20

Example 2

INPUT :

M = 4

$\begin{matrix} 1 & 2 & 4 & 9 \\ 2 & 5 & 8 & 3 \\ 1 & 6 & 7 & 4 \\ 3 & 7 & 6 & 5 \end{matrix}$

OUTPUT :

ORIGINAL MATRIX

$\begin{matrix} 1 & 2 & 4 & 9 \\ 2 & 5 & 8 & 3 \\ 1 & 6 & 7 & 4 \\ 3 & 7 & 6 & 5 \end{matrix}$

MATRIX AFTER ROTATION

$\begin{matrix} 3 & 1 & 2 & 1 \\ 7 & 6 & 5 & 2 \\ 6 & 7 & 8 & 4 \\ 5 & 4 & 3 & 9 \end{matrix}$

Sum of the corner elements = 18

Example 3

INPUT :
M = 14

OUTPUT :
SIZE OUT OF RANGE

Example 4

INPUT :
M = 112
N = 130

OUTPUT :
INVALID INPUT

#### Programming Code:

/**
* The class Q2_ISC2015 inputs a square matrix and rotates it 90° clockwise
* It also finds and displays the sum of the corner elements
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2015 Question 2
*/

import java.util.*;
class Q2_ISC2015
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();

if(m<3 || m>9)
System.out.println("Size Out Of Range");
else
{
int A[][]=new int[m][m];

/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}

/* Printing the original matrix */
System.out.println("*************************");
System.out.println("The Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("*************************");

/*Rotation of matrix begins here */
System.out.println("Matrix After Rotation is : ");
for(int i=0;i<m;i++)
{
for(int j=m-1;j>=0;j--)
{
System.out.print(A[j][i]+"\t");
}
System.out.println();
}
System.out.println("*************************");

int sum = A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1]; // Finding sum of corner elements
System.out.println("Sum of the corner elements = "+sum);
}
}
}

#### Alternate Way (Creating a new Matrix for storing rotated matrix):

If you want, you can also save the rotated matrix in a separate array and then print it.

#### Programming Code:

/**
* The class Q2_ISC2015 inputs a square matrix and rotates it 90° clockwise
* It also finds and displays the sum of the corner elements
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2015 Question 2
*/

import java.util.*;
class Q2_ISC2015
{
public static void main(String args[])throws Exception
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of the matrix : ");
int m=sc.nextInt();

if(m<3 || m>9)
System.out.println("Size Out Of Range");
else
{
int A[][]=new int[m][m];

/* Inputting the matrix */
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print("Enter an element : ");
A[i][j]=sc.nextInt();
}
}

/* Printing the original matrix */
System.out.println("*************************");
System.out.println("The Original Matrix is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(A[i][j]+"\t");
}
System.out.println();
}
System.out.println("*************************");

int B[][]=new int[m][m];
int x;

/*Rotation of matrix begins here */
for(int i=0;i<m;i++)
{
x = m-1;
for(int j=0;j<m;j++)
{
B[i][j]=A[x][i];
x--;
}
}

/* Printing the rotated matrix */
System.out.println("Matrix After Rotation is : ");
for(int i=0;i<m;i++)
{
for(int j=0;j<m;j++)
{
System.out.print(B[i][j]+"\t");
}
System.out.println();
}
System.out.println("*************************");

int sum = A[0][0]+A[0][m-1]+A[m-1][0]+A[m-1][m-1]; // Finding sum of corner elements
System.out.println("Sum of the corner elements = "+sum);
}
}
}

#### Output:

Enter the size of the matrix : 4
Enter an element : 1
Enter an element : 2
Enter an element : 4
Enter an element : 9
Enter an element : 2
Enter an element : 5
Enter an element : 8
Enter an element : 3
Enter an element : 1
Enter an element : 6
Enter an element : 7
Enter an element : 4
Enter an element : 3
Enter an element : 7
Enter an element : 6
Enter an element : 5
*************************
The Original Matrix is :
1 2 4 9
2 5 8 3
1 6 7 4
3 7 6 5
*************************
Matrix After Rotation is :
3 1 2 1
7 6 5 2
6 7 8 4
5 4 3 9
*************************
Sum of the corner elements = 18

## [Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

1. Rotation can also be done by first finding the transpose of the original matrix and then mirror view of the transpose matrix?

2. sir , i have used different method to solve this problem..at first I have transposed the matrix and then I swapped the columns ….. the programme worked perfect…..will i get less marks for dis…

3. Sir, your code rocks! Only now I realize I wasted effort and paper in separating the rotation part into two separate nested loops.
But what I wished to ask you is if it is absolutely necessary to use ” throws Exception ” while using the Scanner class. I assume since it compiles without that statement, teachers do not mention its importance. Still I ask, in reference to what is expected in Paper-1.

4. Sir I attempted this question and I managed to run the program in one go without any errors which I had written in my paper…but sir regarding the sum of the corner elements the coding which I had written was only for m=3 so I made some slight changes in the program while typing…after this my program was 100% perfect and even my viva was perfect…so sir this change will affect my marks…I’m a bit tensed..sir visiting examiner appreciated my program as it was of shortest length…sir can u also tell me how the ultimate marks in the isc are given…sir I want to thank u bcoz this site helped me a lot while preparing for the practical exam even it will help in my theory…

5. Sir, i attempted this question today. but i did not save the values of the rotated matrix in another array. all i did was use two for loops to simply print the rotated matrix. used to variables i and j. initial value of i was 0, final value was i=0 and decremented by 1. and finally printed it by System.out.print(matrix[j][i]+ ” “); . It worked perfectly. Am i good to go or was it necessary to store the numbers in another matrix?

• Hello Dhanyakumar, your code, from what I could get, is absolutely fine. It was not necessary to store the numbers in another matrix.

6. Sir, u did not reply whether the code given by me was correct or not…am writing it once more…
the main 90 degree reversing loop…
for(int i=0;i<M;i++)
{
for(int j=0;j<M;j++)
{
B[j][M-1-i]=A[i][j];
}//end of j
}//end of i
Here A is the original matrix and B is the rotated matrix….

7. Sir,I have attempted this program and I got the output in one try..but later I found that at two places,I have written another variable ‘n’ instead of ‘M’ by mistake…I have corrected it in the printout portion as well as in the code written on the computer..will any marks be deducted for this?? And I also want to thank you for providing many viva questions which helped in the exam.

8. Sir, I attempted this particular question and written the code absolutely perfect. The algorithm too was correct. Even the viva went very well. The examiner complemented me even. But in the end, (as we do come to know about our marks from the school itself), I got the news that no one in our batch scored 95 even. It struck pretty much as a shock to us, as we are used to getting 98-99 even in the harder school semester examinations. My question is where could the possible deduction happen? And is there any way this marks can increase a bit afterwards?
Kind Regards,

9. How much marks are there for output

10. sir i made slight changes in the program which i typed on the computer compared to the answer which i wrote on the answer sheet and i printed the sum of boundry elements instead of corner elements…how many marks will be deducted.

11. Sir, I have written if(M10) in the condition for invalid input, kindly reply as to how many marks would be deducted?

12. sir i compiled and had attached the output but forgot to write the question number in answer sheet. Will this effect my marks

13. Sir,i compiled this program and successfully got my output during my board practical but i forgot to write “import java.io.*” in the answer sheet.How many marks would be cancelled for this thing.Reply soon
I must say “GUIDE FOR SCHOOL” is best for ISC students.It helped me a lot