From da0dddd97593ddd0062c7b5f78afbfb24a2f7e65 Mon Sep 17 00:00:00 2001 From: pvtrx Date: Fri, 14 Nov 2025 21:15:48 +0100 Subject: [PATCH] bit of vibe coding in matrix.c --- matrix.c | 36 ++++++++++++++++++++++++++++++++---- matrix.h | 7 ++++++- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/matrix.c b/matrix.c index ad00628..b2c2e3b 100644 --- a/matrix.c +++ b/matrix.c @@ -16,20 +16,48 @@ void clearMatrix(Matrix *matrix) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + matrix.data[rowIdx][colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + MatrixType value = matrix.data[rowIdx][colIdx]; + return value; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + Matrix invalidResult = {0}; + + if ( + matrix1.rows == 0 || matrix1.cols == 0 || matrix2.rows == 0 || matrix2.cols == 0 || + matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols || + matrix1.data == NULL || matrix2.data == NULL + ) + { + return invalidResult; + } + + Matrix result = createMatrix(matrix1.rows, matrix1.cols); + if (result.data == NULL) + return invalidResult; + + for (unsigned int row = 0; row < matrix1.rows; row++) + { + MatrixType *dstRow = result.data[row]; + MatrixType *row1 = matrix1.data[row]; + MatrixType *row2 = matrix2.data[row]; + + for (unsigned int col = 0; col < matrix1.cols; col++) + { + dstRow[col] = row1[col] + row2[col]; + } + } + + return result; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { -} \ No newline at end of file +} diff --git a/matrix.h b/matrix.h index cc640d1..6cf95ae 100644 --- a/matrix.h +++ b/matrix.h @@ -5,7 +5,12 @@ typedef float MatrixType; -// TODO Matrixtyp definieren +typedef struct +{ + MatrixType **data; + unsigned int rows; + unsigned int cols; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols);