From 954318593c2447d29e79d9e0d837bfbc817d7e4d Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 9 Nov 2026 09:12:40 +0100 Subject: [PATCH 1/8] createMatrix und clearMatrix erstellt --- Startcode/matrix.c | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index ad00628..a9d39f1 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -1,35 +1,32 @@ +#include "matrix.h" #include #include -#include "matrix.h" // TODO Matrix-Funktionen implementieren Matrix createMatrix(unsigned int rows, unsigned int cols) { - + Matrix matrix; + matrix.rows = rows; + matrix.cols = cols; + matrix.data = malloc(rows * cols * sizeof(MatrixType *)); + + return matrix; } void clearMatrix(Matrix *matrix) { - + free(matrix->data); } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - } -Matrix add(const Matrix matrix1, const Matrix matrix2) -{ - -} +Matrix add(const Matrix matrix1, const Matrix matrix2) {} -Matrix multiply(const Matrix matrix1, const Matrix matrix2) -{ - -} \ No newline at end of file +Matrix multiply(const Matrix matrix1, const Matrix matrix2) {} \ No newline at end of file From fc2b14d0e058dc2bfa7c6fa776c2b44dcf0d5a8c Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 9 Nov 2026 09:21:41 +0100 Subject: [PATCH 2/8] matrix.h erstellt --- Startcode/matrix.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Startcode/matrix.h b/Startcode/matrix.h index cc640d1..9f8bca4 100644 --- a/Startcode/matrix.h +++ b/Startcode/matrix.h @@ -6,7 +6,12 @@ typedef float MatrixType; // TODO Matrixtyp definieren - +typedef struct Matrix +{ + unsigned int rows; + unsigned int cols; + MatrixType *data; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix); @@ -15,5 +20,4 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2); Matrix multiply(const Matrix matrix1, const Matrix matrix2); - #endif From 240cc772880d379df7fff48a73c7b0cfbe5aa7d4 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 15 Nov 2026 22:22:01 +0100 Subject: [PATCH 3/8] setMatrix, getMatrix und Matrix add bearbeitet. Fehlermeldung bei Test von Matrix add, Ursache unbekannt. Liegt vielleicht an fehlender Implementierung von Matrix multiply --- Startcode/matrix.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index a9d39f1..e4e9de0 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -9,7 +9,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix matrix; matrix.rows = rows; matrix.cols = cols; - matrix.data = malloc(rows * cols * sizeof(MatrixType *)); + matrix.data = malloc(rows * cols * sizeof(MatrixType)); return matrix; } @@ -21,12 +21,30 @@ void clearMatrix(Matrix *matrix) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { + matrix.data[rowIdx * matrix.cols + colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { + MatrixType MatrixAt = matrix.data[rowIdx * matrix.cols + colIdx]; + return MatrixAt; } -Matrix add(const Matrix matrix1, const Matrix matrix2) {} +Matrix add(const Matrix matrix1, const Matrix matrix2) +{ + Matrix MatrixAdd = createMatrix(matrix1.rows, matrix1.cols); + for (int i = 0; i < matrix1.rows; i++) + { + for (int j = 0; j < matrix1.cols; j++) + { + MatrixType val1 = getMatrixAt(matrix1, i, j); + MatrixType val2 = getMatrixAt(matrix2, i, j); + setMatrixAt(val1 + val2, MatrixAdd, i, j); + } + } + return MatrixAdd; +} -Matrix multiply(const Matrix matrix1, const Matrix matrix2) {} \ No newline at end of file +Matrix multiply(const Matrix matrix1, const Matrix matrix2) +{ +} \ No newline at end of file From 4a31d6388d3a8a6d1266338bbb8f9e4886515ec4 Mon Sep 17 00:00:00 2001 From: Philipp Date: Sun, 15 Nov 2026 22:33:59 +0100 Subject: [PATCH 4/8] Matrix multiply angefangen --- Startcode/matrix.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index e4e9de0..3d704d3 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -47,4 +47,5 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2) { + Matrix MatrixMult = createMatrix(matrix1.rows, matrix2.cols); } \ No newline at end of file From 0ee879ba207a1012ac70d61aedd67a468d177a6a Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 16 Nov 2026 09:02:57 +0100 Subject: [PATCH 5/8] =?UTF-8?q?MatrixMult=20vollst=C3=A4ndig,=20data=20in?= =?UTF-8?q?=20buffer=20umbenannt,=20damit=20passend=20zu=20MatrixTests.=20?= =?UTF-8?q?5=20Tests=20bestanden,=207=20fehlgeschlagen.=20Fehlersuche=20l?= =?UTF-8?q?=C3=A4uft?= 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 From bea15455640ede927716edec181ae9a65bd50134 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 16 Nov 2026 09:03:53 +0100 Subject: [PATCH 6/8] MatrixType *data in MatrixType *buffer umbenannt, damit passend zu MatrixTests --- Startcode/matrix.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Startcode/matrix.h b/Startcode/matrix.h index 9f8bca4..be69f2a 100644 --- a/Startcode/matrix.h +++ b/Startcode/matrix.h @@ -10,7 +10,7 @@ typedef struct Matrix { unsigned int rows; unsigned int cols; - MatrixType *data; + MatrixType *buffer; } Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); From e1284e9efc22f554b87699294e23ad85fa64983f Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 16 Nov 2026 09:27:08 +0100 Subject: [PATCH 7/8] =?UTF-8?q?Fehlersuche=20l=C3=A4uft,=20keine=20Fortsch?= =?UTF-8?q?ritte?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Startcode/matrix.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 05e5042..3bbcc64 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -10,9 +10,9 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) matrix.rows = rows; matrix.cols = cols; matrix.buffer = NULL; - if (rows == 0 || cols == 0) + if (matrix.rows == 0 || matrix.cols == 0) { - return; + return matrix; } matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); return matrix; @@ -21,6 +21,7 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) void clearMatrix(Matrix *matrix) { free(matrix->buffer); + matrix->buffer = NULL; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) From 0d41f5e63145cecdd394799f43ce458e2dbc5e56 Mon Sep 17 00:00:00 2001 From: Philipp Date: Mon, 16 Nov 2026 20:18:38 +0100 Subject: [PATCH 8/8] alle Fehler behoben, alle Tests bestanden. Kann so genutzt werden --- Startcode/matrix.c | 61 +++++++++++++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 19 deletions(-) diff --git a/Startcode/matrix.c b/Startcode/matrix.c index 3bbcc64..6944fd8 100644 --- a/Startcode/matrix.c +++ b/Startcode/matrix.c @@ -12,6 +12,8 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) matrix.buffer = NULL; if (matrix.rows == 0 || matrix.cols == 0) { + matrix.rows = 0; + matrix.cols = 0; return matrix; } matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); @@ -22,50 +24,71 @@ void clearMatrix(Matrix *matrix) { free(matrix->buffer); matrix->buffer = NULL; + matrix->rows = 0; + matrix->cols = 0; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - matrix.buffer[rowIdx * matrix.cols + colIdx] = value; + if ((rowIdx < matrix.rows) && (colIdx < matrix.cols)) + { + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; + } + return; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - MatrixType MatrixAt = matrix.buffer[rowIdx * matrix.cols + colIdx]; - return MatrixAt; + if ((rowIdx < matrix.rows) || (colIdx < matrix.cols)) + { + MatrixType MatrixAt = matrix.buffer[rowIdx * matrix.cols + colIdx]; + return MatrixAt; + } + return UNDEFINED_MATRIX_VALUE; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - Matrix MatrixAdd = createMatrix(matrix1.rows, matrix1.cols); - for (int i = 0; i < matrix1.rows; i++) + unsigned int resRows = (matrix1.rows >= matrix2.rows) ? matrix1.rows : matrix2.rows; + unsigned int resCols = (matrix1.cols >= matrix2.cols) ? matrix1.cols : matrix2.cols; + + Matrix MatrixAdd = createMatrix(resRows, resCols); + if ((matrix1.rows == matrix2.rows || matrix1.rows == 1 || matrix2.rows == 1) && (matrix1.cols == matrix2.cols || (matrix1.cols == 1 || matrix2.cols == 1)) && matrix1.rows > 0 && matrix1.cols > 0 && matrix2.rows > 0 && matrix2.cols > 0) { - for (int j = 0; j < matrix1.cols; j++) + for (int i = 0; i < resRows; i++) { - MatrixType val1 = getMatrixAt(matrix1, i, j); - MatrixType val2 = getMatrixAt(matrix2, i, j); - setMatrixAt(val1 + val2, MatrixAdd, i, j); + for (int j = 0; j < resCols; j++) + { + MatrixType val1 = getMatrixAt(matrix1, i % matrix1.rows, j % matrix1.cols); + MatrixType val2 = getMatrixAt(matrix2, i % matrix2.rows, j % matrix2.cols); + setMatrixAt(val1 + val2, MatrixAdd, i, j); + } } + return MatrixAdd; } - return MatrixAdd; + return createMatrix(0, 0); } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { Matrix MatrixMult = createMatrix(matrix1.rows, matrix2.cols); - for (int i = 0; i < matrix1.rows; i++) + if (matrix1.cols == matrix2.rows) { - for (int j = 0; j < matrix2.cols; j++) + for (int i = 0; i < matrix1.rows; i++) { - MatrixType skalar = 0; - for (int k = 0; k < matrix1.cols; k++) + for (int j = 0; j < matrix2.cols; j++) { - MatrixType val1 = getMatrixAt(matrix1, i, k); - MatrixType val2 = getMatrixAt(matrix2, k, j); - skalar += val1 * val2; + 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); } - setMatrixAt(skalar, MatrixMult, i, j); } + return MatrixMult; } - return MatrixMult; + return createMatrix(0, 0); } \ No newline at end of file