1.
Hacer un programa con matrices que muestre únicamente los valores de la diagonal por ejemplo si la matriz es de 3 x 3 solo deberá imprimir. [3 8 3]
Hacer un programa con matrices que muestre únicamente los valores de la diagonal por ejemplo si la matriz es de 3 x 3 solo deberá imprimir. [3 8 3]
CÓDIGO FUENTE
package
manu;
import javax.swing.JOptionPane;
/**
*
* @author Admin
*/
public class LeerArreglo{
public static void main(String arg[])
{
int v[] ={774, 647, 192, 754, 515, 578, 861, 947, 253};
int n= (int)Math.sqrt(v.length);
int m[][] = new int[n][n];
int cont = 0;
for (int x = 0; x < n; x++)
{
for (int y = 0; y < n; y++)
{
m[y][x] = v[cont];
cont++;
}
}
System.out.println("Matriz:");
String str = " ";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
str += m[j][i]+" ";
}
System.out.println(str);
str = " ";
}
System.out.print("La diagonal
principal es:\n ");
for (int i=0; i<n; i++)
for (int j=0; j<n; j++){
if (i == j){
System.out.print(m[i][j]+"\t");
} else if(i != j) {
System.out.print("
");
}
}
}}
SALIDA

No hay comentarios.:
Publicar un comentario