-3

我的任务是在 C++ 中制作一个矢量幅度函数,作为一个类项目的数学库的一部分,但是我不确定如何去做,如果有人可以推荐一些页面来阅读或者给我一些帮助,那就是伟大的

编辑:我的 C++ 知识不是很好,我正在寻找帮助我学习如何为向量执行函数的页面

4

2 回答 2

3

一个快速的谷歌想出了

震级

由于这是一个课堂项目,我将让您阅读链接,而不是在此处提供完整的详细信息。

您可能想阅读有关使用向量的信息,例如这里

我个人更喜欢尽可能使用 C++ 标准算法。您可以使用 std::accumulate 快速有效地完成此类事情。

#include <iostream>
#include <vector>
#include <numeric>
#include <string>
#include <functional>

int main()
{
    std::vector<double> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    double ssq = std::accumulate(v.begin(), v.end(),
                                    0.0,
                                    /* add here */);

    std::cout << "ssq: " << ssq << '\n';
}

标记的行/* add here */是您需要添加一个运算符的位置,该运算符采用当前运行的平方和以及要添加的下一个值,并返回新的运行平方和。

或者,您可以只编写一个 for 循环

double ssq = 0.0;
std::vector<double> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (auto e : v)
{
    // calculate sum of squares here
}
于 2017-11-13T14:52:34.877 回答
1

Take a revisit to GCSE maths:

c² = a² + b²

double magnitude = sqrt((Vector.x*Vector.x) + (Vector.y*Vector.y));
于 2017-11-13T16:25:09.960 回答