olorin82 wrote: |
---|
Hi I can make the program generate random numbers but still it should the following: Create a lotto number generator: Constructor takes two parameters that determine the range of numbers that the generator produces. Write a program that generates numbers for three different games: 1. Lotto A: 7 numbers 1 – 40 2. Lotto B: 6 numbers 1 – 48 3. Lotto C: 5 numbers 1 – 50 Use either generate or generate_n algorithm to generate the three sets of numbers. Print each set of numbers using a suitable algorithm and output stream iterator. Then find numbers that appear in all three sets and print them. Use set_intersection algorithm to find the numbers that appear in all three sets: • Find matching numbers from two sets and print them using for_each algorithm • Find matching number from the third set and print them using for_each algorithm To print the numbers with index numbering you need to use a function object (can also be implemented with a lambda) to remember the index between calls. Example: Lotto A: 1 12 24 36 11 15 32 Lotto B: 24 7 11 18 35 1 Matching numbers: #1: 1 #2: 11 #3: 24 Lotto C: 47 1 40 24 4 Matching numbers in three sets: #1: 1 #2: 24 After finding the matching numbers ask user if he/she wants to continue. If the answer is yes then generate another three sets of lotto numbers. MY CODE: sorry the format code button is not working for me #include <algorithm> #include <iostream> #include <random> #include <vector> int randGen_1() { return rand() % 10 + 1; } class RandGen_2 { public: RandGen_2() : numbers() { srand(time(NULL)); } int operator()(); private: vector<int> numbers; }; std::vector<int> generate_lotto(std::size_t num, int low, int high) { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> distrib(low, high); std::vector<int> lotto(num); std::generate(lotto.begin(), lotto.end(), [&]() { return distrib(gen); }); return lotto; } int main() { std::vector<int> lotto_a = generate_lotto(7, 1, 40); std::vector<int> lotto_b = generate_lotto(6, 1, 48); std::vector<int> lotto_c = generate_lotto(5, 1, 50); } void main(void) { vector<int> numbers(10); srand(time(NULL)); generate(numbers.begin(), numbers.end(), randGen_1); */ RandGen_2 randGen_2; generate(numbers.begin(), numbers.end(), randGen_2); for (auto n : numbers) cout << n << endl; } int RandGen_2::operator()() { int number; do { number = rand() % 10 + 1; } while (find(numbers.begin(), numbers.end(), number) != numbers.end()); numbers.push_back(number); return number; } |
|
|
int
. (Preferably 0 or small, positive value.)(void)
. The ()
is fine.Use either generate or generate_n algorithm to generate the three sets of numbers. |
std::generate
/std::generate_n
simply with random number generator can yield same value more than once. I would use std::iota
and std::shuffle
for "lotto".
|
|
lotto a 2 8 16 24 40 37 23 lotto b 10 19 23 17 42 13 lotto c 20 8 16 44 23 |
homework instructions wrote: |
---|
Use set_intersection algorithm to find the numbers that appear in all three sets |
|
|
lotto a #1: 2 #2: 9 #3: 10 #4: 11 #5: 19 #6: 20 #7: 40 lotto b #1: 2 #2: 3 #3: 17 #4: 22 #5: 41 #6: 48 lotto c #1: 2 #2: 3 #3: 15 #4: 37 #5: 39 lotto_ab #1: 2 lotto_ac #1: 2 lotto_bc #1: 2 #2: 3 lotto_abc #1: 2 |
|
|
|
|
Winning numbers 17 21 23 37 38 59 My numbers 14 18 19 43 51 57 My winning numbers NONE! Winning numbers 4 28 37 46 47 51 My numbers 2 13 21 24 33 49 My winning numbers NONE! Winning numbers 15 35 51 54 56 57 My numbers 10 19 27 39 52 59 My winning numbers NONE! Winning numbers 4 9 21 33 52 57 My numbers 3 37 47 49 55 57 My winning numbers 57 Winning numbers 7 18 22 25 29 39 My numbers 8 24 38 39 44 58 My winning numbers 39 Winning numbers 2 16 21 23 35 48 My numbers 19 20 31 50 55 58 My winning numbers NONE! Winning numbers 19 23 24 30 36 58 My numbers 9 14 17 21 49 52 My winning numbers NONE! Winning numbers 17 32 33 46 48 59 My numbers 4 9 18 41 57 59 My winning numbers 59 Winning numbers 6 10 13 21 24 46 My numbers 6 21 26 32 38 40 My winning numbers 6 21 Winning numbers 25 33 34 38 42 44 My numbers 3 16 19 25 35 56 My winning numbers 25 |
|
|
|
|