From 0ee879ba207a1012ac70d61aedd67a468d177a6a Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 16 Nov 2026 09:02:57 +0100 Subject: [PATCH] =?UTF-8?q?MatrixMult=20vollst=C3=A4ndig,=20data=20in=20bu?= =?UTF-8?q?ffer=20umbenannt,=20damit=20passend=20zu=20MatrixTests.=205=20T?= =?UTF-8?q?ests=20bestanden,=207=20fehlgeschlagen.=20Fehlersuche=20l=C3=A4?= =?UTF-8?q?uft?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Startcode/matrix.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 3d704d3..05e5042 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -9,24 +9,28 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix matrix; matrix.rows = rows; matrix.cols = cols; - matrix.data = malloc(rows * cols * sizeof(MatrixType)); - + matrix.buffer = NULL; + if (rows == 0 || cols == 0) + { + return; + } + matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); return matrix; } void clearMatrix(Matrix *matrix) { - free(matrix->data); + free(matrix->buffer); } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - matrix.data[rowIdx * matrix.cols + colIdx] = value; + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - MatrixType MatrixAt = matrix.data[rowIdx * matrix.cols + colIdx]; + MatrixType MatrixAt = matrix.buffer[rowIdx * matrix.cols + colIdx]; return MatrixAt; } @@ -48,4 +52,19 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2) { Matrix MatrixMult = createMatrix(matrix1.rows, matrix2.cols); + for (int i = 0; i < matrix1.rows; i++) + { + for (int j = 0; j < matrix2.cols; j++) + { + MatrixType skalar = 0; + for (int k = 0; k < matrix1.cols; k++) + { + MatrixType val1 = getMatrixAt(matrix1, i, k); + MatrixType val2 = getMatrixAt(matrix2, k, j); + skalar += val1 * val2; + } + setMatrixAt(skalar, MatrixMult, i, j); + } + } + return MatrixMult; } \ No newline at end of file