-
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;
}
-
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);
}
-
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.
-
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.
-
Would be better if you used your define:
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;
}
-
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;