【发布时间】:2014-01-03 06:32:12
【问题描述】:
当谈到 c 时,我是一名一年级学生,需要帮助将 5 个随机值存储在一个数组中并输出它们。这是我在哪里。
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
struct score_card {int A_ones; int B_twos; int C_threes; int D_fours; int E_fives; int F_sixes; int G_chance;};
int dice_rolls[5];
int randomize(void);
int value;
int main(void) {
struct score_card test;
randomize;
int i;
for(i = 0; i <= 4; i++){
printf("%d\n", dice_rolls[i]);
}
printf("\n");
return 0;
}
int randomize(void){
int i;
srand(time(0));
for(i = 0; i <= 4; i++){
value = rand() % 6 + 1;
dice_rolls[i] = value;
}
}
输出是: 6294304 6294308 6294312 6294316 6294320
目标是使用模除法从 1 -->6 中获取值并将它们存储在 dicerolls 数组中。
【问题讨论】:
-
你忘了问问题。这不是您预期的输出吗?
-
randomize;-->randomize(); -
改进代码格式不会有什么坏处。
-
printf("%d", &dice_rolls[i]);-->printf("%d", dice_rolls[i]); -
int dice_rolls[4];-->int dice_rolls[5];