Thread: C Programming - Dice Toss Chart

  1. #1
    Registered User
    Join Date
    Oct 2017
    Posts
    3

    C Programming - Dice Toss Chart

    Hi! One of my labs is that I have to toss a die 60 times and then display a chart with the results that should look like this:

    Side Tosses
    1 8
    2 15
    3 6

    ...and so on and so forth.

    I can only use the while loop in this lab. I know I have to use <time.h> and should get random numbers. So far I've only been able to initialize tosses1, tosses2, tosses3... to 0 and declare the side, and rolls.

    I have been stuck on this for about 2 days because nothing I've been doing works. Please help me understand this while using the while loop. Thank you!

  2. #2
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    You should post your code so I can see what you are doing. Use the code tags if you aren't sure how "[ code ] [ / code ]"

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (void)
    {
    int die_roll = xx;
    
    while (die_roll-- > 0)    
    {
        // one line of code here perhaps?
    }
    return 0;    
    }
    Last edited by userxbw; 10-30-2017 at 06:59 PM.

  4. #4
    Registered User
    Join Date
    Oct 2017
    Posts
    3
    I don't understand how to generate only 60 random numbers and what I am supposed to put in the while loop.



    Code:
    #include
    Code:
    <stdio.h>#include <time.h>
    #include <stdlib.h>
    
    
    int main(int argc, const char * argv[]) {
    
        int number = (rand() % 6) + 1;
        int rolls;
        int tosses1 = 0;
        int tosses2 = 0;
        int tosses3 = 0;
        int tosses4 = 0;
        int tosses5 = 0;
        int tosses6 = 0;
    
        srand(60);
    
        while (
            )
        switch (number)
        {
            case 1:
                tosses1++;
                break;
            case 2:
                tosses2++;
                break;
            case 3:
                tosses3++;
                break;
            case 4:
                tosses4++;
                break;
            case 5:
                tosses5++;
                break;
            case 6:
                tosses6++;
                break;
        }
    
    printf("Number  Frequency");
    printf("\n  1        %i", tosses1);
    printf("\n  2        %i", tosses2);
    printf("\n  3        %i", tosses3);
    printf("\n  4        %i", tosses4);
    printf("\n  5        %i", tosses5);
    printf("\n  6        %i\n", tosses6);]

  5. #5
    Banned
    Join Date
    Aug 2017
    Posts
    861
    60 rolls and 60 results? or 6 out of 60?
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    
    int main(void)     //int argc, const char * argv[]) {
    {
    
        int number = (rand() % 6) + 1;
        int rolls = 60;
        int tosses1 = 0;
        int tosses2 = 0;
        int tosses3 = 0;
        int tosses4 = 0;
        int tosses5 = 0;
        int tosses6 = 0;
    
        srand(60);
    
        while ( rolls-- > 0)
        {
    
        switch (number)
        {
            case 1:
                tosses1++;
                break;
            case 2:
                tosses2++;
                break;
            case 3:
                tosses3++;
                break;
            case 4:
                tosses4++;
                break;
            case 5:
                tosses5++;
                break;
            case 6:
                tosses6++;
                break;
        }
    }
    
    printf("Number  Frequency");
    printf("\n  1        %i", tosses1);
    printf("\n  2        %i", tosses2);
    printf("\n  3        %i", tosses3);
    printf("\n  4        %i", tosses4);
    printf("\n  5        %i", tosses5);
    printf("\n  6        %i\n", tosses6);
    
    return 0;
    }
    your results
    Code:
    Number  Frequency
      1        0
      2        60
      3        0
      4        0
      5        0
      6        0
    Look at where you're calling to get a ran num, then when it starts checking that (same) number.
    this I think is closer to what it should look like.
    Code:
    Number  Frequency
      1        13
      2        7
      3        8
      4        10
      5        15
      6        7
    60 rolls 60 results
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main (void)
    {
    int die_roll = 60;
    
    while (die_roll-- > 0)    
    {
    printf("%d : %d  %d \n",die_roll, rand()% 6 +1, rand() % 6 +1);
    }
    return 0;    
    }
    Last edited by userxbw; 10-31-2017 at 02:15 PM.

  6. #6
    Registered User
    Join Date
    Oct 2017
    Posts
    3
    It should be 60 rolls to generate 60 random numbers. So when you add all the frequencies together in the output it should equal 60.

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by hihellooo View Post
    It should be 60 rolls to generate 60 random numbers.

    So when you add all the frequencies together in the output it should equal 60.
    imposable to add all the rolls together to equal 60 using a 60 random numbers thing.
    that makes an equation like this (X + Y)^60 = 60; something like that. where X and Y = 1...6 on a random basis.

    if you go back and look again, it is set up to only roll a total times of 60, with each match of 1 thru 6 it adds it each time. you just need to move your
    number = rand() % 6+1; to a point where is calls it each time to get that new roll to check it again, else it will always be the same number at start up.

    what is the max number two die can total at one given toss of both die with the largest number of each die being 6?
    better put:
    1 .. 6 on one six sided die. What is the greatest number that can be achieved times 2?
    Code:
    int die_roll = 60;
    int total = 0, die1 = 0, die2 = 0;
    while (die_roll-- > 0)    
    {
        die1 = rand()% 6 +1;
        die2 = rand() % 6 +1;
        printf("%d : %d  %d \n",die_roll, die1,die2 );
        total += (die1+die2);
        
    }
    printf("total : %d\n", total);
    results of total 60 random rolls of two die 1 ... 6;
    Code:
    total : 399
    Last edited by userxbw; 10-31-2017 at 02:44 PM.

  8. #8
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Quote Originally Posted by userxbw View Post
    imposable to add all the rolls together to equal 60 using a 60 random numbers thing.
    Umm, what they are asking is not impossible but how it should be. They said add the frequencies of each rolled number together so if there are 60 rolls then the sum of the frequencies must be 60 otherwise something is wrong.

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by Hodor View Post
    Umm, what they are asking is not impossible but how it should be. They said add the frequencies of each rolled number together so if there are 60 rolls then the sum of the frequencies must be 60 otherwise something is wrong.
    (6 + 6) * 60 = 720
    12 * 60 = 720

    12 being the highest set of numbers added together 60 times.
    2 * 60 = 120
    figure the odds

    if they are refering to the amount of times rolled then added together
    1 die * 60 = 60 because it has not been roll more or less than 60
    1 die * 60 = 60 because it has not been roll more or less than 60

    2 die this is, 2 * 60 = 120

    Just saying it has to be worded wrong. I am not arguing with you just what is being said because it makes no sense to me.

  10. #10
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    I don't understand

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main(void)
    {
        int i = 60;
        int hist[6] = { 0 };
        int total;
    
        srand(time(NULL));
    
        while (i--)
            hist[rand() % 6]++;
    
        total = 0;
        for (i = 0; i < 6; i++) {
            printf("%d: %d\n", i, hist[i]);
            total += hist[i];
        }
        printf("----\nTotal frequency count: %d\n", total);
    
        return 0;
    }

  11. #11
    Banned
    Join Date
    Aug 2017
    Posts
    861
    can you post your original assignment verbatim?
    results
    Code:
    userx@slackwhere:~/bin
    $ ./frequency
    0: 13
    1: 12
    2: 8
    3: 13
    4: 3
    5: 11
    ----
    Total frequency count: 60
    the only thing I can see that MIGHT BE (don't quote me on that) wrong here is your rand a die has 1 - 6
    Code:
      hist[rand() % 6]++;
    what would happen if you wrote it like before?
    Code:
      hist[rand() % 6 + 1]++;
    to keep it between 1 thru 6.

    I went back up and reread it again,
    have to toss a die 60 times and then display a chart with the results that should look like this:
    I was using a two die throught process.

    but as far as this goes
    have been stuck on this for about 2 days because nothing I've been doing works. Please help me understand this while using the while loop.
    I think you got way past that issue.

    I have not seen it coming up zeros either, so I'd give you an A and Saturday off.
    Last edited by userxbw; 11-01-2017 at 09:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C programming Bar chart
    By Mustafa Ekicim in forum C Programming
    Replies: 15
    Last Post: 04-04-2014, 02:10 PM
  2. C Programming Dice Display Function?
    By Brian Westerhof in forum C Programming
    Replies: 8
    Last Post: 02-21-2012, 07:11 AM
  3. Need Help on Coin Toss Program
    By Blazefury5 in forum C++ Programming
    Replies: 11
    Last Post: 04-12-2011, 12:32 PM
  4. coin toss
    By ejd81882 in forum C Programming
    Replies: 3
    Last Post: 10-14-2008, 12:53 PM
  5. Coin Toss
    By kazuakijp in forum C Programming
    Replies: 4
    Last Post: 08-31-2004, 09:02 PM

Tags for this Thread