掷骰子统计各个面出现的次数 调用函数实现 使用全局变量

#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;
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值