Beginner at C++ really struggling w/ Vectors
Sep 7, 2017 at 4:49pm UTC
Any help that can be given is greatly appreciated! This is my first C++ assignment and I am really struggling with getting it to print correctly. Long story short, I need to create a lottery app that uses random_shuffle() and sort()on a vector and passes it to a print function that will display the numbers in numerical order. I've gotten the random_shuffle aspect to work but am struggling with the sort() and getting it to print. Right now it is only printing multiple 0's.
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
void lotto(int highNum, int numSlots, vector<int >& lottoNumbers);
void displayNumbers(vector<int >& lottoNumbers);
int main()
{
int highNum;
int numSlots;;
char repeat = ' ' ;
vector<int > lottoNumbers(numSlots);
do
{
cout << "Enter the highest number: " ;
cin >> highNum;
cout << "How many numbers should be generated? " ;
cin >> numSlots;
lotto(highNum, numSlots, vector<int > lottoNumbers);
displayNumbers(vector<int > lottoNumbers);
cout << endl;
cout << "Would you like to generate another ticket? (Y or N)" ;
cin >> repeat;
}
while (repeat == 'y' || repeat == 'Y' );
}
void lotto(int highNum, int numSlots, vector<int >& lottoNumbers)
{
vector<int > allNumbers(highNum);
for (int i = 0; i < highNum; i++)
{
allNumbers[i] = i + 1;
}
random_shuffle(allNumbers.begin(), allNumbers.end());
for (int j = 0; j < numSlots; j++)
{
lottoNumbers[j] = allNumbers[j];
}
sort(lottoNumbers.begin(), lottoNumbers.end());
}
void displayNumbers(vector<int >& lottoNumbers)
{
for (int p = 0; p < lottoNumbers.size(); p++)
{
cout << lottoNumbers[p] << " " ;
}
}
Sep 7, 2017 at 6:36pm UTC
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include <random>
#include <ctime>
std::vector<int > generate_lotto_numbers( int high_num, std::size_t num_slots );
void display_numbers( const std::vector<int >& lotto_numbers );
int main()
{
char repeat = ' ' ;
do
{
int high_num;
std::cout << "Enter the highest number (999-999999): " ;
std::cin >> high_num;
if ( high_num < 999 ) high_num = 999 ; // ideally, avoid such magic numbers
else if ( high_num > 999999 ) high_num = 999999 ; // ie. define constants
// see: https://en.wikipedia.org/wiki/Magic_number_(programming)#Unnamed_numerical_constants
int num_slots;
std::cout << "How many numbers should be generated (1-" << high_num << ")? " ;
std::cin >> num_slots;
if ( num_slots < 1 ) num_slots = 1 ;
else if ( num_slots > high_num ) num_slots = high_num-1 ;
const std::vector<int > lotto_numbers = generate_lotto_numbers( high_num, num_slots );
display_numbers(lotto_numbers);
std::cout << "\nWould you like to generate another ticket? (Y or N): " ;
std::cin >> repeat;
}
while ( repeat == 'y' || repeat == 'Y' );
}
std::vector<int > generate_lotto_numbers( int high_num, std::size_t num_slots )
{
std::vector<int > all_numbers(high_num);
// fill the vector with numbers 1 ... high_num
// http://en.cppreference.com/w/cpp/algorithm/iota
std::iota( all_numbers.begin(), all_numbers.end(), 1 ) ;
// shuffle the numbers (avoid the deprecated std::random_shuffle)
// http://en.cppreference.com/w/cpp/numeric/random
static std::mt19937 rng( std::time(nullptr ) ) ; // the random number engine
// http://en.cppreference.com/w/cpp/algorithm/random_shuffle
std::shuffle( all_numbers.begin(), all_numbers.end(), rng ) ;
// select the first num_slots numbers of the randomly shuffled sequence
// this simulates a random selection without replacement
std::vector<int > lotto_numbers( all_numbers.begin(), all_numbers.begin()+num_slots ) ;
// sort the selected numbers
std::sort( lotto_numbers.begin(), lotto_numbers.end() );
return lotto_numbers ; // and return the selected numbers
}
void display_numbers( const std::vector<int >& lotto_numbers )
{
std::cout << "the randomly drawn " << lotto_numbers.size() << " numbers are\n" ;
// http://www.stroustrup.com/C++11FAQ.html#for
for ( int number : lotto_numbers ) std::cout << number << ' ' ;
std::cout << '\n' ;
}
Sep 9, 2017 at 4:33pm UTC
Thanks! This really helped me get an idea on how to pass the vector and display the results. Appreciate the help.
Topic archived. No new replies allowed.