Actually, it looks like you were
very close, layanM. Your formula is backwards, resulting in the counts for each star's output being zero. Other than that, you pretty much had it, although you forgot the random number seed, which ensures your numbers really are random.
Check this out:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
# include <iostream>
#include <windows.h>
#include <time.h>
using namespace std;
int main()
{
int die1=0,die2=0,sum=0,roll=20000;
int freq[12]={0};//frequency totals
srand(time(NULL));//set random number seed
//20000 rolls
while (roll>0)
{
//simulate die roll
die1=rand()%6+1;
die2=rand()%6+1;
sum=die1+die2;//sum roll
freq[sum-1]++;//increment count
roll--;
}
//output frequency as a histogram
for (int i=1;i<12;i++)
{
//output stars to represent percentages
for (int s=(freq[i]*100/20000);s>0;s--)
cout<<"*";
cout<<endl;
}
return 0;
}
|
**
*****
********
**********
*************
****************
*************
***********
********
*****
** |
IF you divide by 20000 first, and then multiply by 100 to get your percentage, you will get no output because these are integers and all decimal values round to 0 before being multiplied by 100. Even in this case, the percentages are rounded because there's only about 94 stars. If you change the code to output roll totals, you will see there is a difference in rolls each time, but the histogram will nearly always look the same, with the most common rolls in the middle because there are more combinations equaling those totals.
At any rate, it looks like some minor variable name changes, and the addition of a second array that holds these percentage values that should be added to the last loop would meet the requirements. I've already got the key differences figured out, but this should get you moving in the right direction. If you're tearing your hair out unable to figure out what to change to meet all requirements, I'll show you what I came up with later. (BTW, there was nothing wrong with most of your code, I just wrote it in my own style.)