c++ - Program to roll 2 dice 36000 times with possibilities -


i have c++ homework do, question

//dietel & dietel c programming //chapter 6 arrays: page 241 exercise: 6.19 /* write program simulates rolling of 2 dice.   * program should use rand roll first die, ,  * should use rand again roll second die.   * sum of 2 values should calculated. (note: since each die  * can show integer value 1 6, sum of 2 values vary * 2 12 7 being freqent sum , 2 , 12 being least frequent * sums.) figure 6.23 shows 36 possible combinations of 2 dice.   * program should * roll 2 dice 36,000 times. use single-scripted array tally numbers of times  * each possible sum appears.   * print results in tabular format. also, determine if totals  * resonable; i.e there 6 ways roll 7, approximately 1 sixth of of * rolls should 7.  */ 

i created program, gives me output this:

sum of faces    frequency       2            0       3         4041       4            0       5         7922       6            0       7        12154       8            0       9         7936      10            0      11         3948      12            0 sum: 36001 

i don't why it's giving 0 frequency numbers

here's coded far:

#include <iostream> #include<iomanip> using namespace std;  int main() {      const int arraysize = 13;      int counter[13], sum=0;     // init counter     for(int i=0; i<13; i++)         counter[i] = 0;      int die1;      int die2;      ( int roll1 = 0; roll1 <=36000; roll1++ ) {         die1 =  1 + rand() % 6;         die2 =  1 + rand() % 6;         counter[die1+die2]++;     }      cout<<"sum of faces"<<setw(13)<<"frequency"<<endl;      for(int face=2; face<arraysize;face++)     {         cout<<setw(7)<<face<<setw(13)<<counter[face]<<endl;         sum += counter[face];     }      cout << "sum: " << sum;       return 0; } 

i need add possibilities dice well, example:

1 + 1 = 2 : 1 possibility sum 2 1 + 2 = 2 + 1 = 3 : 2 possibility sum 3 1 + 3 = 2 + 2 = 3 + 1 = 4 : 3 possibility sum 4 . . . 6 + 6 = 12 : 1 possibility sum 12 

i copy past code , run it, seem lack library rand function ( have no idea how run yours ) anyways imported library rand....

#include <bits/stdc++.h> using namespace std;  int main() {      const int arraysize = 13;      int counter[13], sum=0;     // init counter     for(int i=0; i<13; i++)         counter[i] = 0;      int die1;      int die2;      ( int roll1 = 0; roll1 <=36000; roll1++ ) {         die1 =  1 + rand() % 6;         die2 =  1 + rand() % 6;         counter[die1+die2]++;     }      cout<<"sum of faces"<<setw(13)<<"frequency"<<endl;      for(int face=2; face<arraysize;face++)     {         cout<<setw(7)<<face<<setw(13)<<counter[face]<<endl;         sum += counter[face];     }      cout << "sum: " << sum;       return 0; } 

it running correctly in machine.


Comments