#include <stdio.h>
#include <stdlib.h>
/* declaration and initialize globe variable */
int FREQUENCY1 = 0;
int FREQUENCY2 = 0;
int FREQUENCY3 = 0;
int FREQUENCY4 = 0;
int FREQUENCY5 = 0;
int FREQUENCY6 = 0;
/* function prototype */
int statistic(int);
int main(){
int i_times;/* declaration the throw times */
int i_tmp;/* storage the result */
/* prompt */
printf("Enter the time of throw the dice: ");
scanf("%d", &i_times);
/* throw the dice */
while(i_times){
i_tmp = 1 + rand() % 6;/* 6 is the zoom factor, make sure the result between 0~5*/
statistic(i_tmp);/* call the function to statistic the frequency */
i_times--;
}
/* output the result */
printf("The 1 appear %d times;\n", FREQUENCY1);
printf("The 2 appear %d times;\n", FREQUENCY2);
printf("The 3 appear %d times;\n", FREQUENCY3);
printf("The 4 appear %d times;\n", FREQUENCY4);
printf("The 5 appear %d times;\n", FREQUENCY5);
printf("The 6 appear %d times;\n", FREQUENCY6);
return 0;
}
/* definition the function */
int statistic(int result)
{
switch(result){
case 1:
FREQUENCY1++;
break;
case 2:
FREQUENCY2++;
break;
case 3:
FREQUENCY3++;
break;
case 4:
FREQUENCY4++;
break;
case 5:
FREQUENCY5++;
break;
case 6:
FREQUENCY6++;
break;
}
return 0;
}
掷骰子统计各个面出现的次数 调用函数实现 使用全局变量
最新推荐文章于 2022-12-26 20:57:08 发布