From 7f797957aa1730f08e0330e3bc71636e6f2dc750 Mon Sep 17 00:00:00 2001 From: Ben Skuppin Date: Mon, 17 Nov 2025 13:27:23 +0100 Subject: [PATCH] addmatrix fertig Testdatei PASS --- matrix.c | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/matrix.c b/matrix.c index b0c4786..bfc9173 100644 --- a/matrix.c +++ b/matrix.c @@ -45,7 +45,50 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2) { - return matrix1; + int matrixResultCols; + + if(matrix1.cols <= matrix2.cols){ + matrixResultCols = matrix2.cols; + } + else{ + matrixResultCols = matrix1.cols; + } + + Matrix result = createMatrix(matrix1.rows, matrixResultCols); + + if(matrix1.rows != matrix2.rows){ + + result.buffer = NULL; + result.rows = 0; + result.cols = 0; + return result; + } + + if(matrix1.cols == matrix2.cols){ + for(int i = 0; i < matrix1.rows; i++){ + for(int j = 0; j < matrix1.cols; j++){ + *(result.buffer + (matrix1.cols * i + j)) = *(matrix1.buffer + (matrix1.cols * i + j)) + *(matrix2.buffer + (matrix2.cols * i + j)) ; + } + } + } + + if((matrix1.cols ==1 && matrix2.cols != 1)){ + for(int i = 0; i < matrix2.rows; i++){ + for(int j = 0; j < matrix2.cols; j++){ + *(result.buffer + (result.cols * i + j)) = *(matrix2.buffer + (matrix2.cols * i + j)) + *(matrix1.buffer + i); + } + } + } + + if((matrix2.cols == 1 && matrix1.cols != 1)){ + for(int i = 0; i < matrix1.rows; i++){ + for(int j = 0; j < matrix1.cols; j++){ + *(result.buffer + (result.cols * i + j)) = *(matrix1.buffer + (matrix1.cols * i + j)) + *(matrix2.buffer + i); + } + } + } + + return result; } Matrix multiply(const Matrix matrix1, const Matrix matrix2)