纳输出百分比计算,骰子滚动程序

时间:2014-03-19 14:22:30

标签: c

我正在尝试计算两个骰子每两个骰子滚动的次数百分比。我的“实际”值返回nan,我的预期返回0.我知道代码效率低,我们不允许在这个赋值中使用数组有人可以指出我正确的方向吗?谢谢!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int main() {


    int d2 = 0;
    int d3 = 0;
    int d4 = 0;
    int d5 = 0;
    int d6 = 0;
    int d7 = 0;
    int d8 = 0;
    int d9 = 0;
    int d10 = 0;
    int d11 = 0;
    int d12 = 0;


int k, roll_1, roll_2, num_roll, total, count = 0 ;

    float perc, exp;
    srand((long)time(NULL));

    printf("Enter number \n");

    scanf("%d", &num_roll);




for (k = 0; k < num_roll; k++)

            {
            roll_1 = rand() %6 +1;
            roll_2 = rand() %6 +1;
            total = roll_1 + roll_2;
            total++;

            if (total == 2) d2++;
            else if (total == 3) d3++;
            else if (total == 4) d4++;
            else if (total == 5) d5++;
            else if (total == 6) d6++;
            else if (total == 7) d7++;
            else if (total == 8) d8++;
            else if (total == 9) d9++;
            else if (total == 10) d10++;
            else if (total == 11) d11++;
            else if (total == 12) d12++;
     }

printf("Roll\t Total\t Session\tExpected\n\n");


     printf("2:\t %d\t %.3f \t\t %.3f \n", d2, perc, exp);
     printf("3:\t %d\t %.3f \t\t %.3f \n", d3, perc, exp);
     printf("4:\t %d\t %.3f \t\t %.3f \n", d4, perc, exp);
     printf("5:\t %d\t %.3f \t\t %.3f \n", d5, perc, exp);
     printf("6:\t %d\t %.3f \t\t %.3f \n", d6, perc, exp);
     printf("7:\t %d\t %.3f \t\t %.3f \n", d7, perc, exp);
     printf("8:\t %d\t %.3f \t\t %.3f \n", d8, perc, exp);
     printf("9:\t %d\t %.3f \t\t %.3f \n", d9, perc, exp);
     printf("10:\t %d\t %.3f \t\t %.3f \n", d10, perc, exp);
     printf("11:\t %d\t %.3f \t\t %.3f \n", d11, perc, exp);
     printf("12:\t %d\t %.3f \t\t %.3f \n", d2, perc, exp);

  return 0;
  }

1 个答案:

答案 0 :(得分:4)

  1. total++似乎错了;这是为了什么?

  2. 您永远不会向percexp分配任何内容。

  3. 您打印d2两次,但绝不打印d12

  4. 你应该打开一些警告。对于-Wall,clang说:

    example.c:22:41: warning: unused variable 'count' [-Wunused-variable]
    int k, roll_1, roll_2, num_roll, total, count = 0 ;
                                            ^
    example.c:58:48: warning: variable 'perc' is uninitialized when used here
          [-Wuninitialized]
         printf("2:\t %d\t %.3f \t\t %.3f \n", d2, perc, exp);
                                                   ^~~~
    example.c:24:15: note: initialize the variable 'perc' to silence this warning
        float perc, exp;
                  ^
                   = 0.0
    example.c:58:54: warning: variable 'exp' is uninitialized when used here
          [-Wuninitialized]
         printf("2:\t %d\t %.3f \t\t %.3f \n", d2, perc, exp);
                                                         ^~~
    example.c:24:20: note: initialize the variable 'exp' to silence this warning
        float perc, exp;
                       ^
                        = 0.0
    3 warnings generated.
    

    添加-Weverything会产生另一个警告:

    example.c:25:11: warning: implicit conversion loses integer precision: 'long' to
          'unsigned int' [-Wshorten-64-to-32]
        srand((long)time(NULL));
        ~~~~~ ^~~~~~~~~~~~~~~~
    

    您可能会或可能不会关心的那个。