gpt4 book ai didi

c++ - 随机整数/出现次数 (C++) 问题

转载 作者:搜寻专家 更新时间:2023-10-30 23:59:28 28 4
gpt4 key购买 nike

我目前已经在一个 C++ 问题上停留了大约一个半小时。这是问题:

编写一个程序,生成 0 到 9 之间的一百个随机整数,并显示每个数字的计数。 (提示:使用 rand()% 10 生成 0 到 9 之间的随机整数。使用十个整数的数组,说计数,以存储 O、l 的数量的计数。 .., 9.)

这就是我目前所拥有的。我想我已经很接近了,但是对于每个随机整数的出现(或计数),我一直得到“0”。任何帮助将不胜感激。

const int SIZE = 100;

int main()
{
int integers[SIZE];
int index;
int zero = 0;
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int seven = 0;
int eight = 0;
int nine = 0;

cout << "The following 100 integers are random:" << endl;
cout << endl;

srand(time(0));

for (index = 0; index < SIZE; index++)
{
integers[SIZE] = rand() % 10;
cout << integers[SIZE] << " ";
}

cout << endl;

for (index = 0; index < SIZE; index++)
{
if (integers[index] == 0)
{
zero += 1;
}
}

for (index = 0; index < SIZE; index++)
{
if (integers[index] == 1)
{
one += 1;
}
}

for (index = 0; index < SIZE; index++)
{
if (integers[index] == 2)
{
two += 1;
}
}

for (index = 0; index < SIZE; index++)
{
if (integers[index] == 3)
{
three += 1;
}
}

for (index = 0; index < SIZE; index++)
{
if (integers[index] == 4)
{
four += 1;
}
}

for (index = 0; index < SIZE; index++)
{
if (integers[index] == 5)
{
five += 1;
}
}

for (index = 0; index < SIZE; index++)
{
if (integers[index] == 6)
{
six += 1;
}
}

for (index = 0; index < SIZE; index++)
{
if (integers[index] == 7)
{
seven += 1;
}
}

for (index = 0; index < SIZE; index++)
{
if (integers[index] == 8)
{
eight += 1;
}
}

for (index = 0; index < SIZE; index++)
{
if (integers[index] == 9)
{
nine += 1;
}
}

cout << "The number of zeros in the random list are " << zero << endl;
cout << "The number of ones in the random list are " << one << endl;
cout << "The number of twos in the random list are " << two << endl;
cout << "The number of threes in the random list are " << three << endl;
cout << "The number of fours in the random list are " << four << endl;
cout << "The number of fives in the random list are " << five << endl;
cout << "The number of sixes in the random list are " << six << endl;
cout << "The number of sevens in the random list are " << seven << endl;
cout << "The number of eights in the random list are " << eight << endl;
cout << "The number of nines in the random list are " << nine << endl;

getch();

return 0;

}

最佳答案

您有未定义的行为,因为您使用 SIZE 而不是 index 作为数组的索引:

for (index = 0; index < SIZE; index++)
{
integers[SIZE] = rand() % 10;
cout << integers[SIZE] << " ";
}

这将访问数组边界之外(因为范围是从 0SIZE-1)。在循环体内,将 SIZE 更改为 index

但是,我认为它会帮助您重新阅读您的问题陈述。具体来说:

Use an array of ten integers, say counts, to store the counts for the number of 0s, 1s, ... , 9s

您改为使用数组来存储您的随机数。这根本没有必要。您不需要跟踪生成的数字。您只需将适当的计数加 1,然后就可以简单地丢弃随机数。

你应该简单地有一个名为 counts 的数组,其中 counts[0] 存储到目前为止 0 的数量, counts[1] 存储1的个数,等等。那么你就不需要这些可怕的变量了,叫做zero, one等。如果您发现自己定义了这样的变量名(其中的数字越来越多),那么您可能应该改用数组。

关于c++ - 随机整数/出现次数 (C++) 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16369032/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com