Computing the scalar product of two vectors in C++

Posted by HowardRoark on Stack Overflow See other posts from Stack Overflow or by HowardRoark
Published on 2012-06-06T04:03:04Z Indexed on 2012/06/06 4:40 UTC
Read the original article Hit count: 136

Filed under:

I am trying to write a program with a function double_product(vector<double> a, vector<double> b) that computes the scalar product of two vectors. The scalar product is

$a_{0}b_{0}+a_{1}b_{1}+...+a_{n-1}b_{n-1}$.

Here is what I have. It is a mess, but I am trying!

#include <iostream>
#include <vector>

using namespace std;

class Scalar_product
{
    public:
    Scalar_product(vector<double> a, vector<double> b);
};
double scalar_product(vector<double> a, vector<double> b)
{
    double product = 0;
    for (int i = 0; i <= a.size()-1; i++)
        for (int i = 0; i <= b.size()-1; i++)
            product = product + (a[i])*(b[i]);
    return product;
}

int main() {
    cout << product << endl;
    return 0;
}

© Stack Overflow or respective owner

Related posts about c++