Advertisement
Guest User

Untitled

a guest
Jan 17th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4.  
  5. class MoodleTask2
  6. {
  7.  
  8. public static bool CheckItentity(decimal[,] matrix)
  9. {
  10. for (int i = 0; i < matrix.GetLength(0); i++)
  11. for (int z = 0; i < matrix.GetLength(1); z++)
  12. if ((i == z) && matrix[i, z] == 1 || (i != z) && matrix[i, z] == 0)
  13. return true;
  14. else
  15. return false;
  16. return false;
  17.  
  18. }
  19.  
  20. public static void PrintMatrix(decimal[,] matrix)
  21. {
  22.  
  23. for (int i = 0; i < matrix.GetLength(0); i++)
  24. {
  25. for (int k = 0; k < matrix.GetLength(1); k++)
  26. {
  27. Console.Write($"{matrix[i, k]} ");
  28. }
  29.  
  30. Console.WriteLine();
  31. }
  32.  
  33. }
  34.  
  35.  
  36. static decimal SumOnNegativeAntiDiagonal(decimal[,] matrix, out decimal sum)
  37. {
  38. sum = 0;
  39. int coll = matrix.GetLength(0);
  40. for (int row = 0; row < matrix.GetLength(1); row++)
  41. {
  42. if (matrix[row, coll - 1] < 0)
  43. {
  44. sum += matrix[row, coll - 1];
  45. }
  46. coll--;
  47. }
  48. return sum;
  49.  
  50. }
  51.  
  52. public static void NormaliseRows(decimal[,] matrix)
  53. {
  54. decimal sum = 0;
  55. for (int row = 0; row < matrix.GetLength(0); row++)
  56. {
  57. for (int c = 0; c < matrix.GetLength(1); c++)
  58. sum += matrix[row, c] * matrix[row, c];
  59. decimal sum1 = (decimal)Math.Sqrt((double)sum);
  60. for (int c = 0; c < matrix.GetLength(1); c++)
  61. {
  62. if (sum1 != 0)
  63. matrix[row, c] /= sum1;
  64. Console.Write($"{matrix[row, c]}");
  65. }
  66. Console.WriteLine();
  67.  
  68. }
  69. }
  70.  
  71. public static void SortMatrix(decimal[,] matrix)
  72. {
  73.  
  74. for(int coll =1; coll < matrix.GetLength(1); coll++)
  75. {
  76. for (int row = 1; row < matrix.GetLength(0); row++)
  77. {
  78. if ((coll % 2) == 0)
  79. {
  80. if (matrix[row-1, coll-1] > matrix[row, coll])
  81. {
  82. var t = matrix[row -1, coll-1];
  83. matrix[row-1, coll-1] = matrix[row, coll];
  84. matrix[row, coll ] = t;
  85.  
  86. }
  87. else
  88. {
  89. if (matrix[row, coll] < matrix[row-1, coll-1])
  90. {
  91. var t = matrix[row-1, coll-1];
  92. matrix[row, coll] = matrix[row , coll];
  93. }
  94. }
  95. }
  96.  
  97. }
  98. }
  99.  
  100. }
  101. static void Main()
  102. {
  103. string za = Console.ReadLine();
  104. decimal[,] matrix;
  105. using (var az = File.OpenText(@za))
  106. {
  107. string row = az.ReadLine();
  108. string coll = az.ReadLine();
  109. int rownum = int.Parse(row);
  110. int collnum = int.Parse(coll);
  111.  
  112. matrix = new decimal[rownum, collnum];
  113. int rower = 0;
  114.  
  115. while (!az.EndOfStream)
  116. {
  117.  
  118. var line = az.ReadLine();
  119. var elements = line.Split(new char[] { ' ' }, StringSplitOptions.None);
  120.  
  121.  
  122. int count = 0;
  123. for (int coller = 0; coller < collnum; coller++)
  124. {
  125. var value = int.Parse(elements[count]);
  126. matrix[rower, coller] = value;
  127.  
  128. count++;
  129.  
  130. }
  131. rower++;
  132.  
  133. }
  134.  
  135.  
  136. }
  137. decimal a;
  138.  
  139. SumOnNegativeAntiDiagonal(matrix, out a);
  140.  
  141. PrintMatrix(matrix);
  142. Console.WriteLine(a);
  143. // NormaliseRows(matrix);
  144.  
  145. SortMatrix(matrix);
  146. PrintMatrix(matrix);
  147. Console.ReadKey();
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement