Tuesday, May 17, 2011

To find maximum number in upper triangle of matrix

Example:
1 2 3
0 4 5
0 0 6

public class arraydemotriangleBmax {

static void triangleBmax(int x[][])
{
int i,j,max=0;
for(i=0;i<3;i++) { for(j=0;j<3;j++) {max=x[0][0]; if(i<=j) { if(x[i][j]>max)
{
max=x[i][j];
}
}
}
}
System.out.println("The Max Value in triangle B is:"+ max);
}
public static void main(String[] args)
{
triangleBmax(new int[][]{{1,2,3},{4,5,6},{7,8,9}});
}

}

1 comment:

Anonymous said...

This logic is all wrong it will not work!!! You are just iterating through the array and just printing the last value of array..!!!