Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Linq;
- class MoodleTask2
- {
- public static bool CheckItentity(decimal[,] matrix)
- {
- for (int i = 0; i < matrix.GetLength(0); i++)
- for (int z = 0; i < matrix.GetLength(1); z++)
- if ((i == z) && matrix[i, z] == 1 || (i != z) && matrix[i, z] == 0)
- return true;
- else
- return false;
- return false;
- }
- public static void PrintMatrix(decimal[,] matrix)
- {
- for (int i = 0; i < matrix.GetLength(0); i++)
- {
- for (int k = 0; k < matrix.GetLength(1); k++)
- {
- Console.Write($"{matrix[i, k]} ");
- }
- Console.WriteLine();
- }
- }
- static decimal SumOnNegativeAntiDiagonal(decimal[,] matrix, out decimal sum)
- {
- sum = 0;
- int coll = matrix.GetLength(0);
- for (int row = 0; row < matrix.GetLength(1); row++)
- {
- if (matrix[row, coll - 1] < 0)
- {
- sum += matrix[row, coll - 1];
- }
- coll--;
- }
- return sum;
- }
- public static void NormaliseRows(decimal[,] matrix)
- {
- decimal sum = 0;
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int c = 0; c < matrix.GetLength(1); c++)
- sum += matrix[row, c] * matrix[row, c];
- decimal sum1 = (decimal)Math.Sqrt((double)sum);
- for (int c = 0; c < matrix.GetLength(1); c++)
- {
- if (sum1 != 0)
- matrix[row, c] /= sum1;
- Console.Write($"{matrix[row, c]}");
- }
- Console.WriteLine();
- }
- }
- public static void SortMatrix(decimal[,] matrix)
- {
- for(int coll =1; coll < matrix.GetLength(1); coll++)
- {
- for (int row = 1; row < matrix.GetLength(0); row++)
- {
- if ((coll % 2) == 0)
- {
- if (matrix[row-1, coll-1] > matrix[row, coll])
- {
- var t = matrix[row -1, coll-1];
- matrix[row-1, coll-1] = matrix[row, coll];
- matrix[row, coll ] = t;
- }
- else
- {
- if (matrix[row, coll] < matrix[row-1, coll-1])
- {
- var t = matrix[row-1, coll-1];
- matrix[row, coll] = matrix[row , coll];
- }
- }
- }
- }
- }
- }
- static void Main()
- {
- string za = Console.ReadLine();
- decimal[,] matrix;
- using (var az = File.OpenText(@za))
- {
- string row = az.ReadLine();
- string coll = az.ReadLine();
- int rownum = int.Parse(row);
- int collnum = int.Parse(coll);
- matrix = new decimal[rownum, collnum];
- int rower = 0;
- while (!az.EndOfStream)
- {
- var line = az.ReadLine();
- var elements = line.Split(new char[] { ' ' }, StringSplitOptions.None);
- int count = 0;
- for (int coller = 0; coller < collnum; coller++)
- {
- var value = int.Parse(elements[count]);
- matrix[rower, coller] = value;
- count++;
- }
- rower++;
- }
- }
- decimal a;
- SumOnNegativeAntiDiagonal(matrix, out a);
- PrintMatrix(matrix);
- Console.WriteLine(a);
- // NormaliseRows(matrix);
- SortMatrix(matrix);
- PrintMatrix(matrix);
- Console.ReadKey();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement