first fail fixed

This commit is contained in:
Simon 2025-11-08 14:59:27 +01:00
parent c560fd5241
commit 5b5e1182bd
2 changed files with 26 additions and 29 deletions

View File

@ -6,15 +6,13 @@
Matrix createMatrix(unsigned int rows, unsigned int cols) Matrix createMatrix(unsigned int rows, unsigned int cols)
{ {
Matrix m; Matrix m = {0, 0, NULL};
m.buffer = NULL; if (rows > 0 && cols > 0)
{
m.rows = rows; m.rows = rows;
m.cols = cols; m.cols = cols;
m.buffer = malloc(rows * cols * sizeof(int));
if (rows > 0 || cols > 0)
{
m.buffer = malloc (rows * cols * sizeof(int));
} }
return m; return m;
@ -23,7 +21,8 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
if (matrix == NULL) { if (matrix == NULL)
{
return; return;
} }
@ -38,21 +37,21 @@ void clearMatrix(Matrix *matrix)
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) 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 getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
MatrixType value = 0; 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 add(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = {0}; Matrix result = {0};
if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{ {
return result; return result;
} }
@ -61,21 +60,20 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
result.cols = matrix1.cols; result.cols = matrix1.cols;
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
//wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen // wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
if(result.buffer == NULL) if (result.buffer == NULL)
{ {
result.rows = result.cols = 0; result.rows = result.cols = 0;
return result; return result;
} }
//Matritzenaddition // Matritzenaddition
for (unsigned int i = 0; i < result.rows; i++) 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]; result.buffer[i * result.cols + j] = matrix1.buffer[i * matrix1.cols + j] + matrix2.buffer[i * matrix2.cols + j];
} }
} }
return result; return result;
@ -85,7 +83,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = {0}; Matrix result = {0};
if(matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols) if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{ {
return result; return result;
} }
@ -94,15 +92,15 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
result.cols = matrix1.cols; result.cols = matrix1.cols;
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType)); result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
//wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen // wenn buffer nicht allokiert werden kann dann zurücksetzen und abbrechen
if(result.buffer == NULL) if (result.buffer == NULL)
{ {
result.rows = result.cols = 0; result.rows = result.cols = 0;
return result; return result;
} }
//Matritzenmultiplikation // Matritzenmultiplikation
for (unsigned int i = 0; i < result.rows; i++) 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++)

View File

@ -11,7 +11,6 @@ typedef struct
{ {
unsigned int rows; // Anzahl der Zeilen unsigned int rows; // Anzahl der Zeilen
unsigned int cols; // Anzahl der Spalten unsigned int cols; // Anzahl der Spalten
//void *buffer;
MatrixType *buffer; // Zeiger auf die Matrixdaten MatrixType *buffer; // Zeiger auf die Matrixdaten
} Matrix; } Matrix;