2013-05-03 30 views
2

我一直被困在C++问题上大约一个半小时。这里的问题是:随机整数/事件(C++)问题

编写一个程序,生成0到9之间的一百个随机整数,并显示每个数字的计数。 (提示:使用rand() %10生成一个介于0和9之间的随机整数。使用一个由10个整数组成的数组,012次表示计数,以存储O的个数,1,...,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; 

} 

回答

7

你有不确定的行为,因为你使用SIZE作为索引你的阵列,而不是index

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

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

但是,我认为这会帮助你重读你的问题陈述。具体做法是:

使用十个整数数组,说counts,存储计数0的个数,1秒,...,787-9

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

您应该只是有一个数组称为counts,其中counts[0]门店0 S的数量到目前为止,counts[1]1 S,数量等,然后你不需要这些可怕的变量称为zeroonetwo,等等。如果你发现自己定义了这样的变量名(随着数字的增加),那么你应该使用数组来代替。

+0

谢谢,这很有帮助 – jtarr523 2013-05-03 23:46:29

+1

@ jtarr523请记住,只要你一遍又一遍地复制相同的代码,你应该重新考虑你在做什么。 – 2013-05-03 23:47:58

+0

@ jtarr523如果有帮助,不要忘记接受答案。 – 2013-05-03 23:58:04