Thread: homework help: program to graph date...

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    3

    homework help: program to graph date...

    Hi, I have to write a program that will use a random number generator to roll two standard dice. and then graph the data. this is what i have so far:

    Code:
    int main(){
        int die1;
        int die2;
        int total[5000];
        int role;
        int j,i;
    
    
    //asking for number of times dice are to be rolled
    
    
    printf("How manytimes do you want to role the dice?(100-5000): \n");
      scanf("%d", &role);
    
    
        while( role < 100 || role > 5000)
            {
    
    
                if( role < 100 || role > 5000)
                {
                  printf("\nPlease enter a number between 100 and 5000: ");
                    scanf("%d", &role);
                }
            }
    
    
    // initialize random see
    
    
      srand (time(NULL));
    
    
    //loop to store rolles into errays
    
    
    
    
    for(i = 0; i < role; i++){
    
    
    
    
    //genreate number between 1 and 6
    
    
    
    
     	die1 = rand() %6 + 1;
    
    
    	die2 = rand() %6 + 1;
    
    
    
    
          total[i] = die2 + die1;
    }
    
    
    //printf("\ntotal: %d\n", total);
    
    
    
    
    for(j=0; j<role; j++){
    
    
    printf("%d ", total[j]);
    
    
    }
    
    
      return 0;
    }
    So far I have put the total sum of both dice into an array.
    but now I have to graph it into a data and I am really lost on how to do this, can someone please give me a push in the right direct as to what i should do next? Thank you!

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    If you don't mind a basic vertical graph, then you could create two loops - an outer loop controlled by the length of data and an inner loop controlled by data's value at each iteration for printing a chosen graphing character.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    596
    Exactly what are you trying to graph?

    Do you want a graph bar for each roll of the two die, showing a value of 2 to 12?

    Or do you want to show how many times a 2 was rolled, how many times a 3 was rolled, how many times a 4 was rolled, etc?

    If it's the first case, you already have the data stored in the total[] array. Now it's a matter of "drawing" a bar with characters
    for each roll.

    For example,
    A 2 would look like this - **
    A 3 would look this this - ***
    A 7 would look like this - *******


    But if it's the second case, which I suspect it is, you will need to first produce totals for each
    of the possible roll values, 2 through 12. Then you will only be drawing 11 bars, one for each
    possible roll value. The length of the bar will then be the total for that possible value.
    Last edited by megafiddle; 04-29-2013 at 04:36 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't want total[5000]. You want roll[13].

    Every time you roll the dice, you will increment a counter in roll[value]:
    maybe: roll[value]++;

    When you're done, you'll have the result of every dice roll, separated by it's value:

    roll[2] will equal how many times "snake eyes" was rolled.
    roll[3] will equal how many times you roll a two and a one
    etc.

    Yes, roll[0] and roll[1] will be unused, and should be set to empty just in case.

    Next you need to calculate the scale of the graph. You should have a #define MAXx and #define MAXy
    where MAXx is the largest number of spaces that can be used in any row (horizontal), and MAXy does the same for vertical columns.

    Then use a 2D array to work with, creating your graph.

    Finally, print out the 2D array you made, making sure that the lower values are being displayed in the proper orientation (for a vertical bar type graph, you want the lesser values at the bottom of the screen, but the console prints from top to bottom, so you have to either print it from the top to the bottom, or create the array, using a "flipped" scheme.

    Work it through by hand a time or two with very simple values, and you'll see the basics of what's needed.

    IMO, a horizontal bar type graph is easier to make than a vertical bar graph, by a good deal.
    Last edited by Adak; 04-29-2013 at 06:11 PM.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    3
    Quote Originally Posted by Adak View Post
    You don't want total[5000]. You want roll[13].

    Every time you roll the dice, you will increment a counter in roll[value]:
    maybe: roll[value]++;

    When you're done, you'll have the result of every dice roll, separated by it's value:

    roll[2] will equal how many times "snake eyes" was rolled.
    roll[3] will equal how many times you roll a two and a one
    etc.

    Yes, roll[0] and roll[1] will be unused, and should be set to empty just in case.

    Next you need to calculate the scale of the graph. You should have a #define MAXx and #define MAXy
    where MAXx is the largest number of spaces that can be used in any row (horizontal), and MAXy does the same for vertical columns.

    Then use a 2D array to work with, creating your graph.

    Finally, print out the 2D array you made, making sure that the lower values are being displayed in the proper orientation (for a vertical bar type graph, you want the lesser values at the bottom of the screen, but the console prints from top to bottom, so you have to either print it from the top to the bottom, or create the array, using a "flipped" scheme.

    Work it through by hand a time or two with very simple values, and you'll see the basics of what's needed.

    IMO, a horizontal bar type graph is easier to make than a vertical bar graph, by a good deal.
    why only 13 times? if they are allowed to roll from 100 to 5000 times?
    Last edited by Jazmin Jasso; 04-29-2013 at 08:45 PM.

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    The array that Adak speaks of doesn't keep track of how many times the dice were rolled - it keeps track of the only possible results (of two six-sided dice added together).

    In that instance, you can only get the results 2 - 12.

    The reason 13 was suggested is because arrays run from 0 to 'n'-1. So for an array "roll[13]", the highest index would be "roll[12]". The lowest index would be "roll[0]", but in reality, the lowest index you'd use is "roll[2]".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Program that output the very next date after Input a date
    By rumel.ehsan in forum C Programming
    Replies: 7
    Last Post: 03-23-2013, 11:53 AM
  2. Replies: 2
    Last Post: 05-07-2012, 07:37 AM
  3. Replies: 10
    Last Post: 03-28-2012, 10:30 AM
  4. Replies: 11
    Last Post: 03-27-2012, 11:37 PM
  5. Date program starts DOS's date
    By jrahhali in forum C++ Programming
    Replies: 1
    Last Post: 11-24-2003, 05:23 PM