在C中乘以两个矩阵

在C中乘以两个矩阵,第1张

概述我正在编写一个程序,它接受两个不同矩阵的元素,然后它将它们相乘,接下来它将它们保存在一个多维数组中. 但它只适用于方阵. (Visual Studio 2013没有错误.) 如果你帮我编辑这段代码以便繁殖各种矩阵,我会很高兴的. int a, b, c, d; int Mat1[10][10]; int Mat2[10][10]; int Mat3[10][10]; 我正在编写一个程序,它接受两个不同矩阵的元素,然后它将它们相乘,接下来它将它们保存在一个多维数组中.
但它只适用于方阵. (Visual Studio 2013没有错误.)
如果你帮我编辑这段代码以便繁殖各种矩阵,我会很高兴的.

int a,b,c,d;    int Mat1[10][10];    int Mat2[10][10];    int Mat3[10][10];    Back1:    cout << endl << "Enter the number of rows in Matrix 1 : ";    cin >> a;    cout << endl << "Enter the number of columns in Matrix 1 : ";    cin >> b;    cout << endl << "Enter the number of rows in Matrix 2 : ";    cin >> c;    cout << endl << "Enter the number of column in Matrix 2 : ";    cin >> d;    if (b != c) {        cout << endl << "******************************************"             << "\nThis type of Matrix can't be multiplIEd . " << endl;        goto Back1;    }    for (int i = 0; i < a; i++) {        for (int j = 0; j < b; j++) {            cout << endl << "(MAT 1 ) \n\nEnter the Element of row " << i + 1                 << " column " << j + 1 << " : ";            cin >> Mat1[i][j];        }    }    for (int i = 0; i < c; i++) {        for (int j = 0; j < d; j++) {            cout << endl << "(MAT 2 ) \n\nEnter the Element of row " << i + 1                 << " column " << j + 1 << " : ";            cin >> Mat2[i][j];        }    }    for (int i = 0; i < a; i++) {        for (int j = 0; j < d; j++) {            Mat3[i][j] = Mat1[i][j] * Mat1[i][j];        }    }    for (int i = 0; i < a; i++) {        for (int j = 0; j < d; j++) {            cout << setw(4) << Mat3[i][j] << setw(4);        }        cout << endl;    }}
解决方法 你的矩阵乘法代码是错误的.代替:

for (int i = 0; i < a; i++){   for (int j = 0; j < d; j++)   {      Mat3[i][j] = Mat1[i][j] * Mat1[i][j];   }}

你需要:

for (int i = 0; i < a; i++){   for (int j = 0; j < d; j++)   {      Mat3[i][j] = 0;      for (int k = 0; k < c; k++)      {         Mat3[i][j] += Mat1[i][k] * Mat2[k][j];      }   }}
总结

以上是内存溢出为你收集整理的在C中乘以两个矩阵全部内容,希望文章能够帮你解决在C中乘以两个矩阵所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1216195.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-05
下一篇 2022-06-05

发表评论

登录后才能评论

评论列表(0条)

保存