added if clause to check for fittig matrix dimensions

This commit is contained in:
Lukas Weber 2025-11-12 12:56:34 +01:00
parent 6124b38e8d
commit 8d4b56f703

View File

@ -39,22 +39,23 @@ Matrix add(const Matrix matrix1, const Matrix matrix2)
Matrix multiply(const Matrix matrix1, const Matrix matrix2)
{
if (matrix1.rows == matrix2.cols) {
Matrix result;
result.rows = matrix1.rows;
result.cols = matrix2.cols;
Matrix result;
result.rows = matrix1.rows;
result.cols = matrix2.cols;
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
result.buffer = malloc(result.rows * result.cols * sizeof(MatrixType));
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
MatrixType value = 0;
for(int k = 0; k < matrix1.cols; k++) {
value += matrix1[i][k] * matrix2[k][j];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
MatrixType value = 0;
for(int k = 0; k < matrix1.cols; k++) {
value += matrix1[i][k] * matrix2[k][j];
}
result.buffer[i][i] = value;
}
result.buffer[i][i] = value;
}
return result;
}
return result;
printf("Die angegebenen Matrizen haben keine passenden Dimensionen für die Multiplikation");
return NULL;
}