From 19910ab17f669f04f9134939d84b63fd32d28fc9 Mon Sep 17 00:00:00 2001 From: = Date: Thu, 20 Nov 2025 14:42:57 +0100 Subject: [PATCH] Added addition and multiplication to new version, still no functionality with accessing the matrix information --- matrix.c | 33 +++++++++++++++++++++++++++++++-- matrix.h | 4 ++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/matrix.c b/matrix.c index c8eb6aa..ec22def 100644 --- a/matrix.c +++ b/matrix.c @@ -87,10 +87,39 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2) { - + //check if the matrices are able to be added (same size) + if (matrix1.cols == matrix2.cols && matrix1.rows == matrix2.rows){ + //size of the matrices should be the same, if the addition is supposed to happen + Matrix outputMatrix = createMatrix(matrix1.rows, matrix1.cols); + for (int i = 0; i < matrix1.rows;i++) { + for (int j = 0; j < matrix1.cols; j++) { + // I dont exactly know how the input for the matrix is set, so this is only the explanation on how to do the math + // outputMatrix.buffer[i][j] = matrix1.buffer[i][j] + matrix2.buffer[i][j]; + } + } + } else { + //no matrix could be added, consequence should be defined + // return NULL; + } + } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + //check, if the matrices can be multiplied + if (matrix1.rows == matrix2.cols) { + Matrix outputMatrix = createMatrix(matrix1.rows, matrix2.cols); + for(int i = 0; i < matrix1.rows; i++) { + for (int j = 0; j < matrix2.cols; j++) { + for (int k = 0; k < matrix2.rows; k++) { + // I dont exactly know how the input for the matrix is set, so this is only the explanation on how to do the math + // outputMatrix.buffer[i][j] = matrix1.buffer[i][k] * matrix2[k][j]; + } + } + } + return outputMatrix; + } else { + //no matrix could be added, consequence should be defined + // return NULL; + } } \ No newline at end of file diff --git a/matrix.h b/matrix.h index e89fdc7..8f05f79 100644 --- a/matrix.h +++ b/matrix.h @@ -7,8 +7,8 @@ typedef float MatrixType; // TODO Matrixtyp definieren typedef struct Matrix { - size_t rows; - size_t cols; + size_t rows; //X-Element + size_t cols; //Y-Element MatrixType *buffer; } Matrix;