gpt4 book ai didi

c++ - 阵列滚动 2 个骰子

转载 作者:行者123 更新时间:2023-11-30 00:38:33 24 4
gpt4 key购买 nike

编写一个模拟两个骰子滚动的程序。然后应计算这两个值的总和并将其放入单下标数组中。打印数组。还要找出 12 出现了多少次。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 13
int main()
{
int face;
int arraysize=13;
int counter[13];
int frequency[ SIZE ]= {13};
for(int i=0; i<13; i++)
counter[i] = 0;

int die1;
int die2;



srand( time( NULL ) );


for ( int roll1 = 0; roll1 <=36000; roll1++ ) {
die1 = 1 + rand() % 6;
die2 = 1 + rand() % 6;
counter[die1+die2]++;
++frequency[ face ];
}

printf("%4s%17s\n","Sum of Values","Frequency");

for(int face=2; face<arraysize;face++)
{
printf("%8d%17d\n",face,frequency[ face ]);
}

system("PAUSE");
return 0;
}
PRİNT SCREENSum of Values        Frequency       2            36001       3                0       4                0       5                0       6                0       7                0       8                0       9                0      10                0      11                0      12                0

怎么了???

最佳答案

这一行

 ++frequency[ face ];

总是递增相同的位置,因为循环不会改变 face 的值,你应该做类似的事情

++frequency[die1+die2];

我也不知道为什么你有frequencycounter有什么区别?

编辑:就像有人指出的那样,face 根本没有初始化(除非您删除了一些代码)。

关于c++ - 阵列滚动 2 个骰子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10420508/

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