Advertisement
Guest User

Untitled

a guest
Dec 15th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.14 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3.  
  4. namespace MatrixExerciseExam
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             int n = int.Parse(Console.ReadLine());
  11.             var matrix = new decimal[n, n];
  12.             Initialize(matrix, "matrix.txt");
  13.             PrintMatrix(matrix);
  14.             Console.WriteLine($"Sum = {SumPositiveAndEven(matrix)}");
  15.             int row, col;
  16.             FindLeastPositiveElement(matrix, out row, out col);
  17.             if (row != -1)
  18.             {
  19.                 Console.WriteLine($"Row = {row}, Col = {col}");
  20.             }
  21.             else
  22.             {
  23.                 Console.WriteLine("No positive elements found.");
  24.             }
  25.         }
  26.         static void Initialize (decimal [,] m, string path)
  27.         {
  28.             for (int i = 0; i < m.GetLength(0); i++)
  29.             {
  30.                 for (int k = 0; k < m.GetLength(1); k++)
  31.                 {
  32.                     m[i, k] = -1;
  33.                 }
  34.             }
  35.             using (var f = File.OpenText(path))
  36.             {
  37.                 while (!f.EndOfStream)
  38.                 {
  39.                     // Read a line
  40.                     var line = f.ReadLine();
  41.  
  42.                     // Parse to value, col, row
  43.                     var values = line.Split(' ');
  44.                     var value = decimal.Parse(values[0]);
  45.                     var row = int.Parse(values[1]);
  46.                     var col = int.Parse(values[2]);
  47.  
  48.                     // Assign to matrix
  49.                     m[row, col] = value;
  50.                 }
  51.             }
  52.         }
  53.  
  54.         static void PrintMatrix(decimal[,] m)
  55.         {
  56.             for (int row = 0; row < m.GetLength(0); row++)
  57.             {
  58.                 for (int col = 0; col < m.GetLength(1); col++)
  59.                 {
  60.                     Console.Write(m[row, col] + "\t");
  61.                 }
  62.                 Console.WriteLine();
  63.             }
  64.         }
  65.  
  66.         static decimal SumPositiveAndEven(decimal[,] m)
  67.         {
  68.             decimal sum = 0;
  69.             for (int row = 0; row < m.GetLength(0); row++)
  70.             {
  71.                 for (int col = 0; col < m.GetLength(1); col++)
  72.                 {
  73.                     if (m[row, col] > 0 && m[row, col] % 2 == 0)
  74.                     {
  75.                         sum += m[row, col];
  76.                     }
  77.                 }
  78.             }
  79.             return sum;
  80.         }
  81.  
  82.         static void FindLeastPositiveElement(decimal[,] m, out int leastPositiveRow, out int leastPositiveCol)
  83.         {
  84.             decimal positiveMin = -1;
  85.             leastPositiveCol = -1;
  86.             leastPositiveRow = -1;
  87.  
  88.             for (int row = 0; row < m.GetLength(0); row++)
  89.             {
  90.                 for (int col = 0; col < m.GetLength(1); col++)
  91.                 {
  92.                     if(m[row, col] > 0 && (m[row, col] < positiveMin || leastPositiveCol == -1))
  93.                     {
  94.                         positiveMin = m[row, col];
  95.                         leastPositiveRow = row;
  96.                         leastPositiveCol = col;
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement