Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace MatrixClass
- {
- using System;
- public class Matrix
- {
- private const string NoRowsExceptionMsg = "The Matrix must have at least one row.";
- private const string NoColsExceptionMsg = "The Matrix must have at least one column.";
- private const string OutOfRangeExceptionMsg = "Index is out of range.";
- private int rows;
- private int cols;
- private int[,] matrix;
- public Matrix(int rows, int cols)
- {
- this.Rows = rows;
- this.Cols = cols;
- this.matrix = new int[rows, cols];
- }
- public int Rows
- {
- get
- {
- return this.rows;
- }
- private set
- {
- if (value < 1)
- {
- throw new ArgumentException(NoRowsExceptionMsg);
- }
- this.rows = value;
- }
- }
- public int Cols
- {
- get
- {
- return this.cols;
- }
- private set
- {
- if (value < 1)
- {
- throw new ArgumentException(NoColsExceptionMsg);
- }
- this.cols = value;
- }
- }
- public int this[int rowIndex, int colIndex]
- {
- get
- {
- return this.matrix[rowIndex, colIndex];
- }
- set
- {
- if (rowIndex < 0 || rowIndex >= this.Rows || colIndex < 0 || colIndex >= this.Rows)
- {
- throw new IndexOutOfRangeException(OutOfRangeExceptionMsg);
- }
- this.matrix[rowIndex, colIndex] = value;
- }
- }
- public override string ToString()
- {
- string rows = string.Empty;
- for (int row = 0; row < this.Rows; row++)
- {
- for (int col = 0; col < this.Cols; col++)
- {
- rows += string.Format("{0,5}", this.matrix[row, col]);
- }
- rows += Environment.NewLine;
- }
- return rows;
- }
- }
- public class MatrixClass
- {
- public static void Main()
- {
- Matrix matrix = new Matrix(3, 3);
- int matrixRowsCount = matrix.Rows;
- int matrixColsCount = matrix.Cols;
- int index = matrix[1, 1];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement