Dice program getting 0s

This is a discussion on Dice program getting 0s within the C Programming forums, part of the General Programming Boards category; Hi, I'm new to this forum. I've written a program for one of my classes. It's supposed to be a ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    2

    Dice program getting 0s

    Hi, I'm new to this forum. I've written a program for one of my classes. It's supposed to be a really simple program and I know I'm making some kind of small mistake. The program compilies but when I run it, input the seed value and number of tosses, I get nothing but 0's as the output.

    My suspicion is that I'm somehow doing an integer divided by integer thing. In my calculation for the percentage of 8's I even put 100.0 to try to get rid of any integer problems. Please help.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(void)
    {
            /* Declare variables and function prototypes */
            unsigned int seed;
            int rand_int(int a, int b);
            int tosses, k, dice_1, dice_2, total=0;
    
            /* Get seed value from user */
            printf("Input seed: \n");
            scanf("%u" ,&seed);
            srand(seed);
    
            /* Get number of tosses from user */
            printf("Enter the number of tosses: \n");
            scanf("%i" ,&tosses);
    
            /* Simulate rolls of the dice */
            for (k=1; k<=tosses; k++)
            {
                    dice_1 = rand_int(1,6);
                    dice_2 = rand_int(1,6);
                    if (dice_1+dice_2 == 8)
                            total++;
            }
    
            /* Compute the percentage and print the number */
            /* of 8's rolled                               */
            printf("The number of 8's are: %5i \n",total);
            printf("The percent of 8's are: %5.2f \n",total*100.0/tosses);
    
            /* Exit Program */
            return 0;
    }
    
    /*******************************************************/
    /* This function generates a random integer between    */
    /* specefied limits a and b (a<b).                     */
    
    int rand_int(int a, int b)
    {
            return rand()%(b-a+1) + a;
    }
    /*******************************************************/

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You have an interesting definition of "0":
    Code:
    Input seed:
    17
    Enter the number of tosses:
    255
    The number of 8's are:    32
    The percent of 8's are: 12.55

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Don't know why you are not getting the output

    see what i did with your program

    [rocky@localhost test]$ ./a.out
    Input seed:
    123
    Enter the number of tosses:
    345
    The number of 8's are: 27
    The percent of 8's are: 7.83

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    2
    wow seriously? I don't know what happened. I used putty ssh, gcc compilied then ran my hw5.out file and put the inputs in and got 0's. Thank you for confirming that it works, I guess I'll just turn it in now.

    edit: yeah I just tried it out again and it works. I feel like an idiot. Sorry for wasting your time guys. Thanks again!
    Last edited by domezy; 10-27-2009 at 03:12 AM.

  5. #5
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    You were probably running an old build. thinking it was the current or something.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 11:58 PM
  4. rolling dice program
    By sunny2005 in forum C Programming
    Replies: 20
    Last Post: 03-21-2005, 04:45 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21