MatrixMult vollständig, data in buffer umbenannt, damit passend zu MatrixTests. 5 Tests bestanden, 7 fehlgeschlagen. Fehlersuche läuft

This commit is contained in:
welschph103497 committed 2026-11-16 09:02:57 +01:00
1 parent 4a31d6388d
commit 0ee879ba20
1 file changed
+24 -5
+24 -5
View File
@@ -9,24 +9,28 @@ Matrix createMatrix(unsigned int rows, unsigned int cols)
Matrix matrix; Matrix matrix;
matrix.rows = rows; matrix.rows = rows;
matrix.cols = cols; matrix.cols = cols;
matrix.data = malloc(rows * cols * sizeof(MatrixType)); matrix.buffer = NULL;
if (rows == 0 || cols == 0)
{
return;
}
matrix.buffer = malloc(rows * cols * sizeof(MatrixType));
return matrix; return matrix;
} }
void clearMatrix(Matrix *matrix) void clearMatrix(Matrix *matrix)
{ {
free(matrix->data); free(matrix->buffer);
} }
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.data[rowIdx * matrix.cols + colIdx] = value; matrix.buffer[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)
{ {
MatrixType MatrixAt = matrix.data[rowIdx * matrix.cols + colIdx]; MatrixType MatrixAt = matrix.buffer[rowIdx * matrix.cols + colIdx];
return MatrixAt; return MatrixAt;
} }
@@ -48,4 +52,19 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix multiply(const Matrix matrix1, const Matrix matrix2) Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{ {
Matrix MatrixMult = createMatrix(matrix1.rows, matrix2.cols); Matrix MatrixMult = createMatrix(matrix1.rows, matrix2.cols);
for (int i = 0; i < matrix1.rows; i++)
{
for (int j = 0; j < matrix2.cols; j++)
{
MatrixType skalar = 0;
for (int k = 0; k < matrix1.cols; k++)
{
MatrixType val1 = getMatrixAt(matrix1, i, k);
MatrixType val2 = getMatrixAt(matrix2, k, j);
skalar += val1 * val2;
}
setMatrixAt(skalar, MatrixMult, i, j);
}
}
return MatrixMult;
} }