**Click here to download the complete ISC 2016 Computer Science Paper 2 (Practical).**

**Question:**

Write a program to declare a square matrix A[][] of order (M x M) where ‘M’ must be greater than 3 and less than 10. Allow the user to input positive integers into this matrix. Perform the following tasks on the matrix:

(a) Sort the non-boundary elements in ascending order using any standard sorting technique and rearrange them in the matrix.

(b) Calculate the sum of both the diagonals.

(c) Display the original matrix, rearranged matrix and only the diagonal elements of the rearranged matrix with their sum.

Test your program with the sample data and some random data:

**Example 1**

**INPUT :**M = 4

9 2 1 5 8 13 8 4 15 6 3 11 7 12 23 8

**OUTPUT:**

**ORIGINAL MATRIX**

9 2 1 5 8 13 8 4 15 6 3 11 7 12 23 8

**REARRANGED MATRIX**

9 2 1 5 8 3 6 4 15 8 13 11 7 12 23 8

**DIAGONAL ELEMENTS**

9 5 3 6 8 13 7 8

SUM OF THE DIAGONAL ELEMENTS = 59

**Programming Code:**

/** * The class SortNonBoundary_ISC2016 inputs a square matrix and * sorts the non-boundary elements in ascending order * @author : www.guideforschool.com * @Program Type : BlueJ Program - Java * @Question Year : ISC Practical 2016 Question 2 */ import java.util.*; class SortNonBoundary_ISC2016 { int A[][],B[],m,n; void input() //Function for taking all the necessary inputs { Scanner sc = new Scanner(System.in); System.out.print("Enter the size of the square matrix : "); m=sc.nextInt(); if(m<4 || m>10) { System.out.println("Invalid Range"); System.exit(0); } else { A = new int[m][m]; n = (m-2)*(m-2); B = new int[n]; //Array to store Non-Boundary Elements System.out.println("Enter the elements of the Matrix : "); for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { System.out.print("Enter a value : "); A[i][j]=sc.nextInt(); } } } } /* The below function stores Non-Boundary elements * from array A[][] to array B[] if s = 1 * else stores the Non-Boundary elements in array A[][] from array B[] */ void convert(int s) { int x=0; for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { if(i != 0 && j != 0 && i != m-1 && j != m-1) { if(s==1) B[x] = A[i][j]; else A[i][j] = B[x]; x++; } } } } void sortArray() //Function for sorting Non-Boundary elements stored in array B[] { int c = 0; for(int i=0; i<n-1; i++) { for(int j=i+1; j<n; j++) { if(B[i]>B[j]) { c = B[i]; B[i] = B[j]; B[j] = c; } } } } void printArray() //Function for printing the array A[][] { for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { System.out.print(A[i][j]+"\t"); } System.out.println(); } } void printDiagonal() //Function for printing the diagonal elements and their sum { int sum = 0; for(int i=0;i<m;i++) { for(int j=0;j<m;j++) { if(i==j || (i+j)==m-1) { System.out.print(A[i][j]+"\t"); sum = sum + A[i][j]; } else System.out.print("\t"); } System.out.println(); } System.out.println("Sum of the Diagonal Elements : "+sum); } public static void main(String args[]) { SortNonBoundary_ISC2016 ob = new SortNonBoundary_ISC2016(); ob.input(); System.out.println("*********************"); System.out.println("The original matrix:"); System.out.println("*********************"); ob.printArray(); //Printing the original array ob.convert(1); //Storing Non-Boundary elements to a 1-D array ob.sortArray(); //Sorting the 1-D array (i.e. Non-Diagonal Elements) ob.convert(2); //Storing the sorted Non-Boundary elements back to original 2-D array System.out.println("*********************"); System.out.println("The Rearranged matrix:"); System.out.println("*********************"); ob.printArray(); //Printing the rearranged array System.out.println("*********************"); System.out.println("The Diagonal Elements:"); System.out.println("*********************"); ob.printDiagonal(); //Printing the diagonal elements and their sum } }

**Output:**

Enter the size of the square matrix : 4 Enter the elements of the Matrix : Enter a value : 9 Enter a value : 2 Enter a value : 1 Enter a value : 5 Enter a value : 8 Enter a value : 13 Enter a value : 8 Enter a value : 4 Enter a value : 15 Enter a value : 6 Enter a value : 3 Enter a value : 11 Enter a value : 7 Enter a value : 12 Enter a value : 23 Enter a value : 8 ********************* The original matrix: ********************* 9 2 1 5 8 13 8 4 15 6 3 11 7 12 23 8 ********************* The Rearranged matrix: ********************* 9 2 1 5 8 3 6 4 15 8 13 11 7 12 23 8 ********************* The Diagonal Elements: ********************* 9 5 3 6 8 13 7 8 Sum of the Diagonal Elements : 59

Sir shouldn’t the sum be 79 for this example?

No.

9 + 5 + 3 + 6 + 8 + 13 + 7 + 8 = 59

Can’t thank enough to the author of the site. Following guideforschool.com even before than when it changed from javaforschool.com.

It helped me alot in my std. 10 board exams because of which I was able to score above 90% in computer science.

Since then I have been a regular follower of the website.

Back then only few of my friends knew about the site but now its astonishing to see how much popular this site has become. I learned a lot of new thing from the programs that are given. Solutions are short and easy to understand. There is something new to learn in every other program.

When the site changed to guideforschool.com and was providing for other subjects too I became more than just happy. Regular updates made it even better.

Today I gave my std.12 computer practical exam and guideforschool.com was the sole location for practicing programs and viva questions.

I was able to solve all the 3 questions and gave my viva nicely. Today when I reached school guideforschool.com was the talk of the town. All of my friends had brought printed notes of the viva questions given in the website.

Yesterday while opening the page sometimes it showed error and I knew what the reason was. Every student around the country was opening the site which was slowing down the server. This shows how huge the site has grown.

And the icing on the cake was when I saw that just 2 nights before the physics practical exam 2011 and 2008 solved practical question was uploaded and the same question came in the physics practical exam. Wonderful!

I am really thankful to Mr. Md Zeeshan Akhtar for creating such a helpful site and providing help to students around the country free of costs.Today I thought I really needed to thank you for all of this.Keep up the good work sir !

Regards,

Sangram Gorai

Thank u so much as this site was very helpful. I did this question for the ISC board exam practical 2day and i got everything correctly. Also,viva was very easy. So,30 marks assured.