|
class Matrix{
public static void main(String []args){
int ArrayMatrix [] [] = {
{1,0,0,0,0},
{0,1,0,0,0},
{0,0,1,0,0},
{0,0,0,1,0},
{0,0,0,0,1},
};
int l = ArrayMatrix.length;
int i,j;
boolean a=true;
boolean b=false;
for( i=0;i<l;i++)
{
for(j=0;j<l;j++)
{
System.out.print(ArrayMatrix[i][j]+" ");
}
System.out.println("");
}
System.out.println("\n Array is Unit Matrix");
}
}
Asad Ali Jogi
modified 12-Nov-12 1:43am.
|
|
|
|
|
I assume you do not get it to work.
As this looks much like a homework, I'll give you some tips but no solution.
read them, follow them and you'll find the right way:
- variable names always start with lower case
always. ALWAYS. Please rename "ArrayMatrix" to "iArrayMatrix". Copy it and set up a second Array for testing.
You can comment them in/out by using Strg+7 in any IDE.
- use clear names
"i" and "j" are common counters. But how about "iRow" and "iCol" ? Those give you much more control on what is happening.
- delete "l"
No. One does not put the length of an Array in a variable. Simply NO.
- I think this homework is about recursion. Read about that on the web. That would need some methods - is this topic already introduced in class? can we use some?
- one boolean "bIsMatrix" should be enough. give it a init value "false" and use the loops to set it to true when you find a value "1" in the diagonal. Set it "false" if not. Read "bIsMatrix" after the loops to present result.
|
|
|
|
|
That's not the complete rule for an identity matrix. The complete rule is that the matrix contains only 1s on the leading diagonal.
Don't hand this in, your teacher won't like the code style, but ...
boolean isIdentity(int[][] m) {
boolean isIdentity = true;
for(int i = 0; i < m.length; i++) for(j = 0; j < m[i].length; j++)
isIdentity &= m[i][j] == (i == j ? 1 : 0);
return isIdentity;
}
... I need to get my crazy coding instincts out in a controlled environment now and then
|
|
|
|