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.
Asked
Active
Viewed 35 times
-2
-
3By writing code and applying the (mathematical) operation? Also how is this specific to C++? – UnholySheep Oct 03 '17 at 14:15
-
I'm learning c++ – Patrick Moloney Oct 03 '17 at 14:16
-
1Sounds like you could benefit from reading one of [these C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if it is C++ you want to learn. – Ron Oct 03 '17 at 14:16
-
Thanks Ron fantastic thread. – Patrick Moloney Oct 03 '17 at 14:20
2 Answers
1
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.

gst1502
- 306
- 1
- 10
-1
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;
}

Patrick Moloney
- 105
- 4