diff --git a/matrix.c b/matrix.c index 85f8497..0655d72 100644 --- a/matrix.c +++ b/matrix.c @@ -6,14 +6,12 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { - Matrix m; + Matrix m = {0, 0, NULL}; - m.buffer = NULL; - m.rows = rows; - m.cols = cols; - - if (rows > 0 || cols > 0) + if (rows > 0 && cols > 0) { + m.rows = rows; + m.cols = cols; m.buffer = malloc(rows * cols * sizeof(int)); } @@ -46,7 +44,12 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co { MatrixType value = 0; - return value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col) + if (rowIdx < matrix.rows && colIdx < matrix.cols) + { + value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col) + } + + return value; } Matrix add(const Matrix matrix1, const Matrix matrix2) @@ -85,7 +88,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) { Matrix result = {0}; - if (matrix1.cols != matrix2.rows) + if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { return result; } diff --git a/matrix.h b/matrix.h index 595ac23..fb042ea 100644 --- a/matrix.h +++ b/matrix.h @@ -11,7 +11,6 @@ typedef struct { unsigned int rows; // Anzahl der Zeilen unsigned int cols; // Anzahl der Spalten - //void *buffer; MatrixType *buffer; // Zeiger auf die Matrixdaten } Matrix;