简体   繁体   中英

Storing data of two 3-D vectors and computing the dot product

I understand how to store data in a numerical array, but if I had a user input data for 2 three dimensional vectors, how could I then print the dot product of those vectors. Not a homework problem. Just was wondering how I would go about it.

for(int i = 0;i < 3;i++)
{
    sum = sum + v[i]*u[i];
}

Here sum has the dot product if u and v are the vectors. Its just a for loop.

This is what I ended up with

#include <iostream>

using namespace std;

int main()
{
    double vec1[3];
    double vec2[3];
    int i;
    double scalar = 0.0;

   cout << "Enter components of vector 1:\n";

   for(i=0;i<3;i++)
   {
       cout << "Component " << i+1 << ": ";
       cin >> vec1[i];
   }

   cout << "Enter components of vector 2:\n";
   for(i=0; i<3; i++)
   {
       cout << "Component " << i+1 << ": ";
       cin >> vec2[i];
   }
    for(i=0; i<3; i++)
    {
    scalar = scalar + (vec1[i] * vec2[i]);
    }

    cout << "The scalar product is " << scalar << endl;

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM