rolling dice problem

This is a discussion on rolling dice problem within the C Programming forums, part of the General Programming Boards category; I have a question with this program due tommorow. I need help totaling up the frequency column and the percent ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    34

    Unhappy 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. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    34
    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");
    }

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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()

  5. #5
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    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.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    34

    Thanx

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

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Ahh.... the sum.... gotcha.... nevermind then....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Dice Rolling Problem
    By EdSquareCat in forum C++ Programming
    Replies: 3
    Last Post: 05-26-2007, 01:53 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 10:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21