From 8722f104a65ea425e1b65310ec82688f0a493713 Mon Sep 17 00:00:00 2001 From: Giorgi Kesidis Date: Mon, 10 Nov 2025 19:54:24 +0100 Subject: [PATCH] matrix fertig --- matrix.c | 42 ++++++++++++++++++++++++++++++++++++------ matrix.h | 7 +++++++ 2 files changed, 43 insertions(+), 6 deletions(-) diff --git a/matrix.c b/matrix.c index ad00628..289922f 100644 --- a/matrix.c +++ b/matrix.c @@ -5,31 +5,61 @@ // TODO Matrix-Funktionen implementieren Matrix createMatrix(unsigned int rows, unsigned int cols) -{ - +{ + Matrix matrix; + matrix.rows = rows; + matrix.cols = cols; + + matrix.data = (MatrixType *)malloc(rows* cols* sizeof(MatrixType)); + + if(matrix.data == NULL){ + + matrix.rows = 0; + matrix.cols = 0; + + return matrix; + } + + for (int i = 0; i < rows; i++) + { + for (int j = 0; j < cols; j++) + { + matrix.data[i * matrix.cols + j] = UNDEFINED_MATRIX_VALUE; + } + } } void clearMatrix(Matrix *matrix) { + if(matrix->data != NULL) + { + free(matrix->data); + matrix->data = NULL; + } + + matrix->rows = 0; + matrix->cols =0; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if(rowIdx >= matrix.rows || colIdx >= matrix.cols) + { + fprintf(stderr, "Fehler: Ungültiger Index (%u, %u) bei Matrixgröße %u x %u\n", rowIdx, colIdx, matrix.rows, matrix.cols); + } + + matrix.data[rowIdx * matrix.cols +colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - } Matrix add(const Matrix matrix1, const Matrix matrix2) { - } Matrix multiply(const Matrix matrix1, const Matrix matrix2) { - } \ No newline at end of file diff --git a/matrix.h b/matrix.h index cc640d1..e10b794 100644 --- a/matrix.h +++ b/matrix.h @@ -7,6 +7,13 @@ typedef float MatrixType; // TODO Matrixtyp definieren +typedef struct Matrix { + unsigned int rows; + unsigned int cols; + MatrixType *data; +} Matrix; + + Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix);