GetMatrixAt und SetMatrixAt korrigiert, Zeilen statt Spalten benutzt, jetzt korrekt

This commit is contained in:
silvana884 2025-11-16 17:54:36 +01:00
parent 062f6c541d
commit a767bc6cd8

View File

@ -26,14 +26,14 @@ void clearMatrix(Matrix *matrix)
} }
} }
void setMatrixAt(MatrixType value, Matrix matrix, unsigned int rowIdx, unsigned int colIdx) void setMatrixAt(Matrix matrix, unsigned int rowIdx, unsigned int colIdx, MatrixType value)
{ {
matrix -> data[rowIdx * rows + colIdx] = value; matrix->data[rowIdx * matrix->cols + colIdx] = value;
} }
MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx) MatrixType getMatrixAt(const Matrix matrix, unsigned int rowIdx, unsigned int colIdx)
{ {
return (matrix->data[rowIdx * rows + colIdx]); return matrix->data[rowIdx * matrix->cols + colIdx];
} }
Matrix add(const Matrix matrix1, const Matrix matrix2) Matrix add(const Matrix matrix1, const Matrix matrix2)
@ -97,26 +97,30 @@ Matrix multiply(const Matrix matrix1, const Matrix matrix2)
int cols1 = cols(matrix1); int cols1 = cols(matrix1);
int rows2 = rows(matrix2); int rows2 = rows(matrix2);
int cols2 = cols(matrix2); int cols2 = cols(matrix2);
if(rows1 == cols2) //Bedingung fuer Multiplikation: Zeilen der ersten Matrix gleich Spalten 2. Matrix if(cols1 == rows2) //Bedingung fuer Multiplikation: Spalten der ersten Matrix gleich Zeilen 2. Matrix
{ {
Matrix result = createMatrix(rows1, cols2); Matrix result = createMatrix(rows1, cols2);
MatrixType wert1; MatrixType wert1;
MatrixType wert2; MatrixType wert2;
MatrixType addition; MatrixType mul;
MatrixType add = 0;
for(int i = 0; i < rows1; i++) for(int i = 0; i < rows1; i++)
{ {
for(int j = 0; j < cols2; j++) for(int k = 0; k < cols2; k++)
{
for(int j = 0; j < cols1; j++)
{ {
wert1 = getMatrixAt(matrix1, i, j); wert1 = getMatrixAt(matrix1, i, j);
wert2 = getMatrixAt(matrix2, j, i); wert2 = getMatrixAt(matrix2, j, k);
addition += wert1 + wert2; mul = wert1 * wert2;
setMatrixAt(addition, result, i,j); add += mul;
} }
addition = 0; setMatrixAt(add, result, i, k);
add = 0;
} }
return result; return result;
} }
return 0; return createMatrix(0,0);
} }
int rows(const Matrix matrix) int rows(const Matrix matrix)