Advertisement
RobotReebot

GestioneMatrice

Dec 8th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ConsoleApplication1
  8. {
  9. public class GestioneMatrice
  10. {
  11. protected int[,] matr = new int[4, 4];
  12. protected int[] vect = new int[16];
  13. public void RiempiMatrice(int nMax)
  14. {
  15. Random rnd = new Random();
  16. for(int i = 0; i < 4; i++)
  17. {
  18. for (int j = 0; j < 4; j++)
  19. {
  20. matr[i, j] = rnd.Next(0, nMax);
  21.  
  22. }
  23. }
  24. }
  25. public void visualizza()
  26. {
  27. for (int i = 0; i < 4; i++)
  28. {
  29. for (int j = 0; j < 4; j++)
  30. {
  31. Console.Write(matr[i, j] + " ");
  32. }
  33. Console.WriteLine();
  34. }
  35. }
  36. public bool trovaValore(int t)
  37. {
  38. bool trovato = false;
  39. for (int i = 0; i < 4; i++)
  40. {
  41. for (int j = 0; j < 4; j++)
  42. {
  43. if (matr[i, j] == t)
  44. {
  45. trovato = true;
  46. }
  47. }
  48. }
  49. return trovato;
  50. }
  51. public int MaxRiga()
  52. {
  53. int somma = 0, riga = 0, maxsomma = 0;
  54. for (int i = 0; i < 4; i++)
  55. {
  56. for (int j = 0; j < 4; j++)
  57. {
  58. somma = somma + matr[i, j];
  59. }
  60. if (somma > maxsomma) {
  61. riga = i;
  62. maxsomma = somma;
  63. }
  64. somma = 0;
  65. }
  66. return riga;
  67. }
  68. public virtual bool TuttiPari()
  69. {
  70. bool p = true;
  71. for (int i = 0; i < 4; i++)
  72. {
  73. for (int j = 0; j < 4; j++)
  74. {
  75. if (matr[i, j] % 2 !=0)
  76. {
  77. p = false;
  78. }
  79. }
  80. }
  81. return p;
  82. }
  83. public void Vettorizza(int mod)
  84. {
  85. if (mod == 0)
  86. {
  87. for (int i = 0; i < 4; i++)
  88. {
  89. for (int j = 0; j < 4; j++)
  90. {
  91. vect[i + j] = matr[i, j];
  92. }
  93. }
  94. }
  95. else
  96. {
  97. for (int i = 0; i < 4; i++)
  98. {
  99. for (int j = 0; j < 4; j++)
  100. {
  101. vect[i + j] = matr[j, i];
  102. }
  103. }
  104. }
  105. }
  106. public void Matricizza (int mod, int []tut)
  107. {
  108. if (mod == 0)
  109. {
  110. for (int i = 0; i < 4; i++)
  111. {
  112. for (int j = 0; j < 4; j++)
  113. {
  114. matr[i, j]= tut[i + j];
  115. }
  116. }
  117. }
  118. else
  119. {
  120. for (int i = 0; i < 4; i++)
  121. {
  122. for (int j = 0; j < 4; j++)
  123. {
  124. matr[j, i]=tut[i + j];
  125. }
  126. }
  127. }
  128. }
  129. }
  130.  
  131. class GestioneMatriceOrdinata : GestioneMatrice
  132. {
  133. public override bool TuttiPari()
  134. {
  135. bool p=true;
  136. for (int i = 0; i < 4; i++)
  137. {
  138. p = true;
  139. for (int j = 0; j < 4; j++)
  140. {
  141. if (matr[i, j] % 2 != 0)
  142. {
  143. p = false;
  144. }
  145. }
  146. if (p == true) break;
  147. }
  148. return p;
  149. }
  150.  
  151. public void OrdinaMatrice(int mod)
  152. {
  153. int appoggio,max,end=4;
  154. if (mod == 0)
  155. {
  156. for (int i = 0; i < 4; i++)
  157. {
  158. while (end > 0)
  159. {
  160. max = trovaMax(i, end);
  161. appoggio = matr[i, max];
  162. matr[i, max] = matr[i, end-1];
  163. matr[i, end-1] = appoggio;
  164. end--;
  165. }
  166. end = 4;
  167. }
  168. }
  169. else
  170. {
  171. //fatevelo voi
  172. }
  173. }
  174. private int trovaMax(int riga, int end)
  175. {
  176. int max = 0; int pos=0;
  177. for (int i = 0; i < end; i++)
  178. {
  179. if (matr[riga, i] > max)
  180. {
  181. max = matr[riga, i];
  182. pos = i;
  183. }
  184. }
  185. return pos;
  186. }
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement