请使用上述函数声明为原型写一个矩阵乘法的函数,并给出使用示例
int** multiplication(int a[][1], int aM, int aN, int b[][1], int bM, int bN) { if (aN != bM) { return NULL; } int *result = new int[aM]; for (int i = 0; i < aM; i++) { result[i] = new int[bN]; for (int j = 0; j < bN; j++) { result[i][j] = 0; for (int k = 0; k < aN; k++) { result[i][j] += a[i][k] * b[k][j]; } } } return result; }
// 使用示例 int a[2][1] = {{2}, {3}}; int b[1][3] = {{4, 5, 6}}; int** c = multiplication(a, 2, 1, b, 1, 3); // c矩阵应该为 {{8, 10, 12}, {12, 15, 18}}
原文地址: https://www.cveoy.top/t/topic/dXu 著作权归作者所有。请勿转载和采集!