I have no idea how to start this program! I appreciate any help :)

Jerry’s Appliance has a special promotion for its week long Anniversary Sale. After Customers have picked out the merchandise they wish to buy, they can draw three balls from an urn to determine the sum of the discount they will receive on their purchase. The balls value is as follows Yellow =1 Blue =2 Red = 3 Purple =4 Orange =5 Green = 6 Maroon=7 and Black=8. There are three balls of each color in the urn. The balls are drawn without replacement by each customer but returned to the urn before the next customer draws.

Jerry would like you to simulate 1000 sets of random draws storing the sums in a tally array. Print out the values of the tally array along with an analysis of the cost of the promotion.

Remember this is random choice without replacement (Hint: swap chosen ball with last ball and remove last ball from choices ( Do this twice).

Run this program 1000 times keeping track of the result in a tally array

(Hint : tally[score]++;

Then print out results for each possible value

Score 0 0

Score 1 0

Score 2 0

Score 3 1

Score 4 3

………

Score 24 1

The expected discount is _____%
Please read your Arrays first.
here is some hint: Build a vector or an array ( probably called Urn) size [24]

Create a rand object range to start at 24 , 23, 22

For loop for 1000 draws

Add the value of three balls to give discount

Tally Array counts the occurrence of each answer

Tally Array [25] note 0,1,2,are always going to be empty

int tally [25] = {0};

total = ball1+ball2 +ball3;

tally[total]++;

grandtotal +=total

average = grandtotal /draw

median = middle value

mode = highest tally

Average = median = mode It is a normal distribution

Your print to monitor is a list of the the tally array and the average
So, it looks like that description contains a breakdown of individual tasks to do. Why not try with the first one?

Build a vector or an array ( probably called Urn) size [24]
vector <int> urn(1,24)
this creates a std::vector<int> size 1 with v[0] == 24
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
47
#include <iostream>
#include <random>
#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 and 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

    // http://en.cppreference.com/w/cpp/numeric/random
    std::mt19937 rng( std::time(nullptr) ) ; // the random number engine

    {
        // repeat this block 1000 times

        // http://en.cppreference.com/w/cpp/algorithm/random_shuffle
        std::shuffle( std::begin(urn), std::end(urn), rng ) ; // 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
}
JLBorges your code doesn't work on CodeBlocks. How can you convert this -> std::mt19937 rng( std::time(nullptr) ) ; // the random number engine

{
// repeat this block 1000 times

// http://en.cppreference.com/w/cpp/algorithm/random_shuffle
std::shuffle( std::begin(urn), std::end(urn), rng )

in Codeblocks?
You'll need to turn on support for C++11. I've never used CodeBlocks, but I'm sure it will have an option somewhere.
So basically is |td::shuffle( std::begin(urn), std::end(urn), rng ) ;| the rand ?
Enable C++11 support in CodeBlocks: http://www.cplusplus.com/doc/tutorial/introduction/codeblocks/

Or C++98 (if the compiler is really old):

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
}
This doesnt print anytging.
It won't print anything till you complete the program (TO DO indicated on lines 43-46)
1
2
3
    // TO DO
    // compute grand total, average
    // print results 
I am so confused can you help me cause my assignment is due tonight ?
Is there somebody to help me with this ?
compute grand total: sum up all the values in the array tally and the result is the grand total

compute average: divide grand total by 22.0 (avoid integer division) and the result is the average.
Note: 22.0 because positions 0,1,2 in the array are not used ( each total is in [3,24] )

print results: print out the value of the grand total and average
Last edited on
any code?
No.
Topic archived. No new replies allowed.