简体   繁体   English

存储两个 3-D 向量的数据并计算点积

[英]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.

相关问题 用C ++计算两个向量的标量积 - Computing the scalar product of two vectors in C++ 通过函子和STL算法得到两个向量的点积 - Get the dot product of two vectors by functors and STL algorithms 使用点积计算两个向量之间的角度 - Calculating the Angle Between Two vectors Using Dot Product 计算两个3d向量之间的角度-如何实现 - computing angle between two 3d vectors - how to implement C ++-3-d数据结构-我应该使用指针向量还是向量向量? - C++ - A 3-d data structure - should I use vector of pointers or vector of vectors? 2 个向量 C++ 的点积 - Dot product of 2 vectors C++ 复杂向量与openMP的点积 - dot product of complex vectors with openMP 这是使用点积查找两个向量之间的角度的正确方法吗? C ++ SFML - Is this the correct method of using dot product to find the angle between two vectors? C++ SFML 两个单精度浮点向量的点积在CUDA内核中产生的结果与在主机上的结果不同 - Dot product of two single-precision floating point vectors yields different results in CUDA kernel than on the host 如何权衡速度精度来评估C ++中两个向量的点积的符号? (不是硬件特定的) - How to tradeoff precision for speed to evaluate sign of dot product of two vectors in C++? (Not hardware specific)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM