我目前正在尝试通过调用dotProduct函数来显示点积,但是我对如何执行此操作感到困惑。我试过调用该函数,但由于它不起作用而将其注释掉。显示点积的正确方法是什么?任何帮助将不胜感激!
#include <iostream>
#include <vector>
#include <ctime>
#include <iomanip>
using namespace std;
class list
{
public:
list();
void input(int s);
void output();
double dotProduct(vector <double> a,vector <double> b);
private:
vector <int> v;
};
list :: list() : v()
{
}
void list :: input(int s)
{
int t;
for(int i = 1; i <= s; i++)
{
t = rand() % 10;
v.push_back(t);
}
}
void list :: output()
{
for(unsigned int i = 0; i < v.size(); i++)
cout << v[i] << " ";
cout << endl;
}
double list :: dotProduct(vector <double> a,vector <double> b)
{
double product = 0;
if(a.size() != b.size())
{
cout << "Vectors are not the same size\n";
}
for (unsigned int i = 0; i < a.size(); i++)
{
product = product + a[i] * b[i];
}
return product;
}
int main()
{
list L1,L2,ob1;
int s;
cout << "Enter the size of list 1: \n";
cin >> s;
L1.input(s);
cout << "\nEnter the size of list 2: \n";
cin >> s;
L2.input(s);
cout << "\nVector 1: ";
L1.output();
cout << endl;
cout << "Vector 2: ";
L2.output();
cout << endl;
cout << "The dot product is: ";
//ob1.dotProduct(L1.output(),L2.output());
cout << endl;
return 0;
}