# Dice program getting 0s

• 10-26-2009
domezy
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;
}
/*******************************************************/

• 10-26-2009
tabstop
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

• 10-26-2009
RockyMarrone
Don't know why you are not getting the output

see what i did with your program

Quote:

[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
• 10-27-2009
domezy
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!
• 10-27-2009
bythos
You were probably running an old build. thinking it was the current or something.