Thread: Create an ASCII bar graph of die rolls HELP!!!

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    Create an ASCII bar graph of die rolls HELP!!!

    for an assignment we need to create basically a program that asks the user for the result of standard six-sided die rolls (numbers from 1 to 6). The program will prompt the user with "Enter a die roll or 0 to exit " for the first entry and “Next die roll? ” for all subsequent entries. The program will continue reading die rolls from the user until the user enters 0. At this point, the program prints two newline characters (one blank line) and finally, the bar chart showing the number of times each die roll has been entered, and then terminates.If the user enters an invalid number, the program will just ignore it, and ask for another number. Only numbers 1-6 inclusive and 0 are valid.

    Example output

    Enter a die roll or 0 to exit 6
    Next die roll? 6
    Next die roll? 7
    Next die roll? 4
    Next die roll? 0

    1 |
    2 |
    3 |
    4 | *
    5 |
    6 | **
    foaad@unix1:~/101 $ ./labQ2
    Enter a die roll or 0 to exit 4
    Next die roll? 4
    Next die roll? 4
    Next die roll? 4
    Next die roll? 4
    Next die roll? 4
    Next die roll? 4
    Next die roll? 3
    Next die roll? 2
    Next die roll? 1
    Next die roll? 0

    1 | *
    2 | *
    3 | *
    4 | *******
    5 |
    6 |

    Enter a die roll or 0 to exit 0

    1 |
    2 |
    3 |
    4 |
    5 |
    6 |
    foaad@unix1:~/101 $ ./labQ2
    Enter a die roll or 0 to exit 1
    Next die roll? 2
    Next die roll? 3
    Next die roll? 4
    Next die roll? 5
    Next die roll? 6
    Next die roll? -33
    Next die roll? 7
    Next die roll? 20
    Next die roll? -8
    Next die roll? 9
    Next die roll? 10
    Next die roll? 223
    Next die roll? 3
    Next die roll? 0

    1 | *
    2 | *
    3 | **
    4 | *
    5 | *
    6 | *

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Hannah!

    The way we work here is, you start your program. Then, if and when you get stuck, post up what you have and what the problem is, and we'll try and help. It's good to post up the requirements of the program, but it's never good to not show specifically what you're trying to do here. That is, there is always more than one way to solve a problem, and we want to narrow down the help to YOUR way.

    To be quite helpful, I'll just mention that if you had an small array of int called rolls[7], then whatever was "rolled" could be simply summed up as rolls[yourRollValue]++. That will tidy up the summing nicely.

  3. #3
    Registered User
    Join Date
    May 2013
    Posts
    2
    this is what i started doing the biggest problem i am having is just figuring our how to take the data and printing out the proper amount of stars for each section. I am really new to coding so I may have gone wrong in a lot of places

    Code:
    #include <stdio.h>
    
    
    #define SENTINEL 0
    
    
    int main(void)
    {
       int dieRoll;
       int total = 0;
       int graph[total][6];
       int one;
       int two;
       int three;
       int four;
       int five;
       int six;
    
    /* Accumulate all die rolls. */
       printf("Enter a die roll or 0 to exit ", SENTINEL);
       scanf("%d", &dieRoll);   /* Get first die roll. */
       while (dieRoll != SENTINEL)
       {
          printf("Next die roll? ", SENTINEL);
           if ( dieRoll < 0 || dieRoll > 6)
           {
               printf("Next die roll?\n");
               return 0;
           else
           {
               for(one = 0; one == 1; one++)
               {
                   scanf(" %d ", &graph[i][0]);
                   total++;
               }
               for(two = 0; two == 2; two++)
               {
                   scanf(" %d ", &graph[i][1]);
                   total++;
               }
               for(three = 0; three == 3; three++)
               {
                   scanf(" %d ", &graph[i][2]);
                   total++;
               }
               for(four = 0; four == 4; four++)
               {
                   scanf(" %d ", &graph[i][3]);
                   total++;
               }
    	    for(five = 0; five == 5; five++)
            {
                scanf(" %d ", &graph[i][4]);
                total++;
            }
    	    for(six = 0; six == 6; six++)
            {
                scanf(" %d ", &graph[i][5]);
                total++;
            }
           }
        printf("1 | %d\n", );
        printf("2 | %d\n", );
        printf("3 | %d\n", );
        printf("4 | %d\n", );
        printf("5 | %d\n", );
        printf("6 | %d\n", );
       }
    return0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Instead of int one, int two, ... int six, consolidate the rolls into one array. Otherwise your logic will have to be recalled or repeated, for each of those separate variables.

    Say you roll a 3, then

    rolls[3]++;

    Now rolls[] can help you print out the stars, in a nested loop:
    Code:
    for(each value i - 1 through 6) {
        while(starsPrinted < rolls[i]) {
             print a star and maybe a space
        }
        print a newline
    }
    If you post your code as plain text, (using the code tags), the forum software will display it with C style colors.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  2. Simplest way to create a good graph?
    By Lonehwolf in forum C Programming
    Replies: 2
    Last Post: 11-29-2010, 11:07 PM
  3. How to create graph and bar char
    By beon in forum Windows Programming
    Replies: 1
    Last Post: 07-25-2007, 11:45 PM
  4. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  5. create a graph of y=x^2
    By asimos in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2002, 12:13 AM