From 80481f037de6d413660276692fd4a56a7c3a0a92 Mon Sep 17 00:00:00 2001 From: Jens Burger Date: Thu, 13 Nov 2025 15:54:04 +0100 Subject: [PATCH] Matrix Strct erstellt/Erste Matrixfunktionen geschrieben --- matrix.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- matrix.h | 7 ++++++- 2 files changed, 51 insertions(+), 5 deletions(-) diff --git a/matrix.c b/matrix.c index ad00628..86fd5ab 100644 --- a/matrix.c +++ b/matrix.c @@ -6,22 +6,63 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { - + Matrix matrix; + + matrix.rows = rows; + matrix.cols = cols; + matrix.buffer = malloc(rows * cols * sizeof(float)); + + if(matrix.buffer == NULL) + { + printf("Fehler bei der Speicherreservierung! Keine Matrix erstellt!"); + matrix.rows = 0; + matrix.cols = 0; + } + + return matrix; } void clearMatrix(Matrix *matrix) { - + free(matrix.buffer); + + matrix->buffer = NULL; + matrix->rows = 0; + matrix->cols = 0; } void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if(matrix.buffer == NULL) + { + printf("Fehler beim Setzen! Matrix nicht initialisiert"); + return; + } + + if(rowIdx >= matrix.rows || colIdx >= matrix.cols) + { + printf("Ungueltige Indizes beim Setzen!\n"); + return; + } + + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - + if(matrix.buffer == NULL) + { + printf("Fehler beim Lesen! Matrix nicht initialisiert"); + return 0; + } + + if(rowIdx >= matrix.rows || colIdx >= matrix.cols) + { + printf("Ungueltige Indizes beim Lesen!\n"); + return 0; + } + + return matrix.buffer[rowIdx * matrix.cols + colIdx]; } Matrix add(const Matrix matrix1, const Matrix matrix2) diff --git a/matrix.h b/matrix.h index cc640d1..42879d5 100644 --- a/matrix.h +++ b/matrix.h @@ -6,7 +6,12 @@ typedef float MatrixType; // TODO Matrixtyp definieren - +typedef struct +{ + int rows; + int cols; + float *buffer; +} Matrix; Matrix createMatrix(unsigned int rows, unsigned int cols); void clearMatrix(Matrix *matrix);