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 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
int main()
{
// The balls value is as follows
enum ball { Yellow=1, Blue=2, Red=3, Purple=4, Orange=5, Green=6, Maroon=7, Black=8 };
// Build a vector or an array ( probably called Urn) size [24]
// There are three balls of each colour in the urn.
ball urn[24] = { Yellow, Yellow, Yellow, Blue, Blue, Blue, Red, Red, Red,
Purple, Purple, Purple, Orange, Orange, Orange, Green, Green, Green,
Maroon, Maroon, Maroon, Black, Black, Black };
// Tally Array counts the occurrence of each answer
// note: positions 0,1,2, are not used
int tally[25] = {0} ; // initialise to all zeroes
// Remember this is random choice without replacement
// (Hint: swap chosen ball with last ball and remove last ball from choices
// ( Do this twice).
// simpler to randomly shuffle the balls in the urn and pick the first three
std::srand( std::time(0) ) ;
{
// repeat this block 1000 times
// http://en.cppreference.com/w/cpp/algorithm/random_shuffle
std::random_shuffle( urn, urn+24 ) ; // shuffle the balls
// hint: total = ball1 + ball2 + ball3;
const int total = urn[0] + urn[1] + urn[2] ; // pick the first three
// hint: tally[total]++;
++tally[total] ;
}
// TO DO
// compute grand total, average
// print results
}
|