From f34adaac304025675c926c91a83f05fc628d7f81 Mon Sep 17 00:00:00 2001 From: hofmannsv99200 Date: Fri, 28 Nov 2025 08:37:59 +0100 Subject: [PATCH] matrix.c fertiggestellt --- matrix.c | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/matrix.c b/matrix.c index 7ca67de..941f583 100644 --- a/matrix.c +++ b/matrix.c @@ -6,19 +6,47 @@ /* Erstellt eine Matrix mit den gegebenen Zeilen und Spalten */ Matrix createMatrix(unsigned int rows, unsigned int cols) { + Matrix m; + if (rows == 0 || cols == 0) + { + m.rows = 0; + m.cols = 0; + m.buffer = NULL; + return m; + } + + m.rows = rows; + m.cols = cols; + m.buffer = (MatrixType *)calloc(rows * cols, sizeof(MatrixType)); + + return m; } -/* Löscht eine Matrix */ void clearMatrix(Matrix *matrix) { - + if (matrix != NULL) + { + if (matrix->buffer != NULL) + { + free(matrix->buffer); + matrix->buffer = NULL; + } + + matrix->rows = 0; + matrix->cols = 0; + } } -/* Setzt einen Wert in die Matrix an der gegebenen Stelle */ void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if (matrix.buffer != NULL) + { + if (rowIdx < matrix.rows && colIdx < matrix.cols) + { + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; + } + } } /* Ausgeben eines Wertes aus der Matrix * matrix Die Matrix