From 5b5e1182bdeeb1a808f6899ebcdd2b863ed6d8a7 Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 8 Nov 2025 14:59:27 +0100 Subject: [PATCH 1/2] first fail fixed --- matrix.c | 54 ++++++++++++++++++++++++++---------------------------- matrix.h | 1 - 2 files changed, 26 insertions(+), 29 deletions(-) diff --git a/matrix.c b/matrix.c index 3270845..a2786d6 100644 --- a/matrix.c +++ b/matrix.c @@ -6,24 +6,23 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { - Matrix m; + Matrix m = {0, 0, NULL}; - m.buffer = NULL; - m.rows = rows; - m.cols = cols; - - if (rows > 0 || cols > 0) + if (rows > 0 && cols > 0) { - m.buffer = malloc (rows * cols * sizeof(int)); + m.rows = rows; + m.cols = cols; + m.buffer = malloc(rows * cols * sizeof(int)); } - + return m; } void clearMatrix(Matrix *matrix) { - - if (matrix == NULL) { + + if (matrix == NULL) + { return; } @@ -38,21 +37,21 @@ void clearMatrix(Matrix *matrix) void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - matrix.buffer[rowIdx * matrix.cols + colIdx] = value; //setzte Matrix auf den Wert value am Punkt (row col) + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; // setzte Matrix auf den Wert value am Punkt (row col) } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { MatrixType value = 0; - return value = matrix.buffer[rowIdx * matrix.cols + colIdx]; //hole Wert value am Punkt (row col) + return value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col) } Matrix add(const Matrix matrix1, const Matrix matrix2) { Matrix result = {0}; - - if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) + + if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { return result; } @@ -61,21 +60,20 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) result.cols = matrix1.cols; result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); - //wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen - if(result.buffer == NULL) + // wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen + if (result.buffer == NULL) { result.rows = result.cols = 0; return result; } - //Matritzenaddition - for (unsigned int i = 0; i < result.rows; i++) + // Matritzenaddition + for (unsigned int i = 0; i < result.rows; i++) { - for (unsigned int j = 0; j < result.cols; j++) + for (unsigned int j = 0; j < result.cols; j++) { result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j]; - } } return result; @@ -84,8 +82,8 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2) { Matrix result = {0}; - - if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) + + if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) { return result; } @@ -94,21 +92,21 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) result.cols = matrix1.cols; result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); - //wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen - if(result.buffer == NULL) + // wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen + if (result.buffer == NULL) { result.rows = result.cols = 0; return result; } - //Matritzenmultiplikation - for (unsigned int i = 0; i < result.rows; i++) + // Matritzenmultiplikation + for (unsigned int i = 0; i < result.rows; i++) { - for (unsigned int j = 0; j < result.cols; j++) + for (unsigned int j = 0; j < result.cols; j++) { MatrixType sum = 0; - for (unsigned int k = 0; k < matrix1.cols; k++) + for (unsigned int k = 0; k < matrix1.cols; k++) { sum += matrix1.buffer[i * matrix1.cols + k] * matrix2.buffer[k * matrix2.cols + j]; } diff --git a/matrix.h b/matrix.h index 595ac23..fb042ea 100644 --- a/matrix.h +++ b/matrix.h @@ -11,7 +11,6 @@ typedef struct { unsigned int rows; // Anzahl der Zeilen unsigned int cols; // Anzahl der Spalten - //void *buffer; MatrixType *buffer; // Zeiger auf die Matrixdaten } Matrix; From ebff958c4e2e81783eeecc81241ad6ec2470cfea Mon Sep 17 00:00:00 2001 From: Simon Date: Sat, 8 Nov 2025 15:05:29 +0100 Subject: [PATCH 2/2] 2nd fail fixed --- matrix.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/matrix.c b/matrix.c index a2786d6..1f6755a 100644 --- a/matrix.c +++ b/matrix.c @@ -44,7 +44,12 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co { MatrixType value = 0; - return value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col) + if (rowIdx < matrix.rows && colIdx < matrix.cols) + { + value = matrix.buffer[rowIdx * matrix.cols + colIdx]; // hole Wert value am Punkt (row col) + } + + return value; } Matrix add(const Matrix matrix1, const Matrix matrix2)