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;
}
/*******************************************************/