From f8035cc4db7c61655181b58cc5ad66bcd5a7e6ed Mon Sep 17 00:00:00 2001 From: Jonas Date: Mon, 10 Nov 2025 20:39:35 +0100 Subject: [PATCH] =?UTF-8?q?funktionalit=C3=A4t=20erweitert=20add?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- I2_NeuronalerAbsturz/Start_Mac/matrix.c | 48 +++++++++++++++++++++---- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/I2_NeuronalerAbsturz/Start_Mac/matrix.c b/I2_NeuronalerAbsturz/Start_Mac/matrix.c index 3f5b3a5..cc40200 100644 --- a/I2_NeuronalerAbsturz/Start_Mac/matrix.c +++ b/I2_NeuronalerAbsturz/Start_Mac/matrix.c @@ -63,7 +63,7 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) { Matrix result; - if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) + if (matrix1.buffer == NULL || matrix2.buffer == NULL || matrix1.rows != matrix2.rows) { result.rows = 0; result.cols = 0; @@ -71,17 +71,51 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) return result; } - result = createMatrix(matrix1.rows, matrix1.cols); - - for (int i = 0; i < matrix1.rows; i++) + if (matrix1.cols == matrix2.cols) { - for (int j = 0; j < matrix1.cols; j++) + result = createMatrix(matrix1.rows, matrix1.cols); + for (int i = 0; i < matrix1.rows; i++) { - MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j); - setMatrixAt(value, result, i, j); + for (int j = 0; j < matrix1.cols; j++) + { + MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, j); + setMatrixAt(value, result, i, j); + } } + return result; } + if (matrix1.cols == 1 && matrix2.cols > 1) + { + result = createMatrix(matrix1.rows, matrix2.cols); + for (int i = 0; i < matrix1.rows; i++) + { + for (int j = 0; j < matrix2.cols; j++) + { + MatrixType value = getMatrixAt(matrix1, i, 0) + getMatrixAt(matrix2, i, j); + setMatrixAt(value, result, i, j); + } + } + return result; + } + else if (matrix2.cols == 1 && matrix1.cols > 1) + { + result = createMatrix(matrix1.rows, matrix1.cols); + for (int i = 0; i < matrix1.rows; i++) + { + for (int j = 0; j < matrix1.cols; j++) + { + MatrixType value = getMatrixAt(matrix1, i, j) + getMatrixAt(matrix2, i, 0); + setMatrixAt(value, result, i, j); + } + } + return result; + } + + //Fall: Unterschiedliche Spaltenanzahl, beide ungleich 1 + result.rows = 0; + result.cols = 0; + result.buffer = NULL; return result; }