From 4640908e1be014531bf43d8aa97afed3931b56ef Mon Sep 17 00:00:00 2001 From: Lukas Weber Date: Wed, 19 Nov 2025 12:26:09 +0100 Subject: [PATCH] Bugfixing, V2 --- matrix.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/matrix.c b/matrix.c index 031bdea..df12e0b 100644 --- a/matrix.c +++ b/matrix.c @@ -75,10 +75,12 @@ return MatrixErgebnis; Matrix multiply(const Matrix matrix1, const Matrix matrix2) { + + Matrix result; + result.rows = matrix1.rows; + result.cols = matrix2.cols; + if (matrix1.rows == matrix2.cols) { - Matrix result; - result.rows = matrix1.rows; - result.cols = matrix2.cols; result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); @@ -86,13 +88,13 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) for(int j = 0; j < result.cols; j++) { MatrixType value = 0; for(int k = 0; k < matrix1.cols; k++) { - value += matrix1.buffer[i][k] * matrix2.buffer[k][j]; + value += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j]; } - result.buffer[i][j] = value; + result.buffer[i * matrix1.cols + j] = value; } } return result; } printf("Die angegebenen Matrizen haben keine passenden Dimensionen für die Multiplikation"); - return NULL; + return result; } \ No newline at end of file