Monday , 24 April 2017
Home » Array Related Programs » ISC 2014 [Question 2] Practical Paper Solved – Symmetric Matrix

ISC 2014 [Question 2] Practical Paper Solved – Symmetric Matrix

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

Question:

Write a program to declare a square matrix A[ ] [ ] of order (M x 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)        Check if the given matrix is Symmetric or not.
           A square matrix is said to be Symmetric, if the element of the ith row and jth column is equal to the element of the jth row and ith column.
(c)        Find the sum of the elements of left diagonal and the sum of the elements of right diagonal of the matrix and display them.

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

Example 1

INPUT           :           M = 3

1       2      3
2       4      5
3       5      6

OUTPUT       :

ORIGINAL MATRIX

1       2      3
2       4      5
3       5      6

THE GIVEN MATRIX IS SYMMETRIC
The sum of the left diagonal = 11
The sum of the right diagonal = 10

Example 2

INPUT           :           M = 4

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

OUTPUT       :

ORIGINAL MATRIX

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

THE GIVEN MATRIX IS NOT SYMMETRIC
The sum of the left diagonal = 17
The sum of the right diagonal = 20

Example 3

INPUT           :           M = 22

OUTPUT       :           THE MATRIX SIZE IS OUT OF RANGE

Programming Code:

/**
* The class SymmetricMatrix_ISC2014 inputs a 2D array and checks whether it is Symmetric or not.
* It then finds the sum of the left and the right diagonals
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Practical 2014 Question 2
*/

import java.io.*;
class SymetricMatrix_ISC2014
{
   public static void main(String args[])throws IOException
   {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

       System.out.print("Enter the number of elements : ");
       int m=Integer.parseInt(br.readLine());
       int A[][]=new int[m][m];
       
       if(m>2 && m<10) // Checking for valid input of rows and columns size
       {  
           System.out.println("\nInputting the elements in the Matrix: n");
           for(int i=0;i<m;i++)
           {
               for(int j=0;j<m;j++)
               {
                   System.out.print("Enter the elements : ");
                   A[i][j]=Integer.parseInt(br.readLine());
               }
           }
       
           /* Printing the Original Matrix */
           System.out.println("\nThe 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();
           }
       
           /* Checking whether the matrix is symmetric or not */
           int flag = 0;
           for(int i=0;i<m;i++)
           {
               for(int j=0;j<m;j++)
               {
                   if(A[i][j] != A[j][i])
                   {
                       flag = 1; // Setting flag = 1 when elements do not match
                       break;
                    }
               }
           }
       
           if(flag == 1)
                System.out.println("\nThe given Matrix is Not Symmetric");
           else
                System.out.println("\nThe given Matrix is Symmetric");
            
                /* Finding sum of the diagonals */
                int ld = 0, rd = 0;
                for(int i=0;i<m;i++)
                {
                    for(int j=0;j<m;j++)
                    {
                        if(i == j) // Condition for the left diagonal
                        {
                            ld = ld + A[i][j];
                        }
                        if((i+j) == (m-1)) // Condition for the right diagonal
                        {
                            rd = rd + A[i][j];
                        }
                    }
                }
       
                System.out.println("The sum of the left diagonal = "+ld);
                System.out.println("The sum of the right diagonal = "+rd);
       }
       
       else
            System.out.println("The Matrix Size is Out Of Range");
   }
}

Output:

symmetric matrix java

Check Also

[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

24 comments

  1. How to accept a character using scanner

  2. We absolutely love your blog and find almost all of your post’s to be just
    what I’m looking for. can you offer guest writers to write content for
    yourself? I wouldn’t mind producing a post or elaborating on a
    few of the subjects you write concerning here.
    Again, awesome weblog!

  3. it helped me a lot..thenku sir!!

  4. it helped me a lot.thanku!

  5. Is it necessary to write isc practical programs using functions or constructors??

  6. Shouldn’t there be another if to check the condition and print the message for an invalid input? Its clearly mentioned in the question right?

  7. sir/mam

    in this program for symmetric condition I have applied this logic
    int A[][]=new int[M][M];
    int b[][]=new int[M][M];
    boolean g= true;
    for(i=0;i<M;i++)
    {
    for(j=0;j<M;j++)
    {
    b[i][j]=A[i][j];
    }
    }
    label :for(i=0;i<M;i++)
    {
    for(j=0;j<M;j++)
    {
    if(b[i][j]==A[j][i]);
    {
    continue;
    g=false;
    }
    else
    {
    System.out.println("the given matrix is not symmetric);
    g=true;
    break label;
    }
    }
    }
    if(g==false)
    {
    System.out.println("the given matrix is symmetric");
    }
    it gave all correct output is it correct

  8. the logic for finding the sum of right diagonal is “if(i==(M-j-1))”
    is it correct?
    also
    for checking the symmetricity of a matrix i have taken a blank 2-d array B[][],transposed the matrix A[][] by “B[j][i]=A[i][j]” and checked the two matrices if (A[i][j]==B[i][j]) then increased a counter variable by 1. if c==arraysize*arraysize, then the matrix is symmetric …..
    is it correct?

    • The condition for accessing right diagonal is (i+j)=m-1
      You can write the above expression like: i=m-j-1 or j=m-i-1. These are nothing but the same above condition. Simple mathematics’ change of subjects.:)
      So what you wrote is correct.

      Regarding your logic for checking for symmetric matrix, then it is correct and you will be awarded full marks for it. However, know that the transpose part was not needed. That is an extra work which you did. 🙂

      A simple A[i][j]==A[j][i] would have done the trick and then you could have increased the variable ‘c’.

      Anyways, no need to worry. 🙂 Focus on your other exams now.

  9. if(m<=2 || m>=10)

    {

    System.out.println(“SIze Out OF Range”);

    }

    this is what I have used.

    reason:M must be greater than 2 and less than 10

    is it wrong. please.

    ——————————————————-

    VIVA VOCE

    The Visiting examiner asked me around 18 questionand I wasn’t able to answer 2-3

    what is the use of out in System.out.println()

    can a package be called as a class.

    please give the answers and also can you predict how many marks I am going to get for VIVA (I couldn’t answer 2-3 Qns out of 16-18)

     

    one more thing.

    i have been taught that public is the default access specifier, but in this website’s viva question post it is mentioned that the  default access specifier is “friendly”   PLEASE HELP ME

    • Hello Anand,

      if(m< =2 || m>=10)
      {
      System.out.println(“SIze Out OF Range”);
      }

      is absolutely correct.

      Regarding the viva questions:

      Q) what is the use of out in System.out.println()
      A) out is an object of the PrintStream class and a static data member of the System class which is calling the println() function.

      Q) can a package be called as a class.
      A) Yes, Package is actually a class present in java.lang package

      According to my prediction, you will lose either 1-2 or no marks. 🙂

      The default access specifier is when you don’t write any access specifier. In java, it is “package private” also called “friendly”.

      Remember this short table:

      Access Levels
      Modifier Within Same Class Within Same Package By Subclass World (Anywhere)
      public Y Y Y Y
      protected Y Y Y N
      no modifier (friendly) Y Y N N
      private Y N N N

      A class may be declared with the modifier public or no modifier. Other members of the class have two additional access modifiers: private and protected.

      1. The public modifier specifies that the class is visible to all classes everywhere.
      2. If a class has no modifier (the default, also known as package-private or friendly), it is visible only to the classes within its own package.
      3. The private modifier specifies that the member can only be accessed in its own class.
      4. The protected modifier specifies that the member can only be accessed within its own package (as with package-private) and, in addition, by a subclass of its class in another package.
      • the way I accessed the diagonals

        left diagonal

        for(int i=0;i<m;i++)

        sum=sum+A[i][i];

        the right diagonal

        int j=M-1;

        for(int i=0;i<M;i++)

        {

        sum=sum+A[i][j];

        j–

        }

        • Hello Anand, you used the same variable ‘sum’ for finding the sum of left and right diagonals? If yes then there is a problem with this. If not and you used two different variables or printed the sum of left diagonal first and made sum=0, before you started the loop for right diagonal, then your code is absolutely fine. However it can be made even better. You need only one loop to perform the above operations. See this:

          int j=M-1, sum1=0, sum2=0;
          for(int i=0;i&lt;M;i++)
          {
            sum1=sum1+A[i][i]; // finding sum of left diagonal
            sum2=sum2+A[i][j]; // finding sum of right diagonal
            j--;
          }
  10. Sir I have implemented the same technique for diagonal sums as you have posted. For symmetric matrix I took a variable as a counter and incremented it whenever A[i][j] was equal to A[j][i]. Then I equated this variable with square of m. If the condition that variable is equal to square of m then it was symmetric or vice versa.Can you tell me whether it will also do?

Leave a Reply to Nabhanshul Cancel reply

Your email address will not be published. Required fields are marked *