Question

#include <stdio.h> 
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main()
{
int i;
int d1, d2, d3;
int a[16];
    srand(time(NULL));

for(i = 0; i <= 15; i++)   
    a[i] = 0;   
for(i = 0; i < 1000; i = i + 1)   
{   
    d1 = rand() % 6 + 1;   
    d2 = rand() % 6 + 1; 
    d3 = rand() % 6 + 1;  
    ++a[d1 + d2 + d3 - 3];   
}  
char asterisks[0x400];
memset(asterisks, '*', sizeof(asterisks)); 
for(i = 0; i <= 15; i = i + 1) 
{   
    printf("%3d - ", i+3);   

    for(j=0;j<a[i];j++)
    {
    printf("%c ",'*');   

    }
    printf("\n");  
}

return 0;
}    

Updated code.
The goal is to have an asterisk histogram judging the dice rolls of 3 dice rolled 1000 times. It is to count how many combinations of sums between 3 and 18. Histogram output should look like this:

Frequency of Results for 3d6
  3 - *
  4 - **
  5 - ****
  6 - *******
  7 - *********
  8 - ***********
  9 - ************
 10 - ************
 11 - *************
 12 - **********
 13 - *************
 14 - ********
 15 - ******
 16 - ***
 17 - ***
 18 - **

This is my output as of now:

3 - * * 
4 - * * * * * * * * * * * * * * * * * * * * 
5 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
6 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * 
7 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * *     * * * * * * * * * * * * 
8 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * 
9 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
10 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
11 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * 
12 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
13 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * 
14 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * * * * * * * * * * * * * * * * * * * 
15 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * * 
16 - * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
17 - * * * * * * * * * 
18 - * * * * * * * * 
Était-ce utile?

La solution

3-18 You need array with the size of 16

int a[16]; 

a[0] indicates Number of 3 counts and 
a[1] indicates Number of 4 counts and 
....

a[15] indicates Number of 18 counts.

You need to change this for loop

 for(i = 0; i <= 15; i++)   
  a[i] = 0;   

and this statement

   a[d1 + d2+d3] = a[d1 + d2 + d3] + 1;

Modify like this

     ++a[d1 + d2 + d3 - 3];

And final for loop also, you need two loops.

for(i = 0; i <= 15; i = i + 1) 
   {   
    printf("%3d - ", i+3);   

    for(j=0;j<a[i];j++)
       {
        printf("%c ",'*');   

       }
        printf("\n");  
   }

Autres conseils

You can use memset to fill a buffer with asterisks and "%*s" printf format to print as many as you need:

char asterisks[0x400];
memset(asterisks, '*', sizeof(asterisks));

for(i = 3; i <= 18; i = i + 1)   
{   
    printf("%d - %*s\n", i, a[i], asterisks);  
}

And yeah as mentioned in the comments you should define a long enough:

int a[19];

The way to fill a char array with asterisks can be done in various ways, a few of which have been explained here, a question that was posted but a few hours ago...

On a different matter:
You're calling the rand function, without providing a seed... rand will then, by default, behave as though it was passed 1 as a seed, and your compiled program will produce not-so-random results.
Consider adding this:

#include <stdlib.h>
#include <time.h>

int main()
{
    int i;
int d1, d2;
int a[13];
    srand(time(NULL));//seed current time
}

That, and, as Jongware pointed out:

d1 = rand() % 6 + 1;
d2 = rand() %6 + 1;

just doesn't make sense:

d1 = rand() % 18 + 1;

Does just the same thing, requiring only 1 function call. But still, there is another issue with what you're doing with this int value:

You're also using an array of ints (a[13]), the declaration of which implies that the highest offset you can use is 12, the lowest (as ever) is 0. Yet, your loops start at offset 3, and go all the way up to 18...
I know this site is called stackoverflow, but your code seems to be actively working towards Buffer overflow. Fix that, please...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top