From ddf70dbefd9f3dbff0b74f5a7277ca795b00423d Mon Sep 17 00:00:00 2001 From: hallerni98888 Date: Sat, 8 Nov 2025 14:12:40 +0100 Subject: [PATCH] matrix.c prototype --- matrix.c | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- matrix.h | 7 +++++ 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/matrix.c b/matrix.c index ad00628..4d1fffa 100644 --- a/matrix.c +++ b/matrix.c @@ -6,30 +6,109 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { + Matrix m; + + m.rows = rows; + m.cols = cols; + + m.data = malloc (rows * cols * sizeof(int)); } void clearMatrix(Matrix *matrix) { + if (matrix == NULL) { + return; + } + + // Speicher freigeben, falls vorhanden + free(matrix->data); + matrix->data = NULL; + + // Metadaten zurücksetzen + matrix->rows = 0; + matrix->cols = 0; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + matrix.data[rowIdx * matrix.cols + colIdx] = value; //setzte Matrix auf den Wert value am Punkt (row col) } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + MatrixType value = 0; + + return value = matrix.data[rowIdx * matrix.cols + colIdx]; //hole Wert value am Punkt (row col) } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + Matrix result = {0}; + + if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) + { + return result; + } + + result.rows = matrix1.rows; + result.cols = matrix1.cols; + result.data = malloc(result.rows * result.cols * sizeof(MatrixType)); + + //wenn Data nicht allokiert werden kann dann zurücksetzen und abbrechen + if(result.data == NULL) + { + result.rows = result.cols = 0; + + return result; + } + + //Matritzenaddition + for (unsigned int i = 0; i < result.rows; i++) + { + for (unsigned int j = 0; j < result.cols; j++) + { + result.data[i * result.cols + j] = matrix1.data[i * matrix1.cols + j] + matrix2.data[i * matrix2.cols + j]; + + } + } } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - + Matrix result = {0}; + + if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) + { + return result; + } + + result.rows = matrix1.rows; + result.cols = matrix1.cols; + result.data = malloc(result.rows * result.cols * sizeof(MatrixType)); + + //wenn Data nicht allokiert werden kann dann zurücksetzen und abbrechen + if(result.data == NULL) + { + result.rows = result.cols = 0; + + return result; + } + + //Matritzenmultiplikation + for (unsigned int i = 0; i < result.rows; i++) + { + for (unsigned int j = 0; j < result.cols; j++) + { + MatrixType sum = 0; + for (unsigned int k = 0; k < matrix1.cols; k++) + { + sum += matrix1.data[i * matrix1.cols + k] * matrix2.data[k * matrix2.cols + j]; + } + result.data[i * result.cols + j] = sum; + } + } + + return result; } \ No newline at end of file diff --git a/matrix.h b/matrix.h index cc640d1..c53e546 100644 --- a/matrix.h +++ b/matrix.h @@ -7,6 +7,13 @@ typedef float MatrixType; // TODO Matrixtyp definieren +typedef struct +{ + unsigned int rows; // Anzahl der Zeilen + unsigned int cols; // Anzahl der Spalten + MatrixType *data; // Zeiger auf die Matrixdaten +} Matrix; + Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix);