1. ## Help Please With Arrays.

I wrote the following program to answer the question:

Write a program that simulates the rolling of two dice. The program should use rand to roll the first die and should use rand again to roll the second die. The sum of the two values should then be calculated. Note: Since each die can show an integer value from 1 to 6, then the sum of the two values will vary from 2 to 12, with 7 being the most frequent sum and 2 and 12 being the least frequent sums. There are 36 possible combinations of the two dice. Your program should roll the two dice 36,000 times. Use a single-subscripted array to tally the numbers of times each possible sum appears. Print the results in a tabular format. Also, determine if the totals are reasonable (i.e., there are six ways to roll a 7, so approximately one sixth of all the rolls should be 7).
#include<iostream.h>
#include<iomanip.h>
#include<stdlib.h.>
#include<time.h>
int rollDice(void);
int sum;

int main()
{
const int arraysize=12;
int frequencyOfSum[]={0};

for (int roll=1;roll<=36000;roll++)
{ rollDice();
++frequencyOfSum[rollDice()];
}

cout<<"Sum"<<setw(10)<<"Frequency"<<endl;
for(sum=2;sum<=arraysize;sum++)
cout<<setw(4)<<sum<<setw(14)<<frequencyOfSum[rollDice()]<<endl;
return 0;
}

int rollDice(void)
{
srand(time(0));
int die1,die2;

die1=1+rand()%6;
die2=1+rand()%6;
sum=die1+die2;
return sum;
}

2. ## Re: Help Please With Arrays.

Originally posted by lyna

Code:
```int main()
{
const int arraysize=12;
int frequencyOfSum[arraysize]={0};

for (int roll=1;roll<=36000;roll++)
{ rollDice();	<--thiscalldoes nothing
++frequencyOfSum[rollDice()];
}

cout<<"Sum"<<setw(10)<<"Frequency"<<endl;
for(sum=2;sum<=arraysize;sum++)
cout<<setw(4)<<sum<<setw(14)<<frequencyOfSum[sum]<<endl;
return 0;
}

int rollDice(void)
{
srand(time(0));
int die1,die2;

die1=1+rand()%6;
die2=1+rand()%6;
sum=die1+die2;
return sum;
}```

I changed whats in red,
and about your rand calls; using % takes away the randomness because it only looks at the upper bits,; you should do a search on google for srand and rand and see other , better ways to use them.

Popular pages Recent additions