Thread: *Need help with a throwing dice program*

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    1

    *Need help with a throwing dice program*

    Hello I'm Mike and I'm not great at C and I need help with one of my programs. I've done the first part of the task which works fine. This is the first task :

    "Design and write a program which throws two dice 300 times. Each time total the number of spots and count how many times each number of spots was thrown. Use a 12 element array to hold the spot counts."

    And this is the code for it.

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <time.h>
    
    
    void initarray(int d[]);
    
    
    void printstar(int n);
    void fancyprint(int d[]);
    int roll(void);
    int numrolls=300;
    
    
    int main(void) {
    
    
       int d[13]; 
       int i;     
     
       initarray(d);
       printf("Total rolls: %1d\n\n", numrolls);
    
    
       for (i = 0; i < numrolls; i++)
          d[roll() + roll()]++;
       fancyprint(d);
       return 0;
    }
    
    
    
    
    void initarray(int d[]) {
       int i;
       for (i = 2; i < 13; i++)
          d[i] = 0;
    }
    
    
    void printstar(int n) {
       while (n > 0) {
          printf("*");
          n--;
       }
    }
    
    
    void fancyprint(int d[]) {
       int i;
       printf("\n");
       for (i = 2; i < 13; i++) {
          printf("Sum:%3d |", i);
    
    
          printstar(300*d[i]/numrolls);
          
          printf("\n");
       }
       printf("\n");
    }
    
    
    
    
    int roll(void) {
       return (int) (6.0*(rand()/(double)RAND_MAX)
          + 1.0);
    }
    I now have a problem with the second part.
    This is the second part of the task:

    "Extend program 3 to ask the user whether they want to throw 2, 3, or 4 dice. Use a 24 element array to hold the results, but only display either 12, 18 or 24 lines on the bar chart, depending on the number of dice chosen."

    Please Help as I have no idea how to do the second part.
    Thank you.

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Bad@CMike View Post
    I now have a problem with the second part.
    This is the second part of the task:

    "Extend program 3 to ask the user whether they want to throw 2, 3, or 4 dice. Use a 24 element array to hold the results, but only display either 12, 18 or 24 lines on the bar chart, depending on the number of dice chosen."

    Please Help as I have no idea how to do the second part.
    Thank you.
    On this line:

    Code:
    d[roll() + roll()]++;
    ... you've hard-coded the roll of two dice. For the next part, you want to roll 'N 'dice (where 'N' is the value entered by the user).

    You've done a good job of breaking your code into functions, so I would suggest you create a new function that rolls a specified number of dice and returns the total - the prototype might look something like this:

    Code:
    int roll_dice(int number_of_dice);
    For printing, just send the value 'N' to your print function, which will determine how many results to display.



    A few other comments on the code (which looks really good, by the way):

    If you want to initialize your entire array to zero, you can do it this way:

    Code:
    int d[13] = { 0 };
    Since the value of "numrolls" never changes, you'd be better off using a named constant (which, by convention, should be all caps):

    Code:
    #define NUM_ROLLS  300
    Notice you're getting the same results each time you run the program - if you want to get different values each run, you need to seed the RNG with "srand()".

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Mistakes in your current code:
    * you were asked to use a 12 element array; you used a 13 element array.
    * the 1 in the "%1d" format spec for printing numrolls is pointless (it is essentially the default). Should just be "%d".
    * this makes no sense
    Code:
    printstar(300*d[i]/numrolls);   // note that numrolls == 300
    * you forgot to call srand to seed the random number generator. This is usually done early in main (the first executable instruction is a good place) and is done like this:
    Code:
    srand((unsigned)time(NULL));   // include <stdlib.h> and <time.h>]
    If you don't use srand then rand will give the same sequence of outputs every run.
    Last edited by algorism; 01-20-2016 at 09:49 AM.

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    6
    Hello, I'm his friend and doing the same task and we are still struggling with the second part of the task, can someone like re-write the code for us, as we have no idea how to do this and that is the only task left that needs to be complete. Please and Thank you

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Wlod View Post
    Hello, I'm his friend and doing the same task and we are still struggling with the second part of the task, can someone like re-write the code for us, as we have no idea how to do this and that is the only task left that needs to be complete. Please and Thank you
    Please start your own thread for any problems you might be having.

    Note that nobody will re-write the code for you - see the homework policy.

    If you and your "friend" are both learning, I strongly recommend you each work separately when attempting to solve such exercises. Otherwise, it might look like you're just copying their work.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dice Program
    By M_A_T_T in forum C Programming
    Replies: 4
    Last Post: 01-31-2013, 03:52 PM
  2. Replies: 3
    Last Post: 03-01-2011, 07:00 PM
  3. Dice program getting 0s
    By domezy in forum C Programming
    Replies: 4
    Last Post: 10-27-2009, 03:52 AM
  4. Replies: 16
    Last Post: 08-12-2009, 07:22 PM