Guide For School logo Guide For SchoolICSE and ISC Resources

[Question 2] ISC 2016 Computer Practical Paper Solved - Sorting Non-Boundary Matrix Elements

17 February 2016

Solution of Program 2 of ISC 2016 Computer Science Paper 2 (Practical) Exam. Java program to input a square matrix and sort its non-Boundary elements.


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

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

OUTPUT:

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

REARRANGED MATRIX

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

DIAGONAL ELEMENTS

Plain
9			5	
	3	6		
	8	13		
7			8

SUM OF THE DIAGONAL ELEMENTS = 59


Programming Code:

Java
/**
* 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");
        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:

Plain
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

Tags and Categories

Array Related ProgramsClass 12ISC Important ProgramsPrevious Year Solved Questions (ISC)ISCComputer Applications