Menu Close

Dot Product of Two Vectors C++

#include<iostream>
using namespace std;

template <class T>
class vector
{
  public:
      T* v;
      int size;

      vector(int m)
      {
          v=new T[size=m];
      }
      T dotProduct(vector &y)
      {
          T s=0;
          for (int i=0;i<size;i++)
          {
              s+=this->v[i]*y.v[i];
          }
          return s;
      }
};

int main()
{
  vector <float> v1(3);
  v1.v[0]=1.4;
  v1.v[1]=3.3;
    v1.v[2]=0.1;

  vector <float> v2(3);
  v2.v[0]=0.1;
  v2.v[1]=1.9;
  v2.v[2]=4.1;

  float a = v1.dotProduct(v2);
  cout<<a;
    return 0;

}

Output

6.82

More Related Stuff