Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.02 KB | None | 0 0
  1. using System;
  2.  
  3. namespace MatrizSubMatriz
  4. {
  5. class Program
  6. {
  7. /// <summary>
  8. /// Rellena un array de valores en orden ascendente, comenzando por el 1
  9. /// </summary>
  10. /// <param name="a">Array de cualquier tamaño</param>
  11. static void RellenaEnOrdenBi(int[,] a)
  12. {
  13. int i, j;
  14. int valor = 1;
  15. for (i = 0; i < a.GetLength(0); i++)
  16. {
  17. for (j = 0; j < a.GetLength(1); j++)
  18. {
  19. a[i, j] = valor;
  20. valor++;
  21. }
  22. }
  23. }
  24.  
  25. /// <summary>
  26. /// Rellena un array 3x3 con números de tipo entero
  27. /// </summary>
  28. /// <returns>Devuelve matriz 3x3 con valores ingresados por consola</returns>
  29. static int[,] RellenaMatriz3x3() //Tenemos que pasar por parámetro un array 3x3
  30. {
  31. //Variables
  32. int[,] array = new int[3, 3];
  33. int i, j;
  34. int cont = 5;
  35.  
  36. for (i = 0; i < 3; i++)
  37. {
  38. for (j = 0; j < 3; j++)
  39. {
  40. array[i, j] = cont;
  41. cont++;
  42. }
  43. }
  44. return array;
  45. }
  46.  
  47. /// <summary>
  48. /// Muestra por consola un array detallado
  49. /// </summary>
  50. /// <param name="a">Array de enteros vacío</param>
  51. static void EscribeArray(int[] a)
  52. {
  53. Console.Write("[");
  54. for (int i = 0; i < a.Length - 1; i++)
  55. {
  56. Console.Write(a[i] + ",");
  57. }
  58. Console.Write(a[a.Length - 1] + "]");
  59. }
  60.  
  61. /// <summary>
  62. /// Función que imprime por pantalla array multidimensional de cualquier tamaño.
  63. /// </summary>
  64. /// <param name="a">Parámetro de la función y array de la misma</param>
  65. static void EscribeArrayBi(int[,] a)
  66. {
  67. int i, j;
  68. for (i = 0; i < a.GetLength(0); i++)
  69. {
  70. Console.Write("| ");
  71. for (j = 0; j < a.GetLength(1); j++)
  72. {
  73. Console.Write(a[i, j] + " ");
  74. }
  75. Console.WriteLine("|");
  76. }
  77. }
  78.  
  79. /// <summary>
  80. /// Función que copia valores de array desde dos puntos de cota. Te copia los valores desde el inico al final
  81. /// de la cota
  82. /// </summary>
  83. /// <param name="a">Determina primer parámetro de la función y corresponde al array</param>
  84. /// <param name="inicio">Determina segundo parámetro y corresponde al inicio de la cota</param>
  85. /// <param name="fin">Determina tercer parámetro y corresponde al final de la cota</param>
  86. /// <returns>Devuelve copia de los elementos de la cota generada en el array</returns>
  87. static int[] SubArray(int[] a, int inicio, int fin)
  88. {
  89. int j, i;
  90.  
  91. int cota = fin - inicio + 1;
  92. int[] b = new int[cota];
  93.  
  94. i = 0;
  95. for (j = inicio; j <= fin; j++)
  96. {
  97. b[i] = a[j];
  98. i++;
  99. }
  100. return b;
  101. }
  102.  
  103. static int[] CopiaMatrizEnArray(int[,] matriz, int tama)
  104. {
  105. int[] array = new int[tama];
  106. int cont = 0;
  107.  
  108. for (int i = 0; i < matriz.GetLength(0); i++)
  109. {
  110. for (int j = 0; j < matriz.GetLength(1); j++)
  111. {
  112. array[cont] = matriz[i, j];
  113. cont++;
  114. }
  115. }
  116.  
  117. return array;
  118. }
  119.  
  120. static string CastingArrayToString(int[] array)
  121. {
  122. string str = "";
  123.  
  124. for (int i = 0; i < array.Length; i++)
  125. {
  126. str = str + array[i].ToString();
  127. }
  128.  
  129. return str;
  130. }
  131.  
  132.  
  133. static void MatrizContenedora()
  134. {
  135. //Variables
  136. int[,] matriz10x10 = new int[10, 10];
  137. int[] arrayEnteros = new int[9];
  138. int[,] matriz3x3;
  139. int[] arrayCopiaM100;
  140. int[] arrayCopiaM9;
  141. int pos0matriz3x3;
  142. string strArray100 = "";
  143. string strArray9 = "";
  144. string primerElementoGuardado = "";
  145. string trozoString = "";
  146.  
  147. //Inicializamos matriz 10x10 y le damos valores
  148. RellenaEnOrdenBi(matriz10x10);
  149.  
  150. //Mostramos por consola matriz10
  151. Console.WriteLine("Array bidimensional de 10x10:");
  152. Console.WriteLine();
  153. EscribeArrayBi(matriz10x10);
  154.  
  155. //Pedimos al usuario los valores enteros para la submatriz 3x3 y le damos valores
  156. Console.WriteLine("Array bidimensional de 3x3:");
  157. Console.WriteLine();
  158. matriz3x3 = RellenaMatriz3x3();
  159. EscribeArrayBi(matriz3x3);
  160. Console.WriteLine();
  161.  
  162. //Pasamos a un array de 1 dimensión los valores de matriz10x10
  163. arrayCopiaM100 = CopiaMatrizEnArray(matriz10x10, 100);
  164. arrayCopiaM9 = CopiaMatrizEnArray(matriz3x3, 9);
  165.  
  166. //Pasamos los arrays a string para trabajar mejor con los datos
  167. strArray100 = CastingArrayToString(arrayCopiaM100);
  168. strArray9 = CastingArrayToString(arrayCopiaM9);
  169.  
  170. //Si strArray9 (subMatriz3x3) está contenida en strArray100 (matriz10x10)
  171. if (strArray100.Contains(strArray9))
  172. {
  173. //Guarda el primer elemento de strArray9 (subMatriz3x3)
  174. primerElementoGuardado += strArray9[0];
  175. }
  176.  
  177. //Hacemos un subString desde primerElementoGuardado+1 (pos[1] de matriz3x3) hasta el final de strArray100 (matriz10x10)
  178. //trozoString = strArray100.Substring();
  179.  
  180. //El resultado final, coordenadas buscadas, serían las coordenadas del último elemento de strArray100
  181. //Tendríamos que comprobar eso en la matriz10x10
  182. }
  183.  
  184. static void Main(string[] args)
  185. {
  186. //MatrizContenedora();
  187.  
  188.  
  189. }
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement