为什么我需要使用两次这个语句——矩阵乘法
Why did I need to use this statement twice - 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 nnNumber 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 nnNumber 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;
}
}
还有一件重要的事:这不是我的家庭作业或作业!
我使用的方法非常有效,但直到我使用了两次这个语句,它才给我正确的答案。(我是通过反复试验的方法得到的)。
matrixAns[r][s] = e+matrixAns[r][s];
我还通过使用循环初始化了matrixAns
(并将其设置为0)。
我是C++的新手,想知道我犯了什么错误,以及使用这两个语句是如何给我正确答案的。
有没有办法在不破坏应用程序的情况下删除其中一条语句?
在计算答案时,您没有正确地进行点积运算。您需要将两个矩阵之间的单个单元格相乘,然后将答案中单个单元格的所有乘积相加。
您的代码只取最后一次乘法的乘积matrix[r][b - 1] * matrixB[b - 1][s]
-的e
,并丢弃第一个N-1乘积。将e
(最后一次乘法)加一次、两次或三次都是不正确的,尽管它可能会出现来处理某些输入。
你的回答循环,带评论:
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];
}
}
将您的回答循环更改为:
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;
}
}
}
您是否打印了matrixAns[r][s]以确保它能容纳一些东西?
听起来matrixAns[r][s]是空的;这就是我认为正在发生的事情:
matrixAns[r][s] = e+matrixAns[r][s]; //stores e in matrixAns[r][s]
matrixAns[r][s] = e+matrixAns[r][s]; //adds e to e (whats in matrixAns[r][s])
相关文章:
- g++的分段错误(在NaN上使用to_string两次时)
- 蛇在C++不会连续转两次
- 检查一个数组是否包含在另一个数组中,以相反的顺序,至少两次
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 我应该如何去缓解两次出现的cin?
- Realloc 两次无法在 Visual Studio 上运行
- 使用 getline(cin, var) 两次在进行字符串比较时会产生错误 (==)
- 为什么映射插入和 map.find() 的单次迭代比插入和 map.find() 的两次单独迭代慢得多
- C++析构函数调用两次,堆栈分配的复合对象
- 为什么参数在构造 std::thread 时移动两次
- Qt插槽调用了两次
- 做 std::用相同的unique_ptr移动两次
- C++两次定义相同的函数会导致错误
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 对于语句结束两次
- 在 c++ 中,如何修改运算符以便在同一语句中连续调用它两次
- 为什么我需要使用两次这个语句——矩阵乘法
- gdb执行同一语句两次
- 在循环重复两次之前,break 语句不起作用
- 如果不使用 pthread_join(),为什么'cout'语句从特定线程打印两次(即使它是同步的)?