Advertisement
svetlai

Sample Matrix Class

Feb 11th, 2015
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.52 KB | None | 0 0
  1. namespace MatrixClass
  2. {
  3.     using System;
  4.  
  5.     public class Matrix
  6.     {
  7.         private const string NoRowsExceptionMsg = "The Matrix must have at least one row.";
  8.         private const string NoColsExceptionMsg = "The Matrix must have at least one column.";
  9.         private const string OutOfRangeExceptionMsg = "Index is out of range.";
  10.  
  11.         private int rows;
  12.         private int cols;
  13.         private int[,] matrix;
  14.  
  15.         public Matrix(int rows, int cols)
  16.         {
  17.             this.Rows = rows;
  18.             this.Cols = cols;
  19.             this.matrix = new int[rows, cols];
  20.         }
  21.  
  22.         public int Rows
  23.         {
  24.             get
  25.             {
  26.                 return this.rows;
  27.             }
  28.  
  29.             private set
  30.             {
  31.                 if (value < 1)
  32.                 {
  33.                     throw new ArgumentException(NoRowsExceptionMsg);
  34.                 }
  35.  
  36.                 this.rows = value;
  37.             }
  38.         }
  39.  
  40.         public int Cols
  41.         {
  42.             get
  43.             {
  44.                 return this.cols;
  45.             }
  46.  
  47.             private set
  48.             {
  49.                 if (value < 1)
  50.                 {
  51.                     throw new ArgumentException(NoColsExceptionMsg);
  52.                 }
  53.  
  54.                 this.cols = value;
  55.             }
  56.         }
  57.  
  58.         public int this[int rowIndex, int colIndex]
  59.         {
  60.             get
  61.             {
  62.                 return this.matrix[rowIndex, colIndex];
  63.             }
  64.  
  65.             set
  66.             {
  67.                 if (rowIndex < 0 || rowIndex >= this.Rows || colIndex < 0 || colIndex >= this.Rows)
  68.                 {
  69.                     throw new IndexOutOfRangeException(OutOfRangeExceptionMsg);
  70.                 }
  71.  
  72.                 this.matrix[rowIndex, colIndex] = value;
  73.             }
  74.         }
  75.        
  76.         public override string ToString()
  77.         {
  78.             string rows = string.Empty;
  79.  
  80.             for (int row = 0; row < this.Rows; row++)
  81.             {
  82.                 for (int col = 0; col < this.Cols; col++)
  83.                 {
  84.                     rows += string.Format("{0,5}", this.matrix[row, col]);
  85.                 }
  86.  
  87.                 rows += Environment.NewLine;
  88.             }
  89.  
  90.             return rows;
  91.         }
  92.     }
  93.  
  94.     public class MatrixClass
  95.     {
  96.         public static void Main()
  97.         {
  98.             Matrix matrix = new Matrix(3, 3);
  99.         int matrixRowsCount = matrix.Rows;
  100.         int matrixColsCount = matrix.Cols;
  101.         int index = matrix[1, 1];
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement