info@meritcampus.com    +91-85006-22255
...
`

Write a program to find if the given matrix is a scalar matrix.

Input (Two Dimensional matrix) Output (boolean)
`1   0   00   1   00   0   1` true
`-3   0   0   00  -3   0   00   0  -3   00   0   0  -3 ` true
`7   00  7` true
`6   0   00   6   00   0   0` false
`12   89   7723   99   6645   100   111` false

``` class IsScalarMatrix {     public static void main(String s[])    {        int input[][] = {{1, 0, 0}, {0, 1, 0}, {0, 0, 1}};        System.out.println("The given matrix is a scalar matrix : " + isScalarMatrix(input));    }     public static boolean isScalarMatrix(int matrix[][]) { ```
```     } } ```
Topic:

### User comments below.All of them might not be correct.

Above program is to determine whether the matrix is scalar or not.

--Scalra Matrix: Matrix is said to be scalar if the diagonal elements of matrix are same.

for eg:
1 0 0
0 1 0
0 0 1

--To check whether the matrix is scalar or not we have to run for loop till the length of 2D Array.

--If the array is 3*3 matrix,loop will run from 0 to 2.for each iteration we have to check element [0][0],[1][1] and [2][2] is same or not.

--First we store [0][0] element in temp and after that we check if [1][1] and [2][2] is equal to temp then set flag=true and return flag.

Posted by Mânïshå Mùlchåndânï    2015-02-27 06:19:01

here we have to find whether a matrix is scalar... a Matrx is scalar only if its left diagnal contains same element and rest elements shud be zero...... fot this we have to traverse evry element of matrix... take flag=true ....take loop a=0 to row { take another loop b=0 to column..... now if (a=b ){ then. if(array[a][b]!=a[0][0]) then flag =false n break..... here it will chek whthr left diagnal elements are same or not... } ELSE IF array[a][b]!=0 then flag=false break.... here it will chek whether rest elements are zero..exit both loop returm flag.

Posted by Asma Mujtaba Khan    2015-02-27 06:59:18

In the program, we need to find whether the given matrix is scalar or not. To do so, first we need to what is scalar..

A scalar matrix is a matrix in which the entries outside the main diagonal are all zero(0) and all its main diagonal entries equal.

To find this we need to traverse the given matrix using nested loops(its better to take for loops).. the row size and column size must be same.. outer for loop will traverse with rows from i = 0 to size - 1 and the inner for loop will traverse each element of the row in matrix from j = 0 to size - 1. After entering the inner for loop check whether the i & j are equal (i.e., i == j), if so then check whether the matrix[i][j] are equal with matrix[0][0](matrix[i][j] == matrix[0][0]), if the condition fails then return false and stop traversing or else traverse through the end of the matrix.. and if i not equal to j (i.e., i != j) then the value in matrix[i][[j] should be 0... otherwise stop traversing and return false else traverse through the end and return true...

Posted by Gopinath Manchikanti    2015-02-27 10:24:12

This dose is now closed and the winners are Mânïshå Mùlchåndânï,  for 'First Correct Comment', Asma Mujtaba Khan,  for 'Second Correct Comment'. The 'lucky liker' is Asma Mujtaba Khan. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-03-02 07:39:19