jueves, 2 de diciembre de 2010

Cuadrado de la diagonal de una matriz

/*
* matriz.java
*
* Created on 23 de noviembre de 2010, 02:44 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package archivo;
import java.io.*;
import java.util.*;


*/
public class matriz {

/** Creates a new instance of matriz */
public static void main(String[]args) throws FileNotFoundException,IOException // Funcion
{
Scanner sc= new Scanner(new File ("in.txt")); //Lee el archivo
FileOutputStream fos = new FileOutputStream("out.txt"); //Crea Nombre del Archivo donde se va a guardar
PrintStream ps= new PrintStream(fos); // Guardar en el archivo.
int n=sc.nextInt(); // Lee la primera linea del archivo
int m[][]= new int [n][n]; //Se guarda en una matriz con el tamaño de n
for (int i=0;i<=n-1;i++) // los 2 for llenan la matriz i es fila y j es columna
{for(int j=0;j<=n-1;j++)
{ m[i][j]=sc.nextInt(); //Lee los numeros y descarta los espacios entrenumeros
// System.out.println(m[i][j]);
}
}
for (int i=0;i<=n-1;i++) //Visualizacion
{for(int j=0;j<=n-1;j++)
{ //m[i][j]=sc.nextInt();
System.out.print(m[i][j]+" "); // se manda al archivo
ps.print(m[i][j]+" ");
}
ps.println();

System.out.println();
}

int x,y,z;
x=m[0][0];
y=m[1][1];
z=m[2][2];
System.out.print((int)Math.pow(x,2)+" "+(int)Math.pow(y,2)+" "+(int)Math.pow(z,2));
ps.print((int)Math.pow(x,2)+" "+(int)Math.pow(y,2)+" "+(int)Math.pow(z,2));
System.out.println();

}

}

No hay comentarios:

Publicar un comentario