[英]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.我了解如何将数据存储在数值数组中,但是如果我有一个用户输入的 2 个三维向量数据,那么我如何打印这些向量的点积。 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.如果 u 和 v 是向量,则 sum 具有点积。 Its just a for loop.它只是一个 for 循环。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.