#pragma warning(disable:4996)
// INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// DEFINES
#define LOWER_DIE 1 // LOWER_DIE must be at least 1
#define UPPER_DIE 10
#define TRIALS 10000
// PROTOTYPES
int genRand(int, int);
int getTwoDieSum(int, int);
// MAIN
int main( int total) {
int numberArray[2 * UPPER_DIE + 1] = { '0', '0', '0', '0', '0', '0', '0','0','0','0','0', '0', '0' };// holds frequency of rolls
int roll = total;
int i = 0;
srand((unsigned int)time(NULL));
for (i = 0; i < TRIALS; i++) {
getTwoDieSum( LOWER_DIE, UPPER_DIE);
numberArray[roll] == numberArray[roll] + 1;
}
printf("ROLL VALUE : FREQUENCY\n");
printf("2\t: %d\n", numberArray[2]);
printf("3\t: %d\n", numberArray[3]);
printf("4\t: %d\n", numberArray[4]);
printf("5\t: %d\n", numberArray[5]);
printf("6\t: %d\n", numberArray[6]);
printf("7\t: %d\n", numberArray[7]);
printf("8\t: %d\n", numberArray[8]);
printf("9\t: %d\n", numberArray[9]);
printf("10\t: %d\n", numberArray[10]);
printf("11\t: %d\n", numberArray[11]);
printf("12\t: %d\n", numberArray[12]);
return 0;
}
// FUNCTION IMPLEMENTATIONS
int genRand(int lower, int upper) {
int range = (upper - lower) + 1;
return rand() % range + lower;
}
int getTwoDieSum( int lower, int upper) {
int rollOne = 0;
int rollTwo = 0;
int total = 0;
rollOne = genRand(lower, upper);
rollTwo = genRand(lower, upper);
total = rollOne + rollTwo;
return total;
}
到目前为止,它应该生成随机数,然后填充数组的正确元素。为所有printf()语句保持输出48。我搞砸的任何想法?我认为它在genRand()区域或getTwoDieSum()中。我需要使用以下shell,我不确定应该做的一切是什么,这就是为什么我搞砸了。另外,我不确定int lower和int upper来自哪里:
int genRand(int lower, int upper) {
int range = (upper - lower) + 1;
return rand() % range + lower;
}
我的问题更多的是我搞砸了哪里,int lower和int upper来自哪里,我是否正确使用随机种子生成器?
需要使用Shell:
// INCLUDES
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// DEFINES
#define LOWER_DIE 1 // LOWER_DIE must be at least 1
#define UPPER_DIE 10
#define TRIALS 10000
// PROTOTYPES
int genRand(int, int);
int getTwoDieSum();
// MAIN
int main() {
int numberArray[2 * UPPER_DIE + 1]; // holds frequency of rolls
// initialize numberArray
// seed the random number generator
// roll the dice and keep track of what was rolled in the array
// display results
return 0;
}
// FUNCTION IMPLEMENTATIONS
int genRand(int lower, int upper) {
int range = (upper - lower) + 1;
return rand() % range + lower;
}
int getTwoDieSum() {
// YOUR IMPLEMENTATION GOES HERE
答案 0 :(得分:0)
使用:
int main(void) {
int numberArray[2 * UPPER_DIE + 1] = { '0', '0', '0', '0', '0', '0', '0','0','0','0','0', '0', '0' };// holds frequency of rolls
int roll = 0;
int i = 0;
srand((unsigned int)time(NULL));
for (i = 0; i < TRIALS; i++) {
getTwoDieSum( LOWER_DIE, UPPER_DIE);
int main_total = getTwoDieSum( LOWER_DIE, UPPER_DIE);
roll = main_total;
numberArray[roll] = numberArray[roll] + 1;
}
我能够让它正常工作。
答案 1 :(得分:0)
您可以更多地改进代码。例如,可以简化计算两个卷的总和的函数:
int getTwoDieSum( int lower, int upper ) {
return genRand(lower, upper) + genRand(lower, upper);
}
现在,当getTwoDieSum
返回一个值,就像genRand
一样,您最好使用它。您的main
函数可能如下所示:
// MAIN
int main() {
// holds frequency of rolls
int numberArray[2 * UPPER_DIE + 1] = {0};
// The size of the array of frequency is set to 21, so that numberArray[20]
// represents the frequency of roll 10+10.
// The first two elements (numberArray[0] and numberArray[1]) are unused
int size = sizeof(numberArray) / sizeof(int);
int i;
// If an array is partially initialized, elements that are not initialized
// receive the value 0 of the appropriate type. Alternatively you can use:
//
// for ( i = 0; i < size; ++i ) {
// numberArray[i] = 0;
// }
//
// or simply
//
// memset(numberArray,0,size);
// seed the random number generator
srand(time(NULL));
// roll the dice and keep track of what was rolled in the array
int roll;
for (i = 0; i < TRIALS; i++) {
roll = getTwoDieSum( LOWER_DIE, UPPER_DIE);
++numberArray[roll];
}
// display results
printf("ROLL VALUE : FREQUENCY\n");
for (i = 2; i < size; i++) {
// numberArray[2] represent the frequency of roll 1+1,
// while numberArray[20] is the frequency of roll 10+10
printf("%d\t: %d\n", i, numberArray[i]);
// add ^^ the roll value
}
return 0;
}
典型的运行给出了这个输出:
ROLL VALUE : FREQUENCY
2 : 99
3 : 179
4 : 273
5 : 390
6 : 513
7 : 587
8 : 678
9 : 777
10 : 902
11 : 1039
12 : 913
13 : 822
14 : 672
15 : 614
16 : 514
17 : 433
18 : 298
19 : 195
20 : 102