From e8eb227d44d15684095b07a09e49c0ce9d9910cd Mon Sep 17 00:00:00 2001 From: dehmfl103364 Date: Mon, 26 Oct 2026 18:20:24 +0100 Subject: [PATCH] mutiplyMatrix implementierung --- Startcode/matrix.c | 41 +++++++++++++++++++++++++++++++++++++---- Startcode/matrixTests.c | 5 ++--- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 5493d7b..f345e56 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -2,6 +2,8 @@ #include #include "matrix.h" +#include + // TODO Matrix-Funktionen implementieren Matrix createMatrix(unsigned int rows, unsigned int cols) @@ -38,8 +40,12 @@ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - MatrixType testHelp = 0; - return testHelp; + if(matrix.rows > rowIdx && matrix.cols > colIdx){ + return matrix.buffer[colIdx + rowIdx * matrix.cols]; + }else{ + return UNDEFINED_MATRIX_VALUE; + } + } Matrix add(const Matrix matrix1, const Matrix matrix2) @@ -50,6 +56,33 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - Matrix testHelp = {NULL, 0,0}; - return testHelp; + Matrix resultMatrix = createMatrix(matrix1.rows,matrix2.cols); + + if(matrix1.cols == matrix2.rows){ + unsigned int commonDimension = matrix1.cols; + + for(int i = 0; i < resultMatrix.rows; i++){ + + for(int j = 0; j < resultMatrix.cols; j++){ + MatrixType entry = 0; + + for(int k = 0; k < commonDimension; k++){ + entry += (matrix1.buffer[k + i * matrix1.cols] * matrix2.buffer[j + k * matrix2.cols]); + } + + resultMatrix.buffer[j + i * resultMatrix.cols] = entry; + } + } + + return resultMatrix; + }else{ + resultMatrix.buffer = NULL; + resultMatrix.rows = 0; + resultMatrix.cols = 0; + return resultMatrix; + } + + + + } \ No newline at end of file diff --git a/Startcode/matrixTests.c b/Startcode/matrixTests.c index 6d56e7a..b7743a7 100644 --- a/Startcode/matrixTests.c +++ b/Startcode/matrixTests.c @@ -123,10 +123,9 @@ void test_multiplyFailsOnWrongInputDimensions(void) Matrix matrix1 = {.rows=2, .cols=3, .buffer=buffer1}; Matrix matrix2 = {.rows=2, .cols=3, .buffer=buffer2}; - Matrix result = multiply(matrix1, matrix2); - + Matrix result = multiply(matrix1, matrix2); // was würde passiern wenn der die Matrix fälschlicherweise doch allociert werden würde und jetzt der Speicher nicht mehr freigegeben wird TEST_ASSERT_NULL(result.buffer); - TEST_ASSERT_EQUAL_UINT32(0, result.rows); + TEST_ASSERT_EQUAL_UINT32(0, result.rows); TEST_ASSERT_EQUAL_UINT32(0, result.cols); }