问题描述
我开发一个程序来找到矩阵乘法。
I was developing a program to find matrix multiplication.
#include <iostream>
using namespace std;
int main()
{
int a=0,b=0,c=0,d=0,e=0;
cout<<"Enter the order of the first matrix A \n\nNumber of Rows : ";
cin>>a;
cout<<"\nNumber of Columns : ";
cin>>b;
cout<<endl;
int matrixA[a][b];
cout<<"Enter the matrix Elements "<<endl;
for(int m=0; m<a; m++)
{
for(int n=0; n<b; n++)
{
cout<<"A ("<< m+1 <<" , "<<n+1<<" ) =";
cin>>matrixA[m][n];
//cout<<",";
}
cout<<endl;
}
////////////////////////// Startup
cout<<"Enter the order of the Second matrix A \n\nNumber of Rows : "<<b;
c=b;
cout<<"\nNumber of Columns : ";
cin>>d;
cout<<endl;
int matrixB[c][d];
cout<<"Enter the matrix Elements "<<endl;
for(int p=0; p<c; p++)
{
for(int q=0; q<d; q++)
{
cout<<"B ("<< p+1 <<" , "<<q+1<<" ) =";
cin>>matrixB[p][q];
//cout<<",";
}
cout<<endl;
}
///////////// initialisting matrixAns
int matrixAns[a][d];
for(int p=0; p<a; p++)
{
for(int q=0; q<d; q++)
{
matrixAns[p][q]=0;
}
}
//////////////////// finding ans
for(int r=0; r<a; r++)
{
for(int s=0; s<d; s++)
{
for(int t=0; t<b; t++)
{
e = matrixA[r][t]*matrixB[t][s];
}
matrixAns[r][s] = e+matrixAns[r][s];
matrixAns[r][s] = e+matrixAns[r][s]; //dont know why i have to add this same code again
}
}
////////////////////// Ans Printing
cout<<"\nMatrix Multiplication Answer \n"<<endl;
for(int h=0; h<a; h++)
{
for(int i=0; i<d; i++)
{
cout<<" "<<matrixAns[h][i]<<" ";
}
cout<<endl;
}
}
和一件重要的事情:这不是我的作业或作业
And one important thing: this is not my homework or assignment!
这是我使用的方法完美的作品,但它并没有给我正确的答案,直到我用这句话两次。 (我用试错法得到了这一点)。
The method that I have used works perfectly but it does not give me the right answer until I use this statement twice. (I got this by a trial and error method).
matrixAns[r][s] = e+matrixAns[r][s];
我也initailised的 matrixAns
通过使用一个循环(,把它设置为0)。
I have also initailised the matrixAns
by using a loop (and had set it to 0).
我是新的C ++和想知道的是,我得到和使用两个语句怎么给我正确的答案错误。
I am new to C++ and want to know what was the error that I got and how the use of the two statements gives me correct answer.
有没有办法摆脱语句之一的,不破坏应用程序?
Is there any way to get rid of one of the statements without destroying the app?
推荐答案
计算你的答案时,你不是在做正确的点产品。你需要乘两个矩阵之间的单个细胞,再总结的所有这些产品在你的答案单个单元格。
You're not doing the dot products correctly when calculating your answer. You need to multiply individual cells between the two matrices, then sum all those products for an individual cell in your answer.
您code仅服用该产品,在电子
的末页的乘法 - 矩阵[ R] [b - 1] * matrixB [b - 1] [S]
- 并丢弃第一的 N-1 的产品。添加电子
- 最后乘法 - 一次,两次或三次都是不正确,虽然它可能的显示的与特定输入工作。
Your code is only taking the product, in e
, of the last multiplication - matrix[r][b - 1] * matrixB[b - 1][s]
- and discarding the first N-1 products. Adding e
- this last multiplication - once, or twice or 3 times are all incorrect, though it may appear to work with certain inputs.
您的答案回路,搭配点评:
Your answer loop, with comments:
for(int r=0; r<a; r++)
{
for(int s=0; s<d; s++)
{
for(int t=0; t<b; t++)
{
e = matrixA[r][t]*matrixB[t][s];
}
// now e only has the value from that final multiplication, of
// matrix[r][b - 1] * matrixB[b - 1][s]. All of the other
// products were lost.
// so now it doesn't matter how many times you add e, you'll
// get the wrong product:
matrixAns[r][s] = e+matrixAns[r][s];
matrixAns[r][s] = e+matrixAns[r][s];
}
}
你的答案循环更改为:
Change your answer loop to:
for(int r=0; r<a; r++)
{
for(int s=0; s<d; s++)
{
for(int t=0; t<b; t++)
{
e = matrixA[r][t]*matrixB[t][s];
// accumulation of the products should be INSIDE the loop:
matrixAns[r][s] = matrixAns[r][s] + e;
}
}
}
这篇关于为什么我需要两次使用此语句 - 矩阵乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!