Advertisement
Guest User

Invertir diagonal principal JAVA

a guest
Nov 29th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 KB | None | 0 0
  1.  Dada una matriz cuadrada invertir su diagonal principal
  2.  
  3.  
  4. import java.util.Scanner;
  5. public class JavaMatrizMetod2 {
  6.      
  7.   public static void llenar (int M [] [], int d)
  8.     {
  9.         Scanner Leer = new Scanner(System.in);
  10.     for (int i = 1 ; i <= d ; i++)
  11.     {
  12.         for (int j = 1 ; j <= d ; j++)
  13.         {
  14.         System.out.print("Inserte pos[" + i + "][" + j + "]: ");
  15.         M [i] [j] = Leer.nextInt();
  16.         }
  17.     }
  18.     }
  19.     public static void mostrar (int M [] [], int d)
  20.     {
  21.     for (int i = 1 ; i <= d ; i++)
  22.     {
  23.         System.out.println ();
  24.         for (int j = 1 ; j <= d ; j++)
  25.         {
  26.         System.out.print("[" + M [i] [j] + "]");
  27.         }
  28.     }
  29.     }
  30.     public static void invierte (int M [] [], int d)
  31.     {
  32.     int fin = d;
  33.     for (int i = 1 ; i <= d / 2 ; i++)
  34.     {
  35.         int aux = M [i] [i];
  36.         M [i] [i] = M [d] [d];
  37.         M [d] [d] = aux;
  38.         fin--;
  39.     }
  40.     }
  41.     public static void main (String args [])
  42.     {
  43.         Scanner Leer = new Scanner(System.in);
  44.     int M [] [] = new int [20] [20];
  45.     System.out.print("Inserte dimen. de la matriz cuadrada: ");
  46.     int d = Leer.nextInt();
  47.     llenar (M, d);
  48.     System.out.print("nMATRIZ ORIGINAL: ");
  49.     mostrar (M, d);
  50.     System.out.print("nnMATRIZ CON LA DIAGONAL PRINCIPAL INVERTIDA: ");
  51.     invierte (M, d);
  52.     mostrar (M, d);
  53.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement