Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/138.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 求随机数组的平均值_C++_Arrays - Fatal编程技术网

C++ 求随机数组的平均值

C++ 求随机数组的平均值,c++,arrays,C++,Arrays,我试图创建一个数组,使用某个随机种子(我已经有了)从0-9生成20个随机数,然后计算生成的随机数的平均值。我已经让它很好地执行数组,但是当我去计算随机数之和时,它返回-16。我看了其他一些不同的东西来帮助他们,他们都有我得到的相同的东西 for (i = 0; i < SIZE; i++) { sum += num[SIZE];} for(i=0;i

我试图创建一个数组,使用某个随机种子(我已经有了)从0-9生成20个随机数,然后计算生成的随机数的平均值。我已经让它很好地执行数组,但是当我去计算随机数之和时,它返回-16。我看了其他一些不同的东西来帮助他们,他们都有我得到的相同的东西

for (i = 0; i < SIZE; i++) {
            sum += num[SIZE];}
for(i=0;i
有人能指出我在这里或程序中的其他地方做错了什么吗

#include <iostream>
#include <cstdlib>

using namespace std;

int main() // Creates the array
{
    srand(2622); // random seed 

    const int SIZE = 20;
    int num[SIZE], i, sum = 0;  


    for (i = 0; i < 20; i++) {
        cout << rand() % 10 << " : "; // generates random numbers 0-10
    }
        cout << endl;

    for (i = 0; i < SIZE; i++) {
        sum += num[SIZE];

    }
        cout << sum << endl; 


    return 0;

}
#包括
#包括
使用名称空间std;
int main()//创建数组
{
srand(2622);//随机种子
常数int SIZE=20;
int num[SIZE],i,和=0;
对于(i=0;i<20;i++){
难道你在这里不见了吗--


#包括
#包括
使用名称空间std;
int main()//创建数组
{
srand(2622);//随机种子
常数int SIZE=20;
int num[SIZE],i,和=0;
对于(i=0;i<20;i++){
num[i]=rand()%10;
难道你在这里不见了吗--


#包括
#包括
使用名称空间std;
int main()//创建数组
{
srand(2622);//随机种子
常数int SIZE=20;
int num[SIZE],i,和=0;
对于(i=0;i<20;i++){
num[i]=rand()%10;

cout仅供说明,这里是使用c++14的相同程序和标准库的惯用用法

#include <iostream>
#include <numeric>
#include <random>
#include <functional>

using namespace std;

template<class Iter>
std::ostream& emit_range(std::ostream& os, Iter first, Iter last,
                         const char* inter_sep = " : ",
                         const char* terminator = "\n")
{
    auto sep = "";
    while (first != last) {
        os << sep << *first;
        ++first;
        sep = inter_sep;
    }
    return os << terminator;
}

int main() // Creates the array
{
    // function object to create a pre-seeded pseudo-random sequence
    auto next_number = [eng = std::default_random_engine(2622),
                 dist = std::uniform_int_distribution<int>(0, 9)]() mutable
    {
        return dist(eng);
    };

    constexpr int SIZE = 20;
    int num[SIZE];

    // generate the random numbers
    std::generate(std::begin(num), std::end(num), next_number);

    // compute the sum
    auto sum = std::accumulate(std::begin(num), std::end(num), 0,
                               std::plus<>());

    // compute the average
    auto average = double(sum) / SIZE;

    // print results    
    emit_range(cout, std::begin(num), std::end(num));
    cout << sum << endl;
    cout << average << endl;


    return 0;

}

为了便于说明,下面是使用c++14的相同程序和标准库的惯用用法

#include <iostream>
#include <numeric>
#include <random>
#include <functional>

using namespace std;

template<class Iter>
std::ostream& emit_range(std::ostream& os, Iter first, Iter last,
                         const char* inter_sep = " : ",
                         const char* terminator = "\n")
{
    auto sep = "";
    while (first != last) {
        os << sep << *first;
        ++first;
        sep = inter_sep;
    }
    return os << terminator;
}

int main() // Creates the array
{
    // function object to create a pre-seeded pseudo-random sequence
    auto next_number = [eng = std::default_random_engine(2622),
                 dist = std::uniform_int_distribution<int>(0, 9)]() mutable
    {
        return dist(eng);
    };

    constexpr int SIZE = 20;
    int num[SIZE];

    // generate the random numbers
    std::generate(std::begin(num), std::end(num), next_number);

    // compute the sum
    auto sum = std::accumulate(std::begin(num), std::end(num), 0,
                               std::plus<>());

    // compute the average
    auto average = double(sum) / SIZE;

    // print results    
    emit_range(cout, std::begin(num), std::end(num));
    cout << sum << endl;
    cout << average << endl;


    return 0;

}

随机数生成器只生成正数?是的,它打印出0 1 5 9 1 0 2 4 9 9 6 4 9 9 5 2 5 9打印num[大小]会很有趣在执行最后一个循环之前,编辑你的帖子并添加C++的标签,随机数生成器只生成正数,是的,它打印出0个1、5个、5个、9个、1个0、2个2 4、9个9、9、x、9、9个,这对打印Num [大小]很有意思。在执行最后一个循环之前,编辑你的帖子并添加C++ +谢谢!这是问题!使用<代码>大小<代码>所有问题都是问题!使用<代码>大小<代码>everywhere@DonReba毫无疑问。@DonReba对此毫无疑问。
#include <iostream>
#include <numeric>
#include <random>
#include <functional>

using namespace std;

template<class Iter>
std::ostream& emit_range(std::ostream& os, Iter first, Iter last,
                         const char* inter_sep = " : ",
                         const char* terminator = "\n")
{
    auto sep = "";
    while (first != last) {
        os << sep << *first;
        ++first;
        sep = inter_sep;
    }
    return os << terminator;
}

int main() // Creates the array
{
    // function object to create a pre-seeded pseudo-random sequence
    auto next_number = [eng = std::default_random_engine(2622),
                 dist = std::uniform_int_distribution<int>(0, 9)]() mutable
    {
        return dist(eng);
    };

    constexpr int SIZE = 20;
    int num[SIZE];

    // generate the random numbers
    std::generate(std::begin(num), std::end(num), next_number);

    // compute the sum
    auto sum = std::accumulate(std::begin(num), std::end(num), 0,
                               std::plus<>());

    // compute the average
    auto average = double(sum) / SIZE;

    // print results    
    emit_range(cout, std::begin(num), std::end(num));
    cout << sum << endl;
    cout << average << endl;


    return 0;

}
1 : 9 : 6 : 6 : 3 : 0 : 3 : 6 : 7 : 1 : 7 : 2 : 1 : 8 : 0 : 0 : 2 : 6 : 1 : 9
78
3.9