i Write a program to check whether a matrix is an identity matrix but i do not

know how to find it

plz help me

this my code

package javaapplication162;

import java.util.Scanner;

public class JavaApplication162 {
    
public static void main(String[] args) {
Scanner input = new Scanner (System.in) ; 
 
int [][] Diag = new int [3][3];

System.out.println(" Enter Number 3 rows and 3 columns ") ; 

for (int i=0 ; i < Diag.length ; i++){   
for (int j=0 ; j <Diag[i].length ; j++){
Diag[i][j]=input.nextInt();

}
}
for (int i=0 ; i < Diag.length ; i++){   
for (int j=0 ; j <Diag[i].length ; j++){
    if(Diag[i][i]==Diag[i][i]){
        System.out.println(" yes it's a Identity  matrix ") ; 
    }
    else if(Diag[i][i]!=Diag[i][i]){
         System.out.println(" No it's a Identity  matrix ") ; 
    }
}
}


}
}

You are heading in the right direction, but you haven't got there yet...

in your loops starting lines 20,21 you need to ask:
is this cell on the main diagonal (i==j)?
if so the value must be 1, if not the value must be zero.

If you find any exception to that rule then it's not an identity matrix. If you get to the end of the loops without finding an exception then it is an identity matrix.
You can use a boolean to show that you found an exception.

Time to write some more code...

like this

package javaapplication162;

import java.util.Scanner;

public class JavaApplication162 {
    
public static void main(String[] args) {
Scanner input = new Scanner (System.in) ; 
 
int [][] Diag = new int [3][3];

System.out.println(" Enter Number 3 rows and 3 columns ") ; 

for (int i=0 ; i < Diag.length ; i++){   
for (int j=0 ; j <Diag[i].length ; j++){
Diag[i][j]=input.nextInt();

}
}
for (int i=0 ; i < Diag.length ; i++){   
for (int j=0 ; j <Diag[i].length ; j++){
   if((i==j && Diag[i][j]==1)||(i!=j && Diag[i][j]==0))

        System.out.println(" yes it's a Identity  matrix ") ; 
   
    else{
         System.out.println(" No it's a Identity  matrix ") ; }
   
}
}


}
}

Getting closer! Did you try running it? If you do you'll see what the problem is.

Just finding one cell that's correct doesn't mean this is an I.M. (But just finding one cell that's wrong does mean that it's not an I.M.). That's why you'll need the boolean I hinted at. Run your code, then think about it.

LILE THIS

Scanner input = new Scanner (System.in) ; 
 
int [][] Diag = new int [3][3];

System.out.println(" Enter Number 3 rows and 3 columns ") ; 

for (int i=0 ; i < Diag.length ; i++){   
for (int j=0 ; j <Diag[i].length ; j++){
Diag[i][j]=input.nextInt();

}
}

   boolean check1=false;
boolean check2=false ;

for (int i=0 ; i < Diag.length ; i++){
for (int j=0 ; j <Diag[i].length ; j++){
if (i == j && Diag[i][j] != 0){
check1= true  ;           
}

else if (Diag[i][j] == 0) {
check2=true ;
}

}

}
if (check1 == check2 ){
System.out.print("Yes it's  a identity  matrix ");
}
else
System.out.println("No it's not  identity  matrix ");

    }
}

LIKE THIS

Scanner input = new Scanner (System.in) ; 
 
int [][] Diag = new int [3][3];

System.out.println(" Enter Number 3 rows and 3 columns ") ; 

for (int i=0 ; i < Diag.length ; i++){   
for (int j=0 ; j <Diag[i].length ; j++){
Diag[i][j]=input.nextInt();

}
}

     boolean check1=false;
boolean check2=false ;

for (int i=0 ; i < Diag.length ; i++){
for (int j=0 ; j <Diag[i].length ; j++){
if (i == j && Diag[i][j] == 1){
check1= true  ;           
}

else if (Diag[i][j] == 0) {
check2=true ;
}

}

}
if (check1 == check2 ){
System.out.print("Yes it's  a identity  matrix ");
}
else
System.out.println("No it's not  identity  matrix ");

    }
}

Edited 4 Years Ago by Hussam Alahmadi: n/a

You don't need two booleans, one is enough. And please give it a meaningful name *check1" tells me nothing. Compare that with, for example:

boolean everyCellIsValid = true; // assume that unless proved otherwize
for ...
  for ...
    if (cell value is incorrect) everyCellIsValid = false;

...
if (everyCellIsValid) ... "Yes it's  a identity  matrix "

Good variable names are one essential sign of a good programmer.

i did not understand this

if (cell value is incorrect) everyCellIsValid = false;

...
if (everyCellIsValid) ... "Yes it's  a identity  matrix "

like this

package javaapplication162;

import java.util.Scanner;

public class JavaApplication162 {
    
public static void main(String[] args) {
    
    Scanner input = new Scanner (System.in) ; 

System.out.println(" Enter Number of Position ") ;
int [][] Diag = new int [3][3];
for (int i=0 ; i < Diag.length ; i++){   
for (int j=0 ; j <Diag[i].length ; j++){
    

Diag[i][j]=input.nextInt();
}
}

boolean everyCellIsValid = true;

for (int i=0 ; i < Diag.length ; i++){
for (int j=0 ; j <Diag[i].length ; j++){
if (i == j && Diag[i][j] == 1) 
    everyCellIsValid=true; 
  



 if (Diag[i][j] == 0) everyCellIsValid = false;
}
}

if (everyCellIsValid==true){
    System.out.println("Yes it's not diagonal matrix ");
}

if (everyCellIsValid==false){

System.out.println("No it's not diagonal matrix ");

}


    }
}

Edited 4 Years Ago by Hussam Alahmadi: n/a

Just think what this code says:

if (i == j && Diag[i][j] == 1)  everyCellIsValid=true;

if just one cell is ok then every cell is valid ??? Obviously not right.
That's a good example of how variable names matter.

Line 31 is the right way to deal with a zero on the diagonal, now you have to do the same thing for a 1 not on a diagonal.

(I'm finishing for the night now, so good luck)

This article has been dead for over six months. Start a new discussion instead.