我如何...计算结构数组的点积 [英] How do i...calculate the dot product of an array of structure
问题描述
我正在尝试编写计算两个给定向量的点积的C ++程序。在向量a和b中,只有非零元素将存储到结构数组(行和列)中。每次我得到不相关的结果。正确的结果应该是50.请建议。
提前谢谢你。
I am trying write C++ programs that compute the dot product of two given vectors. In vectors a and b only nonzero elements will be stored into array of structures(row and col). Each time I am getting irrelevant results. The correct result should be 50. Please advice.
Thank you in advance.
<pre>#include <iostream>
#include <vector>
using namespace std;
const int n=10; /* vector size */
struct element {
int index; /* original index of non-zero array element */
int value ; /* integer non-zero value at index x */
};
element row[n];
element col[n];
vector<int> a={0,0,7,0,5,0,0,8,0,4,-1};
vector<int> b={0,0,0,5,6,0,0,0,0,5,-1};
void generate_row_and_col()
{
for (int i=0; i<=n; i++)
{
if(a[i]!=0)
{
row[i].index=i;
row[i].value=a[i];
}
}
for (int j=0; j<=n; j++)
{
if(b[j]!=0)
{
col[j].index=j;
col[j].value=b[j];
}
}
}
int dotproduct()
{
/* calculate the dot product of row and col output the result*/
int i=0;
int j=0;
int product=0;
while(row[i].index!=-1 && col[j].index!=-1)
{
if(row[i].index == col[j].index)
{
product=product+row[i].value*col[j].value;
i++;
j++;
}
else if(row[i].index<col[j].index)
{
i++;
}
else
{
j++;
}
}
return product;
}
int main()
{
generate_row_and_col() ;
int r;
r=dotproduct();
cout<<"result="<<r<<endl;
return 0;
}
我的尝试:
我想我无法正确地将向量读入结构数组。
What I have tried:
I think i am not able to read the vectors properly into array of structures.
推荐答案
代码中有三个错误。
前两个:对于row
和col
但是正在访问11个元素(越界访问)并且没有初始化所有元素:
There are three errors in your code.
The first two ones: You have a fixed array size of 10 forrow
andcol
but are accessing 11 elements (out of bound access) and did not initialise all elements:
for (int i=0; i<=n; i++)
{
// Not all row elements are initialised
if(a[i]!=0)
{
// Out of bound access here when i == 10
row[i].index=i;
row[i].value=a[i];
}
}
// And similar for col
for (int j=0; j<=n; j++)
{
if(b[j]!=0)
{
col[j].index=j;
col[j].value=b[j];
}
}
下一个是:
The next is here:
while(row[i].index!=-1 && col[j].index!=-1)
在这里检查 index
成员为-1。但情况永远不会是这样,因为索引只是从0运行到n(假设所有元素都已初始化)。你可能想在这里检查 -1
的值成员。
所以你必须:
- 将
n
设为11
- 将向量中的所有元素复制到
row
和col
(可选地检查数组大小并在-1之后中断已复制)
- 检查
值
成员是否为-1而不是索引
member(或者当vector元素为-1时,将上面步骤中的两个成员设置为-1)
- Set
n
to 11
- Copy all elements from the vectors to
row
andcol
(optionally check for the array size and break after -1 has been copied)
- Check the
value
member for being -1 instead of theindex
member (or set both members in the above step to -1 when the vector element is -1)
where you check the index
member for being -1. But that will never be the case because the index is just running from 0 to n (assuming all elements has been initialised). You might want to check the value member for -1
here.
So you would have to:
你试图让一个简单的问题复杂化。尝试
You are trying to complicate a simple matter. Try
#include <iostream>
#include <vector>
using namespace std;
// Please note: error handling (e.g. a.size()!=b.size()) left as exercise
int dot(const vector<int> & a, const vector <int> &b)
{
int dp = 0;
for (size_t n = 0; n< a.size(); ++n)
{
dp += a[n]*b[n];
}
return dp;
}
int main()
{
vector<int> a={0,0,7,0,5,0,0,8,0,4};
vector<int> b={0,0,0,5,6,0,0,0,0,5};
int dp = dot(a,b);
cout << "result = " << dp << endl;
}
这篇关于我如何...计算结构数组的点积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!