Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace MatrixFromFile
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             decimal[,] matrix = null;
  11.             using (var file = File.OpenText("matrix.txt"))
  12.             {
  13.                 var rows = int.Parse(file.ReadLine());
  14.                 var cols = int.Parse(file.ReadLine());
  15.                 matrix = new decimal[rows, cols];
  16.                 var line = file.ReadLine();
  17.                 int r = 0; //ред, започва от 0
  18.                 while (line != null)
  19.                 {
  20.                     var strValues = line.Split(' '); // takes each element from the row
  21.                     for (int c = 0; c < strValues.Length; c++) // c < cols
  22.                     {
  23.                         matrix[r, c] = decimal.Parse(strValues[c]); // cast each element to decimal
  24.                     }
  25.                     line = file.ReadLine();
  26.                     ++r;
  27.                 }
  28.  
  29.                 PrintMatrix(matrix); // this function prints the matrix
  30.                 Console.WriteLine();
  31.                 Console.WriteLine("Identity = {0}", CheckIdentity(matrix)); // function tha checks whether the matrix has only 1 on its main diagonal
  32.                 Console.WriteLine();
  33.                 Console.WriteLine("Sum of negative elements on anti diagonal: {0}", SumNegativeOnAntiDiagonal(matrix));
  34.                 Console.WriteLine();
  35.                 SortMatrix(matrix); // this function sorts the matrix
  36.                 PrintMatrix(matrix); // this function prints the sorted matrix
  37.                 Console.WriteLine();
  38.                 NormalizeRows(matrix); // this function normalizes the sorted matrix
  39.             }
  40.  
  41.         }
  42.         static void PrintMatrix(decimal[,] matrix)
  43.         {
  44.             for (int r = 0; r < matrix.GetLength(0); r++) // rows
  45.             {
  46.                 for (int c = 0; c < matrix.GetLength(1); c++) // colws
  47.                 {
  48.                     Console.Write($"{matrix[r,c]}" + "\t");
  49.                 }
  50.                 Console.WriteLine();
  51.             }
  52.         }
  53.  
  54.         static bool CheckIdentity (decimal [,] m)
  55.         {
  56.             if (m.GetLength(0) != m.GetLength(1)) //проверява дали е квадратна матрицата
  57.             {
  58.                 return false;
  59.             }
  60.  
  61.             for (int r = 0; r < m.GetLength(0); r++) // rows и е квадратна матрица
  62.             {
  63.                 for (int c = 0; c < m.GetLength(1); c++) // cols
  64.                 {
  65.                     if (r == c && m[r, c] != 1) // елементите по главния диагонал
  66.                     {
  67.                         return false; // не е единична матрица
  68.                     }
  69.                     else if (r != c && m[r, c] != 0) // останалите елементи
  70.                     {
  71.                         return false; // не е единична матрица
  72.                     }
  73.                 }
  74.             }
  75.             return true;
  76.         }
  77.  
  78.         static void SortMatrix (decimal [,] m)
  79.         {
  80.             for (int c = 0; c < m.GetLength(1); c++) //cols
  81.             {
  82.                 bool sorted;
  83.                 do
  84.                 {
  85.                     sorted = true;
  86.                     for (int r = 1; r < m.GetLength(0); r++) // rows
  87.                     {
  88.                         if ((c % 2 == 0 && m[r-1, c] > m[r, c]) || (c % 2 != 0 && m[r-1, c] < m[r,c]))
  89.                         {
  90.                             decimal temp = m[r - 1, c];
  91.                             m[r - 1, c] = m[r, c];
  92.                             m[r, c] = temp;
  93.                             sorted = false;
  94.                         }
  95.                     }
  96.                 }
  97.                 while (!sorted);
  98.             }
  99.         }
  100.  
  101.         static decimal SumNegativeOnAntiDiagonal(decimal[,] m)
  102.         {
  103.             if (m.GetLength(0) != m.GetLength(1)) // check whether the matrix is squared
  104.             {
  105.                 throw new ArgumentException("Matrix is not squared.");
  106.             }
  107.             decimal sum = 0;
  108.  
  109.             for (int r = 0; r < m.GetLength(0); r++)
  110.             {
  111.                 var c = m.GetLength(0) - 1 - r;
  112.                 if (m[r, c] < 0) // negative numbers
  113.                 {
  114.                     sum += m[r, c];
  115.                 }
  116.             }
  117.             return sum;
  118.         }
  119.  
  120.         static void NormalizeRows(decimal[,] m)
  121.         {
  122.             for (int r = 0; r < m.GetLength(0); ++r)
  123.             {
  124.                 decimal sum = 0;
  125.                 for (int c = 0; c < m.GetLength(1); ++c)
  126.                 {
  127.                     sum += m[r, c] * m[r, c]; // calculates the square of elements
  128.                 }
  129.                 sum = (decimal)Math.Sqrt((double)sum);
  130.  
  131.                 if (sum != 0)
  132.                 {
  133.                     for (int c = 0; c < m.GetLength(1); ++c)
  134.                     {
  135.                         decimal newSum = m[r, c] /= sum;
  136.                         Console.Write("{0:f3}" + "\t", newSum);
  137.                     }
  138.                 }
  139.                 Console.WriteLine();
  140.             }
  141.         }
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement