diff --git a/matrix.c b/matrix.c index 477d480..8350e08 100644 --- a/matrix.c +++ b/matrix.c @@ -75,7 +75,9 @@ void clearMatrix(Matrix *matrix) { for (int i = 0; i < matrix->rows; i++) { for (int j = 0; j < matrix->cols;j++) { - matrix->buffer[i + matrix->rows * j] = UNDEFINED_MATRIX_VALUE; + // Normally one would expect to work matrices like this, but it is supposed to be the other way around. + // matrix->buffer[i + matrix->rows * j] = UNDEFINED_MATRIX_VALUE; + matrix->buffer[j + matrix->cols * i] = UNDEFINED_MATRIX_VALUE; } } free(matrix->buffer); @@ -87,7 +89,11 @@ void clearMatrix(Matrix *matrix) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { //checks, if the given incies are allowed - if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) matrix.buffer[rowIdx + matrix.rows*(colIdx)] = value; + if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) { + // Normally one would expect to work matrices like this, but it is supposed to be the other way around. + // matrix.buffer[rowIdx + matrix.rows*(colIdx)] = value; + matrix.buffer[colIdx + matrix.cols * rowIdx] = value; + } } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) @@ -95,7 +101,9 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co //checks, if the given indices are allowed if((rowIdx) < matrix.rows && (colIdx) < matrix.cols) { MatrixType returnVal; - returnVal = matrix.buffer[rowIdx + matrix.rows*(colIdx)]; + // Normally one would expect to work matrices like this, but it is supposed to be the other way around. + // returnVal = matrix.buffer[rowIdx + matrix.rows*(colIdx)]; + returnVal = matrix.buffer[colIdx + rowIdx * matrix.cols]; return returnVal; } else return 0;