#include <iostream>
#include <ctime>
#include <cstdlib>
#include <algorithm> // std::random_shuffle
#include <iterator> // std::begin, std::end
int main()
{
const int N = 5 ;
std::srand( std::time(0) ) ;
// the actual results are the five numbers at the beginning of the array
int lottoresults[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
// generate 5 non-repeating numbers for lottoresults by
// generating a random permutation of the elements in the array
// and then picking the first five
// see: http://e...content-available-to-author-only...e.com/w/cpp/algorithm/random_shuffle
std::random_shuffle( std::begin(lottoresults), std::end(lottoresults) ) ;
// or: std::random_shuffle( lottoresults, lottoresults+10 ) ; // legacy C++
// print them out
std::cout << "lotto winning numbers: " ;
for( int i = 0 ; i < N ; ++i ) std::cout << lottoresults[i] << ' ' ;
std::cout << '\n' ;
// numbers entered by the user
int userinput[N] = { 5, 9, 0, 8, 1 } ; // say, as an examplew
std::cout << "user input: " ;
for( int i = 0 ; i < N ; ++i ) std::cout << userinput[i] << ' ' ;
std::cout << '\n' ;
// check for matching numbers
int cnt_matches = 0 ; // #matches
std::cout << "matching numbers: " ;
for( int i = 0 ; i < N ; ++i ) // for each number entered by the user
{
const int user_num = userinput[i] ;
// check if the number number entered by the user is a winning number
// (could use std::find() instead of writing a loop of out own)
for( int j = 0 ; j < N ; ++j ) // for each winning number
{
if( user_num == lottoresults[j] ) // is it the same as user_num?
{
// yes, we got a match
++cnt_matches ;
std::cout << user_num << ' ' ;
}
}
}
std::cout << "\nnumber of matches: " << cnt_matches << '\n' ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3RpbWU+CiNpbmNsdWRlIDxjc3RkbGliPgojaW5jbHVkZSA8YWxnb3JpdGhtPiAvLyBzdGQ6OnJhbmRvbV9zaHVmZmxlCiNpbmNsdWRlIDxpdGVyYXRvcj4gLy8gc3RkOjpiZWdpbiwgc3RkOjplbmQKCmludCBtYWluKCkKewogICAgY29uc3QgaW50IE4gPSA1IDsKCXN0ZDo6c3JhbmQoIHN0ZDo6dGltZS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