From 4893304ccdde814646fc829c3ad516f4312577c0 Mon Sep 17 00:00:00 2001 From: "dietrichsi98865@th-nuernberg.de" Date: Wed, 19 Nov 2025 12:36:06 +0100 Subject: [PATCH] =?UTF-8?q?matrix=20c=20und=20h=20grunds=C3=A4tzlich=20gem?= =?UTF-8?q?acht,=20abbruchbedingungen=20unvollst=C3=A4ndig?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- matrix.c | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++----- matrix.h | 6 ++++++ 2 files changed, 60 insertions(+), 5 deletions(-) diff --git a/matrix.c b/matrix.c index ad00628..643a04b 100644 --- a/matrix.c +++ b/matrix.c @@ -4,32 +4,81 @@ // TODO Matrix-Funktionen implementieren + Matrix createMatrix(unsigned int rows, unsigned int cols) { - + Matrix matrix; + matrix.rows = rows; + matrix.cols = cols; + if(rows == 0 || cols == 0){ + matrix.werte = NULL; + return matrix; + + } + matrix.werte = malloc(rows * cols * sizeof(MatrixType)); + return matrix; } void clearMatrix(Matrix *matrix) { - + //speicher freimachen: + free(matrix->werte); } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { + matrix.werte[rowIdx * matrix.cols + colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + + return matrix.werte[rowIdx * matrix.cols + colIdx]; + } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols){ + //nicht sicher + + + } + Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix1.cols); + + for(int reihe = 0; reihe < matrix1.rows; reihe++){ + for(int spalte = 0; spalte < matrix1.cols; spalte++){ + MatrixType wertM1 = getMatrixAt(matrix1, reihe, spalte); + MatrixType wertM2 = getMatrixAt(matrix2, reihe, spalte); + setMatrixAt(wertM1 + wertM2, ergebnisMatrix, reihe, spalte); + + } + } + return ergebnisMatrix; + } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + Matrix ergebnisMatrix = createMatrix(matrix1.rows, matrix2.cols); + for(int i = 0; i < matrix1.rows; i++){ + for(int j = 0; j < matrix2.cols; j++){ + MatrixType summe = 0; + + for(int l = 0; l < matrix1.cols; l++){ + summe += getMatrixAt( matrix1, i, l) * getMatrixAt(matrix2, l, j); + + + + } + setMatrixAt(summe, ergebnisMatrix, i, j); + + + } + + + } + return ergebnisMatrix; + } \ No newline at end of file diff --git a/matrix.h b/matrix.h index cc640d1..8205858 100644 --- a/matrix.h +++ b/matrix.h @@ -6,8 +6,14 @@ typedef float MatrixType; // TODO Matrixtyp definieren +typedef struct { + unsigned int rows; + unsigned int cols; + float *werte; +} Matrix; + Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix); void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx);