Subscribe to Blog via Email

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

24 comments

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!

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

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:

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?

ICSE | ISC Sample Papers

In this section we will be providing you with the Sample/Guess papers of ICSE (Computer Applications) and ISC (Computer Science) which can be easily downloaded. We hope that the students will benefit from these resources.