patterncsharpMinor
Matrix reloaded ... or reversed
Viewed 0 times
reloadedmatrixreversed
Problem
I have written this
Just for fun, I have added the ability to sum two
And can be called like:
```
Matrix matrix = new Matrix(new int[,]
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
Matrix class after posting an answer to Malachi's rags-to-riches question. The initial purpose had been to reverse a matrix represented by a 2D array. I have changed it to a more object-orientated version.Just for fun, I have added the ability to sum two
Matrix objects:public class Matrix
{
private int[][] internalMatrix;
private int rowCount = -1;
private int columnCount = -1;
private Matrix(int rowCount, int columnCount)
{
this.rowCount = rowCount;
this.columnCount = columnCount;
Initialize();
}
private void Initialize()
{
internalMatrix = new int[rowCount][];
for (int rowIndex = 0; rowIndex = 0; i--)
{
matrix.internalMatrix[i] = GetReversedRowValues(rowIndex);
rowIndex++;
}
return matrix;
}
private int[] GetReversedRowValues(int rowIndex)
{
int[] reversedRow = new int[columnCount];
int j = 0;
for (int i = columnCount - 1; i >= 0; i--)
{
reversedRow[j] = this[rowIndex, i];
j++;
}
return reversedRow;
}
public static Matrix operator +(Matrix first, Matrix second)
{
if (first.rowCount != second.rowCount || first.columnCount != second.columnCount)
{
throw new NotSupportedException("For adding 2 matrix they need to have the same row and columcount");
}
Matrix sum = new Matrix(first.rowCount, first.columnCount);
for (int rowIndex = 0; rowIndex < first.rowCount; rowIndex++)
{
for (int columnIndex = 0; columnIndex < first.columnCount; columnIndex++)
{
sum[rowIndex, columnIndex] = first[rowIndex, columnIndex] + second[rowIndex, columnIndex];
}
}
return sum;
}
}And can be called like:
```
Matrix matrix = new Matrix(new int[,]
{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
}
Solution
to make this a little clearer you can declare more than one incrementation variable inside your for loop
like this
I don't know what the standard on doing this is, but it seems like a good idea especially for reversing an array.
private int[] GetReversedRowValues(int rowIndex)
{
int[] reversedRow = new int[columnCount];
int j = 0;
for (int i = columnCount - 1; i >= 0; i--)
{
reversedRow[j] = this[rowIndex, i];
j++;
}
return reversedRow;
}like this
private int[] GetReversedRowValues(int rowIndex)
{
int[] reversedRow = new int[columnCount];
for (int i = columnCount - 1, j = 0; i >= 0; i--, j++)
{
reversedRow[j] = this[rowIndex, i];
}
return reversedRow;
}I don't know what the standard on doing this is, but it seems like a good idea especially for reversing an array.
Code Snippets
private int[] GetReversedRowValues(int rowIndex)
{
int[] reversedRow = new int[columnCount];
int j = 0;
for (int i = columnCount - 1; i >= 0; i--)
{
reversedRow[j] = this[rowIndex, i];
j++;
}
return reversedRow;
}private int[] GetReversedRowValues(int rowIndex)
{
int[] reversedRow = new int[columnCount];
for (int i = columnCount - 1, j = 0; i >= 0; i--, j++)
{
reversedRow[j] = this[rowIndex, i];
}
return reversedRow;
}Context
StackExchange Code Review Q#66847, answer score: 5
Revisions (0)
No revisions yet.