From 33abc30d53ab40748a55db548516ba170c954e3a Mon Sep 17 00:00:00 2001 From: Thomas Rauh Desktop Date: Thu, 6 Nov 2025 18:24:20 +0100 Subject: [PATCH] Matrix Funktionen --- matrix.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++----- matrix.h | 6 +++- 2 files changed, 98 insertions(+), 9 deletions(-) diff --git a/matrix.c b/matrix.c index ad00628..edf17c8 100644 --- a/matrix.c +++ b/matrix.c @@ -4,32 +4,117 @@ // TODO Matrix-Funktionen implementieren -Matrix createMatrix(unsigned int rows, unsigned int cols) -{ - +/*Matrix createMatrix(unsigned int rows, unsigned int cols) { + Matrix matrix; + matrix.rows = rows; + matrix.cols = cols; + matrix.buffer = NULL; + + if(rows>0 && cols>0) { + matrix.buffer = (MatrixType **)malloc(rows * sizeof(MatrixType *)); // Speicher für Zeiger auf Zeilen + if(matrix.buffer == NULL) { + matrix.rows = 0; + matrix.cols = 0; + return matrix; + } + + for(int i=0;i 0 && cols > 0) { + matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); + if (matrix.buffer == NULL) { + return matrix; + } + matrix.rows = rows; + matrix.cols = cols; + memset(matrix.buffer, UNDEFINED_MATRIX_VALUE, rows*cols); + + } + + return matrix; } void clearMatrix(Matrix *matrix) { - + free(matrix->buffer); + matrix->rows = 0; + matrix->cols = 0; + matrix->buffer = NULL; + free(matrix->buffer); } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if(rowIdx<(matrix.rows) && colIdx<(matrix.cols)){ + matrix.buffer[matrix.cols*rowIdx + colIdx]=value; + } } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if(rowIdx<(matrix.rows) && colIdx<(matrix.cols)){ + return matrix.buffer[matrix.cols*rowIdx + colIdx]; + } + else{ + return UNDEFINED_MATRIX_VALUE; + } } Matrix add(const Matrix matrix1, const Matrix matrix2) { - + Matrix output = {.rows=0, .cols=0, .buffer=NULL}; + + if(matrix1.rows==matrix2.rows && matrix1.cols==matrix2.cols){ + output.rows = matrix1.rows; + output.cols = matrix1.cols; + for (int i=0;i