axeefectushka

Untitled

Oct 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.90 KB | None | 0 0
  1. using System;
  2.  
  3.  
  4. class Program
  5. {
  6.     class Matrix
  7.     {
  8.         private readonly int[] _matrix;
  9.         public Matrix (params int[] data)
  10.         {
  11.             if (data == null) _matrix = new int[0];
  12.             _matrix = data;
  13.         }
  14.         public int this[int i, int j]
  15.         {
  16.             get
  17.             {
  18.                 if (i != j) return 0;
  19.                 return _matrix[i];
  20.             }
  21.             set
  22.             {
  23.                 if (i == j) _matrix[i] = value;
  24.             }
  25.         }
  26.         public int Size { get => _matrix.Length; }
  27.        public int Track()
  28.         {
  29.             int sum = 0;
  30.             for(int i=0; i<Size;i++)
  31.             {
  32.                 sum +=_matrix[i];
  33.             }
  34.             return sum;
  35.         }
  36.         public Matrix Add(Matrix b)
  37.         {
  38.             if (b.Size > Size)
  39.             {
  40.                 for (int i = 0; i < Size; i++)
  41.                 {
  42.                     b._matrix[i] += _matrix[i];
  43.                 }
  44.                 return b;
  45.             }
  46.             return b.Add(this);
  47.         }
  48.        
  49.     }
  50.  
  51.     static void Main()
  52.     {
  53.         Matrix m1 = new Matrix(1, 2, 3, 4, 5);
  54.         Console.WriteLine($"Size of 1st matrix is {m1.Size}");
  55.         Console.WriteLine($"Second element on diagonal is {m1[1, 1]}");
  56.         Console.WriteLine($"Second element of the first row is {m1[0, 1]}");
  57.  
  58.         Matrix m2 = new Matrix(4, 3, 2, 1);
  59.         Console.WriteLine($"The sum of the elements of the 2nd matrix located on the main diagonal is {m2.Track()}");
  60.         Console.WriteLine($"The sum of the elements of two matrices is ");
  61.         Matrix m3 = m2.Add(m1);
  62.         for(int i=0; i<m3.Size;i++)
  63.         {
  64.             for (int j = 0; j < m3.Size; j++)
  65.             {
  66.                 Console.Write(m3[i, j] + " ");
  67.             }
  68.             Console.WriteLine();
  69.         }
  70.         Console.ReadKey();
  71.     }
  72. }
Add Comment
Please, Sign In to add comment