diff --git a/matrix.c b/matrix.c index 666e640..4047e00 100644 --- a/matrix.c +++ b/matrix.c @@ -31,12 +31,21 @@ void clearMatrix(Matrix *matrix) } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) -{ +{ if (rowIdx >= matrix.rows || colIdx >= matrix.cols){ // check if indices are valid + return; // do nothing if out of bounds +} + unsigned int index = rowIdx * matrix.cols + colIdx; // calculate index using row-major formula + matrix.buffer[index] = value ; // set value at that index } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) -{ +{ if (rowIdx >= matrix.rows || colIdx >= matrix.cols){ //check if indices are valid + return UNDEFINED_MATRIX_VALUE; +} + +unsigned int index = rowIdx * matrix.cols + colIdx; //calculate index using row-major formula +return matrix.buffer [index]; // return the value at that index } diff --git a/runMatrixTests b/runMatrixTests index 9fcd440..a8f13eb 100755 Binary files a/runMatrixTests and b/runMatrixTests differ