Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- class Matrix
- {
- private readonly int[] _matrix;
- public Matrix (params int[] data)
- {
- if (data == null) _matrix = new int[0];
- _matrix = data;
- }
- public int this[int i, int j]
- {
- get
- {
- if (i != j) return 0;
- return _matrix[i];
- }
- set
- {
- if (i == j) _matrix[i] = value;
- }
- }
- public int Size { get => _matrix.Length; }
- public int Track()
- {
- int sum = 0;
- for(int i=0; i<Size;i++)
- {
- sum +=_matrix[i];
- }
- return sum;
- }
- public Matrix Add(Matrix b)
- {
- if (b.Size > Size)
- {
- for (int i = 0; i < Size; i++)
- {
- b._matrix[i] += _matrix[i];
- }
- return b;
- }
- return b.Add(this);
- }
- }
- static void Main()
- {
- Matrix m1 = new Matrix(1, 2, 3, 4, 5);
- Console.WriteLine($"Size of 1st matrix is {m1.Size}");
- Console.WriteLine($"Second element on diagonal is {m1[1, 1]}");
- Console.WriteLine($"Second element of the first row is {m1[0, 1]}");
- Matrix m2 = new Matrix(4, 3, 2, 1);
- Console.WriteLine($"The sum of the elements of the 2nd matrix located on the main diagonal is {m2.Track()}");
- Console.WriteLine($"The sum of the elements of two matrices is ");
- Matrix m3 = m2.Add(m1);
- for(int i=0; i<m3.Size;i++)
- {
- for (int j = 0; j < m3.Size; j++)
- {
- Console.Write(m3[i, j] + " ");
- }
- Console.WriteLine();
- }
- Console.ReadKey();
- }
- }
Add Comment
Please, Sign In to add comment