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

Question 2:

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.
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


SOURCE CODE


import java.io.*;
import java.util.*;
class ISC2016_SortNonBoundary
{
    public static void main(String args[])
    {
        int A[][],B[];
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter value of M = ");
        int M=sc.nextInt();
        if(M<4 && M>10)
        {
            System.out.println("Invalid");
        }
        else
        {
            A=new int[M][M];
            int N=(int)Math.pow((M-2),2);
            B=new int[N];
            //Loop for entering data
            for(int i=0;i<M;i++)
            {
                for(int j=0;j<M;j++)
                {
                    System.out.print("Enter a number :");
                    A[i][j]=sc.nextInt();
                }
            }
            //Loop to find b[]'s element
            int k=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))
                    {
                        B[k]=A[i][j];k++;
                    }
                }
            }
            //Loop to sort B[]
            for(int i=0;i<N-1;i++)
            {
                for(int j=0;j<N-i-1;j++)
                {
                    if(B[j]>B[j+1])
                    {
                        int k2=B[j];
                        B[j]=B[j+1];
                        B[j+1]=k2;
                    }
                }
            }
            //Loop to print original data
            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();
            }        
            //LOOP TO REARRANGED MATRIXS
            int arr[][]=new int[M][M];
            int z=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))
                    {
                        arr[i][j]=B[z];
                        z++;
                    }
                    else
                        arr[i][j]=A[i][j];
                }
            }            
            System.out.println("REARRANGED ELEMENT");
            for(int i=0;i<M;i++)         
            {
                for(int j=0;j<M;j++)
                {  
                    System.out.print(arr[i][j]+"\t");                    
                }
                System.out.println();
            }
            //Loop to form diagonal element            
            String ar[][]=new String[M][M];            
            int k3=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))
                    {
                        ar[i][j]=String.valueOf(B[k3]);
                        k3++;
                    }
                    else
                        ar[i][j]=" ";
                }
            }

            ar[0][0]=String.valueOf(A[0][0]);
            ar[0][M-1]=String.valueOf(A[0][M-1]);
            ar[M-1][0]=String.valueOf(A[M-1][0]);
            ar[M-1][M-1]=String.valueOf(A[M-1][M-1]);
            System.out.println("DIAGONAL ELEMENT");
            for(int i=0;i<M;i++)         
            {
                for(int j=0;j<M;j++)
                {  
                    System.out.print(ar[i][j]+"\t");                    
                }
                System.out.println();
            }

            //To find the sum of diagonal            
            int a=M-1,b=0,sum=0;;
            for(int i=0;i<M;i++)
            {
                for(int j=0;j<M;j++)
                {
                    if(i==j)
                    {
                        sum=sum+Integer.valueOf(A[i][j]);
                    }
                    if(i==b && j==a)
                    {
                        sum=sum+Integer.valueOf(A[i][j]);
                        a--;b++;
                    }
                }
            }
            System.out.println("Sum="+sum);
        }
    }
}


OUTPUT





If you have any question then leave a comment below I will do my best to answer that question.

Comments

  1. This comment has been removed by the author.

    ReplyDelete
  2. Replies
    1. Hey this is Puneet here author of this blog. if you want any help regarding any of the question then you can just mail me.
      This is my email id " puneet.uttam.99@gamil.com "

      Delete

Post a Comment

Popular posts from this blog

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

Pattern of your own name.

Designing Patterns - Print 'Z'.