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