My code does an error but I don’t know how to correct it:
public class Cenas {
public static void main(String[] args) {
//gera matrizes de tamanho aleatório com uns e zeros
int a = 2;//(int)(Math.random() *3) + 1;
int b = 2;//(int)(Math.random() *3) + 1;
int[][]matriz = new int [a][b];
do{
for (int i=0; i<a; i++) {
for (int j=0; j<b; j++) {
matriz[i][j] = (int) Math.round(Math.random());
System.out.print(matriz[i][j] + " ");
}
System.out.println("");
}
}while(matrizIdentidade(matriz)==true); //the error is in here!!! the ";"
public static boolean matrizIdentidade (int[][]m){
boolean diagonal = false;
if (m.length==m[0].length) //matriz.Testada[0] é o comprimento da matriz
for (int i = 0; i < m.length; i++)
for (int j = 0; j < m[0].length; j++)
if(i==j && m[i][j]==1)
if(i!=j && m[i][j]==0)
diagonal = true;
return diagonal;
}
}
It generates random matrices and tells me if they are an identity matrix or not. I put the System.out.print and dimension 2 by 2 just for testing. The error makes my loop infinite…
“;” on the line commented appears underlined in red (in Eclipse) giving me an error.
I humbly think that you are missing my question. I don’t know if the statements in my methods are correct (I’m working on it), but what brings me here is that the “;” gives me an error: “Syntax error, insert “}” to complete MethodBody”. If that is due to my badly coded logic I apologized. But I think, instead, that indicates that I’m doing some syntax error on the do-while loop.
You are defining the return value only by the last member
Should be the opposite, you start assuming the matrix is the identity and return false when you check that it is not true.
if the loop ends, the matrix is the identity so return
true.