C++ 使用C+中随机生成的数字进行计算+;

C++ 使用C+中随机生成的数字进行计算+;,c++,C++,所以我应该在1-100之间生成100个随机数。其中20个假设在1-100范围内生成,另外80个假设在40-70范围内生成。我还应该创建函数来计算数字的平均值和标准差 请记住,这应该在不使用数组的情况下完成 我的问题是,我不知道如何让我的程序记住生成的数字,以便我可以将它们用于计算功能。我现在所能做的就是通过使用循环将它们全部打印出来。我不知道如何使生成的数字可用于计算 有什么建议吗?我的方法对吗 #包括 #包括 #包括 #包括 使用名称空间std; int getOutlierScore();

所以我应该在1-100之间生成100个随机数。其中20个假设在1-100范围内生成,另外80个假设在40-70范围内生成。我还应该创建函数来计算数字的平均值和标准差

请记住,这应该在不使用数组的情况下完成

我的问题是,我不知道如何让我的程序记住生成的数字,以便我可以将它们用于计算功能。我现在所能做的就是通过使用循环将它们全部打印出来。我不知道如何使生成的数字可用于计算

有什么建议吗?我的方法对吗


#包括
#包括
#包括
#包括
使用名称空间std;
int getOutlierScore();
int getNormalScore();
int main()
{
srand(时间(空));
库特

这应该给你一个开始。你不需要存储任何东西。只需使用带有clk(r)的Statisics对象输入100个随机值。完成后,你可以调用ave()和std()来获得结果。

好吧,我不想将答案发布到你的家庭作业中。我只是指导你如何实现这一点

到目前为止,你们做得很好。首先,要计算标准偏差,你们需要先找到平均值。这意味着你们必须计算平均值

现在,不需要数组来计算一组数字的平均值。在变量中跟踪这些随机值的总和,例如
sum

您必须将函数
getOutlierScore()
getNormalScore()
中的所有值添加到此变量中

因此,在循环结束时,您将获得所提供条件下的总和“其中20个应在1-100范围内生成,其余80个应在40-70范围内。”

现在你要做的就是找到平均值,然后是标准差。 如果你陷入困境,请告诉我。(练习将帮助你学习,祝你好运!!)

编辑:
正如@Doug所指出的,你也需要平方和。所以在另一个变量中跟踪它。

我完全忘记了,你实际上可以计算标准偏差,而不需要实际存储数字。但是如果你想存储它们,例如提示它们,你可以使用链表。这不是一个数组,因此应该是al洛维德

一个简单的链表(有更多优雅的解决方案,但这一个相当简单)可以如下所示:

struct Node
{
    int val;
    Node* next;

    Node(int val)
    {
        this->val = val;
        next = NULL;
    }

    void add(int val)
    {
        Node* cur = this;
        while (cur->next)
        {
            cur = cur->next;
        }
        cur->next = new Node(val);
    }

    void deleteFromHere() // delete all Nodes after this one
    {
        while (next)
        {
            Node *cur = this;
            Node * last;
            while (cur->next)
            {
                last = cur;
                cur = cur->next;
            }
            delete last->next;
            last->next = NULL;
        }
    }
};
并像这样使用它:

int main(void)
{
    Node head(getOutlierScore());

    for (int i = 0; i < 19; i++)
    {
        head.add(getOutlierScore());
    }

    for (int i = 0; i < 80; i++)
    {
        head.add(getNormalScore());
    }

    Node* cur = &head;
    while (cur->next)
    {
        cout << cur->val << ". ";
        cur = cur->next;
    }
    cout << endl;

    // calculate standard deviation and average here

    head.deleteFromHere();
}

如果我们为你做作业,我们会得到巧克力吗?不是让任何人为我做这项工作,我只是问是否有人能给我一个不使用数组将这些值传递到计算函数的方法,你是否可以使用
std::vector
?你需要以某种方式存储这些数字。至少据我所知。所以你可以写一个简单的链表,那么就不需要使用数组或类似的东西。如果你使用C++(你是,应该是),那就是晚餐。。与传统的
rand/srand/srand
hijinks相比,它是猫的胡子。还需要跟踪每个随机值的平方和。@道格,谢谢,我编辑了它。我猜我年纪大了,我几乎忘记了标准差的公式:)你是说
getOutlierScore()
getNormalScore()
;)@muXXmit2X哈哈哈,该死的复制粘贴:)
struct Node
{
    int val;
    Node* next;

    Node(int val)
    {
        this->val = val;
        next = NULL;
    }

    void add(int val)
    {
        Node* cur = this;
        while (cur->next)
        {
            cur = cur->next;
        }
        cur->next = new Node(val);
    }

    void deleteFromHere() // delete all Nodes after this one
    {
        while (next)
        {
            Node *cur = this;
            Node * last;
            while (cur->next)
            {
                last = cur;
                cur = cur->next;
            }
            delete last->next;
            last->next = NULL;
        }
    }
};
int main(void)
{
    Node head(getOutlierScore());

    for (int i = 0; i < 19; i++)
    {
        head.add(getOutlierScore());
    }

    for (int i = 0; i < 80; i++)
    {
        head.add(getNormalScore());
    }

    Node* cur = &head;
    while (cur->next)
    {
        cout << cur->val << ". ";
        cur = cur->next;
    }
    cout << endl;

    // calculate standard deviation and average here

    head.deleteFromHere();
}
int GetRandom(int min, int max)
{
    static random_device rd; // create random device only once per program run
    mt19937 mt(rd()); // create number generator

    uniform_int_distribution<int> dist(min, max); // define range for the values [min;max]
    return dist(mt); // create a random value in range
}