0

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.

Mr. F
  • 7
  • 3

1 Answers1

0

Save all the results in a single array then iterate through the array. At the end of the day you get another array and you can also iterate through it to print any value.

$array = [1, 6, 6, 6, 8, 9, 0,];
$counter = [];

foreach ($array as $value) {
   $counter[$value] ++;
}

print_r($counter);

Each key of the seccond array ($counter) has the value of the dice roll and the $value of that key has the frequency.

EDIT (wrong language): Well, I wrote PHP instead of C; im sorry :(

You can try something close to the original idea, you can check this post on how to Iterate through a C array and then just add a number as you find duplicate numbers into a new array and voila!

I hope this helps, again im sry about messing the languages :P

Community
  • 1
  • 1
Solrac
  • 924
  • 8
  • 23
  • Alright thank you. I've never used $ or the foreach() function before and I don't fully understand how this would be incorporated but I will try it. – Mr. F Oct 28 '14 at 02:23
  • Boy I screwed that up!! :( – Solrac Oct 28 '14 at 03:50
  • haha I thought it looked confusing! No worries I appreciate you taking the time to try and help a beginner. – Mr. F Oct 28 '14 at 21:48