Thread: prompting user for data

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    1

    Question prompting user for data

    I am a newbie and I want to edit my dice rolling program to prompt the user to input the number of trials and then print the histogram. As it is now, I have the default for the number of times the dice will be rolled set to 1000, but I would rather let the user choose this number. How can I do this? Here is the code from my program:
    Code:
    #include <stdio.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define TRIALS 1000 
    void generate_int(int dice[] );   
    void calc_freq( int dice[],     
             int freq[]);
             void extreme( int *most_freq,     
             int *least_freq,
             int *most_num,
             int *least_num,
             int freq[]);
    void print_report ( int freq[],    data
            int *most_freq,
            int *least_freq,
            int *most_num,
            int *least_num,
            FILE *Output);
    int main(void)                        
    {    
    int dice[1000];                        
    int freq[10] = {0};
    int most_freq =0;
    int least_freq =0;
    int most_num =0;
    int least_num =1001;
    FILE *Output;
    Output = fopen("out.dat","a");                
    srand( time(NULL) );                    
    generate_int( dice );                    
    calc_freq( dice, freq);
    extreme( &most_freq, &least_freq, &most_num, &least_num,freq);
    print_report( freq, &most_freq, &least_freq, &most_num, &least_num, Output);
    return;                        
    }
    
    void generate_int( int dice[] )                
    {
    int count = 0;
      while( count < TRIALS)
       {
         dice[count] = rand() %10 + 2;
         count++;
       }
    }
    
    void calc_freq( int dice[], int freq[] )
    {
    int count = 0;                                
    int temp = 0;
    while( count < 1000)
    {
       temp = dice[count];
       switch (temp)
        {
        case 2:
            freq[0] = freq[0] + 1;
            break;
        case 3:
            freq[1] = freq[1] + 1;
            break;
        case 4:
            freq[2] = freq[2] + 1;
            break;
        case 5:
            freq[3] = freq[3] + 1;
            break;
        case 6:
            freq[4] = freq[4] + 1;
            break;
        case 7:
            freq[5] = freq[5] + 1;
            break;    
        case 8:
            freq[6] = freq[6] + 1;
            break;
        case 9:
            freq[7] = freq[7] + 1;
            break;
        case 10:
            freq[8] = freq[8] + 1;
            break;
        case 11:
            freq[9] = freq[9] + 1;
            break;
        case 12:
            freq[10] = freq[10] + 1;
            break;
        default:    printf("ERROR: INVALID DATA IN ARRAY DICE[]");
            break;
       }
     count++;
    }
    }
    
    void extreme( int *most_freq, int *least_freq, int *most_num, int *least_num, int freq[])
    {
    int count = 0;                
    
    int number = 0;
    number = freq[0];
    while(count < 11)
    {
        if( *most_num < number)
        {    
            *most_num = number;
            *most_freq = count;
        }
        if( *least_num > number)
        {
            *least_num = number;
            *least_freq = count;    
        }    
        count++;
        number = freq[count];
    }
    }    
    
    
    void print_report ( int freq[], int *most_freq, int *least_freq, int *most_num, int *least_num, FILE *Output)
    {
    int number = 0;
    int temp = 0;
    int starnumber = 0;
    int denom = 10;
    int count = 0;
    int control = 0;
    printf("\n\t_______________________________________________________________");
    while (count < 11)
    {
        temp = count + 2;                                //print data
        starnumber = freq[count]/10;    
        printf("\n\t  Number: %d", temp);
        printf("  Rolled : %d", freq[count]);
        printf("\t     ");
        fprintf(Output,"\n\t  Number: %d", temp);
        fprintf(Output,"  Rolled : %d", freq[count]);
        fprintf(Output,"\t     ");
        while( control <= starnumber)                            //printing stats
        {
            printf("*");
            fprintf(Output,"*");
            control++;
        }
        count++;
        control = 0;
        starnumber = 0;
    }
    printf("\n\t_______________________________________________________________\n");
    number = *most_freq +2;
    printf("\t  The most frequent number was: %d ",number);
    printf(" occuring %d", *most_num);
    printf(" times. \n");
    number = *least_freq +2;
    printf("\t  The least frequent number was: %d ",number);
    printf(" occuring %d", *least_num);
    printf(" times. \n");
    fprintf(Output,"\n\t_______________________________________________________________\n");
    number = *most_freq +2;
    fprintf(Output,"\t  The most frequent number was: %d ",number);
    fprintf(Output," occuring %d", *most_num);
    fprintf(Output," times. \n");
    number = *least_freq +2;
    fprintf(Output,"\t  The least frequent number was: %d ",number);
    fprintf(Output," occuring %d", *least_num);
    fprintf(Output," times. \n");
    getch();
    return;
    }
    Last edited by radical; 07-28-2005 at 09:02 AM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    Use atoi() and fgets(), both defined in stdio.h:

    Code:
    #include <stdio.h>
    #define BUFSIZE 5
    
    int main(int argc, char * * argv) {
      char buf[BUFSIZE]; //max of five digits
      printf("How many rolls? ");
      fgets(buf, BUFSIZE, stdin);
      int rolls = atoi(buf);
      printf("Number of rolls: %d\n", rolls);
    }

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    28
    PS, you should look at rewriting a lot of your code. For example, you have 11 lines of the form
    Code:
    freq[0] = freq[0] + 1;
    These could be condensed to just one. Remember that an array subscript can be a variable, not just a constant.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Yeah, and
    Code:
        temp = dice[count];
        switch (temp)
    could be
    Code:
    switch(dice[count])
    since temp is not used anywhere else. You should also indent your code consistently.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int dice[1000];
    Would be better if you used your define:

    Code:
    int dice[TRIALS];
    Code:
    int freq[10] = {0};
    There are 11 possible results, not 10.

    Code:
    void generate_int( int dice[] )                
    {
        int count = 0;
        while( count < TRIALS)
        {
            dice[count] = rand() %10 + 2;
            count++;
        }
    }
    The above generates values in the range from 2 - 11 and not 2 - 12 like you want.

    Code:
    while( count < 1000)
    {
       temp = dice[count];
       switch (temp)
        {
        case 2:
            freq[0] = freq[0] + 1;
            break;
        case 3:
            freq[1] = freq[1] + 1;
            break;
        case 4:
            freq[2] = freq[2] + 1;
            break;
        case 5:
            freq[3] = freq[3] + 1;
            break;
        case 6:
            freq[4] = freq[4] + 1;
            break;
        case 7:
            freq[5] = freq[5] + 1;
            break;    
        case 8:
            freq[6] = freq[6] + 1;
            break;
        case 9:
            freq[7] = freq[7] + 1;
            break;
        case 10:
            freq[8] = freq[8] + 1;
            break;
        case 11:
            freq[9] = freq[9] + 1;
            break;
        case 12:
            freq[10] = freq[10] + 1;
            break;
        default:    printf("ERROR: INVALID DATA IN ARRAY DICE[]");
            break;
       }
     count++;
    }
    Already hinted at, the above can simply be replaced with:

    Code:
    while( count < TRIALS )
    {
        freq[dice[count]-2]++;
        ++count;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    dice[count] = rand() %11 + 2;
    This is a "poor" way of simulating 2 six-sided die rolls. Generating the outcome that way gives each number (2 through 12) the same chance of coming up. In reality, things are a bit different. For instance, the only way to get 2 would be if the dice pair was {1, 1}. But to get 7 you could have many more dice pairs {1, 6}, {2, 5}, {3, 4} so you're much more likely to end up with 7 than 2. Your code doesn't account for that.

    You're better off with at least doing something like:
    Code:
    dice[count] = ((rand() % 6) + 1) + ((rand() % 6) + 1);
    
    or
    
    dice[count] = (rand() % 6) + (rand() % 6) + 2;
    Last edited by itsme86; 07-28-2005 at 11:51 AM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-03-2008, 09:07 PM
  2. Add/Delete Remotely a user
    By Scarvenger in forum Windows Programming
    Replies: 5
    Last Post: 03-24-2008, 08:36 AM
  3. Replies: 4
    Last Post: 04-21-2004, 04:18 PM
  4. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM
  5. Stopping a user from typeing.
    By knave in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2001, 12:21 PM