求计算矩阵数乘,加法和行列式的c++程序

用多文件程序结构,并且能够对错误输入进行处理。最好能说明思路。谢谢大佬们

c++

1个回答

/*矩阵相乘*/
double** mulMatrix(double**a, double**b, int rowa, int cola, int colb)
{
double** mul = initializationMatrix(rowa, colb);
for (int m = 0; m < rowa; m++)
{
for (int n = 0; n < colb; n++)
{
for (int k = 0; k < cola; k++)
{
mul[m][n] += a[m][k] * b[k][n];
}
}
}
return mul;
}

/*行列式*/
double detMatrix(double**arr, int row, int col)
{
if (row != col)return -1;
int*q = new int[row];
for (int i = 0; i < row; i++) q[i] = i;
double sum = 0;
do{
int cnt = 0;
for (int i = 0; i < row; i++)
{
for (int j = row - 1; j > i; j--)
{
if (q[i]>q[j]) cnt++;
}
}
double mul = 1;
for (int m = 0; m < row; m++)mul *= arr[m][q[m]];
if (cnt % 2 == 0)sum += mul;
else sum -= mul;
} while (next_permutation(q, q + row));
return sum;
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问