From ad9a44673c3b3c28143fff058427c6e160386d94 Mon Sep 17 00:00:00 2001 From: silvana884 Date: Sun, 16 Nov 2025 18:46:46 +0100 Subject: [PATCH] Compilermeldungen versucht, zu beheben --- matrix.c | 63 +++++++++++++++++++++++++++++++------------------------- matrix.h | 4 ++-- 2 files changed, 37 insertions(+), 30 deletions(-) diff --git a/matrix.c b/matrix.c index 837251e..ce3dfd9 100644 --- a/matrix.c +++ b/matrix.c @@ -6,42 +6,48 @@ Matrix createMatrix(unsigned int rows, unsigned int cols) { - int size = rows * cols; - MatrixType *speicherbereich; - speicherbereich = (MatrixType *)calloc(size, sizeof(MatrixType) * size); - Matrix matrix = {rows, cols, &speicherbereich}; - return matrix; + Matrix matrix; + matrix.rows = rows; + matrix.cols = cols; + matrix.buffer = malloc(rows * cols * sizeof(MatrixType)); + + for (int i = 0; i < (rows * cols); i++) + matrix.buffer[i] = 0; + + return matrix; } + + void clearMatrix(Matrix *matrix) { - int rows = rows(*matrix); - int cols = cols(*matrix); - for(int i = 0; i < rows; i++) + int rowsM = matrix->rows; + int colsM = matrix->cols; + for(int i = 0; i < rowsM; i++) { - for(int j = 0; j < cols; j++) + for(int j = 0; j < colsM; j++) { - setMatrixAt(0, matrix, i, j); + matrix->buffer[i * colsM + j] = 0; } } } -void setMatrixAt(Matrix matrix, unsigned int rowIdx, unsigned int colIdx, MatrixType value) +void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - matrix->data[rowIdx * matrix->cols + colIdx] = value; + matrix.buffer[rowIdx * matrix.cols + colIdx] = value; } MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) { - return matrix->data[rowIdx * matrix->cols + colIdx]; + return matrix.buffer[rowIdx * matrix.cols + colIdx]; } Matrix add(const Matrix matrix1, const Matrix matrix2) { - rows1 = rows(matrix1); - rows2 = rows(matrix2); - cols1 = cols(matrix1); - cols2 = cols(matrix2); + int rows1 = rows(matrix1); + int rows2 = rows(matrix2); + int cols1 = cols(matrix1); + int cols2 = cols(matrix2); if((rows1 == 1 || rows2 == 1) && cols1 == cols2) //Broadcasting { if(rows1 == 1) //Wenn die erste Matrix der Vektor ist @@ -69,20 +75,20 @@ Matrix add(const Matrix matrix1, const Matrix matrix2) return addition; } else{ - return 0; + return createMatrix(0,0); } } Matrix broadcasting(const Matrix vektor, const Matrix matrix) { - int rows = rows(matrix); - int cols = cols(matrix); - Matrix result = createMatrix(rows, cols); + int rowsM = rows(matrix); + int colsM = cols(matrix); + Matrix result = createMatrix(rowsM, colsM); MatrixType wert; MatrixType koordinate; - for(int i = 0; i < rows; i++) + for(int i = 0; i < rowsM; i++) { - for(int j = 0; j < cols; j++){ + for(int j = 0; j < colsM; j++){ wert = getMatrixAt(matrix, i, j); koordinate = getMatrixAt(vektor, j, 0); setMatrixAt((koordinate + wert), result, i, j); @@ -98,7 +104,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) int rows2 = rows(matrix2); int cols2 = cols(matrix2); if(cols1 == rows2) //Bedingung fuer Multiplikation: Spalten der ersten Matrix gleich Zeilen 2. Matrix - { + { Matrix result = createMatrix(rows1, cols2); MatrixType wert1; MatrixType wert2; @@ -117,18 +123,19 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2) } setMatrixAt(add, result, i, k); add = 0; + } } - return result; - } + return result; + } return createMatrix(0,0); } int rows(const Matrix matrix) { - return matrix->rows; + return matrix.rows; } int cols(const Matrix matrix) { - return matrix->cols; + return matrix.cols; } \ No newline at end of file diff --git a/matrix.h b/matrix.h index 0beec3b..7354a46 100644 --- a/matrix.h +++ b/matrix.h @@ -9,7 +9,7 @@ typedef float MatrixType; typedef struct{ unsigned int rows; unsigned int cols; - MatrixType data[]; //Data wird in einem eindimensionalen Array gespeichert (Spalten und Reihen liegen ja im Speicher hintereinannder) + MatrixType *buffer; //Data wird in einem eindimensionalen Array gespeichert (Spalten und Reihen liegen ja im Speicher hintereinannder) } Matrix; @@ -18,7 +18,7 @@ void clearMatrix(Matrix *matrix); void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx); MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx); Matrix add(const Matrix matrix1, const Matrix matrix2); -Matrix broadcasintg(const Matrix matrix1, const Matrix matrix2); +Matrix broadcasting(const Matrix matrix1, const Matrix matrix2); Matrix multiply(const Matrix matrix1, const Matrix matrix2); int rows(const Matrix matrix); int cols(const Matrix matrix);