I have the entire code done where the user can choose number of sides
of the two dice, the total number of rolls
, and it prints a histogram of the values rolled
. I cannot figure out how to make the frequency
of each value appear in the histogram beside their respective numbers. I have what I believe is all the relevant code, if not I will add more. I've looked through this site and found mainly matlab code
.
case 2:
printf("\nEnter number of trials: ");
fgets(runinput, sizeof(runinput), stdin);
sscanf(runinput,"%d",&n);
if (n > nmax){
printf("Your choice of %d has exceeded the max value of trials. Value set to max of %d.",n,nmax);
n = nmax;
printf("\nRolling dice...");
tried++;
}
else{
printf("\nRolling dice...");
}
for (i=0; i <= s; i++){
a[i] = 0;
}
for (i=0; i <=n-1 ; i++){
d1 = rand()%s;
d2 = rand()%s;
result = d1+d2;
x = random()%(2*s-1);
a[result]++;
results[i] = a;
}
break;
case 3:
if (tried != 0){
printf("\nPrinting histogram...\n");
for (i=0; i <= 2*s-2; i++){
freq = results[i];
printf("%5i| %10i - ", i+2, freq);
for (j=0; j < a[i]+2; j++){
while (a[i] > 0){
if (a[i] >= 100){
printf("X");
a[i]=a[i]-100;
}
else if (a[i]>=10){
printf("x");
a[i]=a[i]-10;
}
else {
printf("*");
a[i]=a[i]-1;
}
}
}
printf("\n");
}
puts("X = 100, x = 10, * = 1\n");
break;
All I'm trying to add is the frequency of which each number occurs to the histogram, any help and explanation is appreciated, I'm only in CS239
so I'm still learning. Thank you in advance!
EDIT: what code do I need to use an array to calculate the frequency of each number rulled? I sometimes find people doing similar things but its with much more advanced programming techniques than I know how to use or will be accepted by my instructor.