Nick Haller 2025-11-08 15:28:39 +01:00
commit 42e92f278f
2 changed files with 11 additions and 9 deletions

View File

@ -6,14 +6,12 @@
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;
if (rows > 0 || cols > 0)
{
m.buffer = malloc(rows * cols * sizeof(int)); m.buffer = malloc(rows * cols * sizeof(int));
} }
@ -46,7 +44,12 @@ MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int co
{ {
MatrixType value = 0; 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) Matrix add(const Matrix matrix1, const Matrix matrix2)
@ -85,7 +88,7 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix result = {0}; Matrix result = {0};
if (matrix1.cols != matrix2.rows) if (matrix1.rows != matrix2.rows || matrix1.cols != matrix2.cols)
{ {
return result; return result;
} }

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;