From bb44ee72901f9c369b7f6de8cdfe959de586215d Mon Sep 17 00:00:00 2001 From: Kruschat Date: Tue, 11 Nov 2025 18:47:54 +0100 Subject: [PATCH] =?UTF-8?q?Matrix=20Multiplikation=20hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/editor.xml | 838 +++++++++++++++++------------------------------ matrix.c | 49 ++- 2 files changed, 339 insertions(+), 548 deletions(-) diff --git a/.idea/editor.xml b/.idea/editor.xml index 1f0ef49..963c96f 100644 --- a/.idea/editor.xml +++ b/.idea/editor.xml @@ -1,62 +1,298 @@ + \ No newline at end of file diff --git a/matrix.c b/matrix.c index 77dc207..8847000 100644 --- a/matrix.c +++ b/matrix.c @@ -70,21 +70,48 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co Matrix add(const Matrix matrix1, const Matrix matrix2) { - - //Prüfung ob größe Matrix1 und matrix2 identisch - Matrix matrix; - matrix.cols = matrix1.cols; - matrix.rows = matrix1.rows; - matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); - for (int i = 0; i < matrix1.rows; i++) { - for (int j = 0; j < matrix1.cols; j++) { - (matrix.buffer[i*matrix.cols+j])= (matrix1.buffer[i*matrix.cols+j]) + (matrix2.buffer[i*matrix.cols+j]); - } + if(matrtix1.cols != matrix2.cols || matrix1.rows != matrix2.rows) + { + return NULL; + } + else + { + Matrix matrix; + matrix.cols = matrix1.cols; + matrix.rows = matrix1.rows; + matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); + for (int i = 0; i < matrix1.rows; i++) { + for (int j = 0; j < matrix1.cols; j++) { + (matrix.buffer[i*matrix.cols+j])= (matrix1.buffer[i*matrix.cols+j]) + (matrix2.buffer[i*matrix.cols+j]); + } + } + return matrix; } - return matrix; } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { + + if(matrix1.cols == matrix2.rows) + { + Matrix matrix; + matrix.cols = matrix1.cols; + matrix.rows = matrix2.rows + matrix.buffer= (MatrixType*) malloc(matrix.rows*matrix.cols * sizeof(MatrixType)); + for(int i = 0; i < matrix1.rows; i++) + { + for(int j = 0; j < matrix2.cols; j++) + { + for(int k = 0; k < matrix1.cols; k++) + { + matrix.buffer[i*matrix.cols+j] += matrix1.buffer[i*matrix.cols+k] * matrix2.buffer[k*matrix.cols+j]; + } + } + } + } + else + { + return NULL; + } //Siehe I1-Pr Aufgabe } \ No newline at end of file