Ver Mensaje Individual
  #1 (permalink)  
Antiguo 16/09/2010, 19:06
jalex16
 
Fecha de Ingreso: octubre-2006
Mensajes: 562
Antigüedad: 17 años, 6 meses
Puntos: 12
Matrices siempre iguales

Hola
Tengo el siguiente código:

Código C#:
Ver original
  1. public partial class Window1 : Window
  2.     {
  3.         int[,] A, B, At, Bt;
  4.         public Window1()
  5.         {
  6.             InitializeComponent();
  7.         }
  8.  
  9.         private void btnCrearMatrices_Click(object sender, RoutedEventArgs e)
  10.         {
  11.             int n = Convert.ToInt32(txtN.Text);
  12.             A = TrabajaMatriz.crearMatriz(n);
  13.             B = TrabajaMatriz.crearMatriz(n);
  14.            
  15.             txtRes.Text += "Matriz A:\n" + TrabajaMatriz.mostrarMatriz(A) + "\n";
  16.             txtRes.Text += "Matriz B:\n" + TrabajaMatriz.mostrarMatriz(B) + "\n";
  17.            
  18.         }
  19.     }

Y mi clase TrabajaMatriz:
Código C#:
Ver original
  1. static class TrabajaMatriz
  2.     {
  3.         public static int[,] crearMatriz(int tam)
  4.         {
  5.             Random x = new Random();
  6.             int[,] M = new int[tam, tam];
  7.             for (int i = 0; i < tam; i++)
  8.             {
  9.                 for (int j = 0; j < tam; j++)
  10.                 {
  11.                     M[i, j] = x.Next(1, 10);
  12.  
  13.                 }
  14.             }
  15.             MessageBox.Show(Convert.ToString(tam));
  16.             return M;
  17.         }
  18.  
  19.         public static string mostrarMatriz(int[,] M)
  20.         {
  21.             string res = "";
  22.             int lim = M.GetLength(0);
  23.             for (int i = 0; i < lim; i++)
  24.             {
  25.                 for (int j = 0; j < lim; j++)
  26.                 {
  27.                     res += Convert.ToString(M[i, j]) + " ";
  28.                     if (j == lim - 1)
  29.                     {
  30.                         res += "\n";
  31.                     }
  32.                 }
  33.             }
  34.             return res;
  35.         }
  36.     }

Se supone que debe imprimirme 2 matrices diferentes, pero siempre salen duplicadas. Si antes del return en crearMatriz() agrego un MessageBox o algo, todo funciona como debe ser. No entiendo qué pasa.