Thread: rolling dice problem

1. rolling dice problem

I have a question with this program due tommorow. I need help totaling up the frequency column and the percent column. I also do not know how to find the number of times doubles came up and to print them out. Any help would be appreciated. I have been explain many times but could someone please show me.

heres what i have...

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROLLS 6000

void main (void)
{
int die1[ROLLS], die2[ROLLS], counter, frequency [13]={0};
int doubles [13] ={0};
float percentage;

/*Rolling the dice 6000 times*/
for (counter=0; counter<ROLLS; counter++)
{
die1[counter] = rand() %6 + 1;
die2[counter] = rand() %6 + 1;

frequency [die1[counter] + die2[counter]]++;
}

/* Displaying the results*/
printf ("Sum of Die Frequency Percent\n");
printf ("______________________________________________\n ");
for (counter=2; counter<13; counter++)
{
percentage = (float)frequency[counter] / 60;
printf(" %2d", counter);
printf(" %4d", frequency[counter]);
printf(" %5.2f\n" ,percentage);

}
printf("__________________________________________ _____\n");
printf("TOTALS \n\n\n");

/*Chart to print out doubles, frequency, and percent*/
printf ("Doubles Frequency Percent\n");
printf("__________________________________________ _____\n");
for (counter=6; counter>0; counter--)
{
printf(" %d\n", counter);
}
printf("__________________________________________ _____\n");
printf("TOTALS \n\n\n");
}

2. Code:
```#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROLLS 6000

void main (void)
{
int die1[ROLLS], die2[ROLLS], counter, frequency [13]={0};
int doubles [13] ={0};
float percentage, sum1=0;

/*Rolling the dice 6000 times*/
for (counter=0; counter<ROLLS; counter++)
{
die1[counter] = rand() %6 + 1;
die2[counter] = rand() %6 + 1;

frequency [die1[counter] + die2[counter]]++;
}

/* Displaying the results*/
printf ("Sum of Die           Frequency       Percent\n");
printf ("______________________________________________\n");
for (counter=2; counter<13; counter++)
{
percentage = (float)frequency[counter] / 60;
printf("     %2d", counter);
printf("              %4d", frequency[counter]);
printf("               %5.2f\n" ,percentage);
sum1 = percentage + percentage;
}
printf("_______________________________________________\n");
printf("TOTALS                                         \n\n\n");
printf("%d", sum1);

/*Chart to print out doubles, frequency, and percent*/
printf ("Doubles             Frequency        Percent\n");
printf("_______________________________________________\n");
for (counter=6; counter>0; counter--)
{
printf("    %d\n", counter);
}
printf("_______________________________________________\n");
printf("TOTALS                                         \n\n\n");
}```

3. To calculate the doubles you would need to store those results in a seperate variables. This is because you can get (for example) from 3 3 or 4 2.

So you could have something such as: (note for this solution there is no need for die1 and die 2 to be an array and as such I'm assuming they aren't in my code examples )
Code:
`int doubles[6] = { 0, 0, 0, 0, 0, 0 };`
After your roll you can have
Code:
```if (die1 == die2)
{
doubles[die1]++;
}```
To extract the frequency is simple as that what it stored and the percentage is just the freq / 6000.

Edit: Oh yeah change
Code:
`void main(void)`
to
Code:
`int main(void)`
and add
Code:
`return 0;`
at the end of main()

4. Code:
`void main (void)`
Don't use void main. Search the board and learn why... Going on from that you will need to return 0; after you change to int main().

Code:
`		frequency [die1[counter] + die2[counter]]++;`
If I understand your code correctly, you should have

Code:
`frequency[your_random_number_here]++;`
Assign your random number to another variable to get this done. Remember to have one after you assign a number to die 1, and then do this again after you do it for die 2.

Try making a flow chart so that you know where all the variables should be before you start coding.

5. actually their
Code:
`frequency [die1[counter] + die2[counter]]++;`
is correct for what they want to do. die1[counter] and die2[counter] both store a number between (inclusive) 1 and 6. They both are the values of 1 die. He wants the frequency the sum of the two comes up.

6. Thanx

thanks for the help with the doubles but now how do you total the frequency and percent and display them

7. Ahh.... the sum.... gotcha.... nevermind then....

Popular pages Recent additions