0

I wrote a small dice rolling program that will print out the results of however many dice rolls that are entered. I want to count how much each number occurs so I thought I would put the output from the rand() function into an array and then search the array for the different values. I don't know how to put numbers into an array that are not entered in manually.

 #include <stdio.H>
 #include <stdlib.h>
 #include <time.h>
 int main(void)
 {
 int count; 
 int roll; 
 srand(time(NULL));
 printf("How many dice are being rolled?\n");
 scanf("%d", &count);
 printf("\nDice Rolls\n");
 for (roll = 0; roll < count; roll++)
 {
 printf("%d\n", rand() % 6 + 1);
 }
 return 0;
 }
asked Apr 25, 2013 at 15:43
2
  • Oh, goody! A new user. There is no <stdio.H>. I think you meant <stdio.h>. Which book are you reading? Commented Apr 25, 2013 at 16:02
  • Yeah, that was a typo. I have the Absolute Beginner's Guide to C, second edition by Greg Perry and C Primer Plus, fifth edition by Stephen Prata. Commented Apr 25, 2013 at 20:32

3 Answers 3

2
 #include <stdio.H>
 #include <stdlib.h>
 #include <time.h>
 int main(void)
 {
 int count; 
 int roll; 
 int* history;
 srand(time(NULL));
 printf("How many dice are being rolled?\n");
 scanf("%d", &count);
 history = malloc( sizeof(int) * count );
 if( !history )
 {
 printf( "cannot handle that many dice!\n" );
 exit( -1 );
 }
 printf("\nDice Rolls\n");
 for (roll = 0; roll < count; roll++)
 {
 history[roll] = rand() % 6 + 1;
 printf("%d\n", history[roll]);
 }
 // do something interesting with the history here
 free( history );
 return 0;
 }
answered Apr 25, 2013 at 15:47
Sign up to request clarification or add additional context in comments.

2 Comments

if you add check for allocation failure, why not add a check for bad scanf too.
Because it was not my intent to show him how to use scanf, it was to show him how to correctly allocate and use a dynamic array? ~smile~
0

just put it into the array

for (roll = 0; roll < count; roll++)
{
 myArray[roll] = rand() % 6 + 1;
 printf("%d\n", myArray[roll] );
}
answered Apr 25, 2013 at 15:47

1 Comment

Bear in mind that your solution here does not take into account the fact that the user is inputting a variable number of die to roll, not specifically three die.
0

If you want to track the number of occurences of each result, you don't even need to save every dice roll.

int result[6] = {} ; // Initialize array of 6 int elements
int current = 0; // holds current random number
for (roll = 0; roll < count
{
 current = rand() % 6;
 result[current]++; // adds one to result[n] of the current random number
 printf("%d\n", current+1);
}

After which you will have an array 0-5 (result), with each element containing the number of each occurence (you will need to add the element number + 1 to get the actual roll). ie. result[0] is the number of occurences of '1'.

answered Apr 25, 2013 at 15:59

1 Comment

Why, thank you. And yes, that is correct twalberg so thanks for that correction as well.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.