Calculation of the scalar product of two vectors in C ++

I am trying to write a program with the function double_product(vector<double> a, vector<double> b) , which calculates the scalar product of two vectors. Scalar product

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

Here is what I have. This is a mess, but I try!

 #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; } 
+8
c ++
source share
5 answers

If you do not need to do this yourself (for example, write this homework), you should really use the standard algorithm that is already written to do exactly what you want:

 #include <iostream> #include <numeric> int main() { double a[] = {1, 2, 3}; double b[] = {4, 5, 6}; std::cout << "The scalar product is: " << std::inner_product(std::begin(a), std::end(a), std::begin(b), 0.0); return 0; } 

Note that although begin(a) , end(a) is new in C ++ 11, std::inner_product is available with C ++ 98.

+34
source share

You can remove the class that you defined. You do not need it.

In your scalar_product function:

 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; } 

It is almost there. You do not need 2 cycles. Only one.

 double scalar_product(vector<double> a, vector<double> b) { if( a.size() != b.size() ) // error check { puts( "Error a size not equal to b size" ) ; return -1 ; // not defined } // compute double product = 0; for (int i = 0; i <= a.size()-1; i++) product += (a[i])*(b[i]); // += means add to product return product; } 

Now to call this function you need to create 2 vector objects in main() , fill them with values โ€‹โ€‹(the same number of values, of course!), And then call scalar_product( first_vector_that_you_create, second_vector_object );

+10
source share

While you have presented many solutions that work, let me deploy one more option to introduce a couple of concepts that will help you write better code:

  • class are only needed to collect data together
  • the function should check its preconditions as soon as possible, they should be documented
  • the function must have postconditions, those must be documented
  • code reuse is the cornerstone of supported programs

With that in mind:

 // Takes two vectors of the same size and computes their scalar product // Returns a positive value double scalar_product(std::vector<double> const& a, std::vector<double> const& b) { if (a.size() != b.size()) { throw std::runtime_error("different sizes"); } return std::inner_product(a.begin(), a.end(), b.begin(), 0.0); } // scalar_product 

You may decide to use the inner_product algorithm directly, but you can look at it:

  • it takes four arguments, not two
  • he does not verify that his arguments are the same size

so itโ€™s better to wrap it.

Note. I used const& to tell the compiler not to copy vectors.

+3
source share

Here is the code you should have. I see you used a class in your code that you really don't need. Let me know if the question requires you to use a class.

As a newbie, this code may scare you. So Iโ€™ll try to explain it when I go. Find comments in the code to understand what is being done, and ask if you understand.

 //Scalar.cpp #include <stdlib.h> #include <iostream> #include <vector> using namespace std; /** This function returns the scalar product of two vectors "a" and "b" */ double scalar_product(vector<double> a, vector<double> b) { //In C++, you should declare every variable before you use it. So, you declare product and initialize it to 0. double product = 0; //Here you check whether the two vectors are of equal size. If they are not then the vectors cannot be multiplied for scalar product. if(a.size()!=b.size()){ cout << "Vectors are not of the same size and hence the scalar product cannot be calculated" << endl; return -1; //Note: This -1 is not the answer, but just a number indicating that the product is not possible. Some pair of vectors might actually have a -1, but in that case you will not see the error above. } //you loop through the vectors. As bobo also pointed you do not need two loops. for (int i = 0; i < a.size(); i++) { product = product + a[i]*b[i]; } //finally you return the product return product; } //This is your main function that will be executed before anything else. int main() { //you declare two vectors "veca" and "vecb" of length 2 each vector<double> veca(2); vector<double> vecb(2); //put some random values into the vectors veca[0] = 1.5; veca[1] = .7; vecb[0] = 1.0; vecb[1] = .7; //This is important! You called the function you just defined above with the two parameters as "veca" and "vecb". I hope this cout is simple! cout << scalar_product(veca,vecb) << endl; } 

If you are using an IDE, then just compile and run. If you use the command line on a Unix-based system with the g ++ compiler, this is what you will do (where Scalar.cpp is the file containing the code):

 g++ Scalar.cpp -o scalar 

To start it, just type

 ./scalar 

You should get 1.99 as the output of the above program.

+1
source share

It seems you want to create a class specifically for vectors. The class I made in my example is adapted to 3-dimensional vectors, but you can change it to another if you wish. The class has i, j, k, but can also perform scalar products based on other MathVectors. Another vector is passed through a C ++ link. It is difficult to deduce the question, but I think that could answer it.

 #include <iostream> using namespace std; class MathVector { private: double i,j,k; public: MathVector(double i,double j,double k) { this->i=i; this->j=j; this->k=k; } double getI(){return i;} double getJ(){return j;} double getK(){return k;} double scalar(MathVector &other) { return (i*other.getI())+(j*other.getJ())+(k*other.getK()); } }; int main(int argc, char **argv) { MathVector a(1,2,5), b(2,4,1); cout << a.scalar(b) << endl; return 0; } 
0
source share

All Articles