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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
// The Lottery
// Julia Mancuso
// 02-23-2018
#include <iostream>
#include <ctime>
#include <cstdlib>
using std::cout; using std::endl; using std::cin;
void assign(int[], int Size);
void draw(int, int[]);
// return true if number exists in the array
bool check( int number, const int array[], int array_sz );
void printOut( const int[], int );
void entry(int &);
int main() {
srand(time(nullptr));
const int arraySize = 10;
int user[arraySize];
cout << "Are you going to win the lottery this week? Find out now!" << endl;
int wins[arraySize];
int userInput;
entry(userInput);
assign(wins, arraySize);
draw(arraySize, wins);
const bool win = check( userInput, wins, arraySize ) ;
if(win) cout << "*** congratulations! you win. ***\n" ;
printOut(wins, arraySize);
}
void assign(int wins[], int Size) {
for (int i = 0; i < Size; ++i)
wins[i] = 0;
}
// fill array wins[] with unique random numbers in [1,1000]
void draw(int arraySize, int wins[]) {
int count = 0;
while (count < arraySize) {
int number = rand() % 1000 + 1;
if( !check( number, wins, arraySize ) ) {
wins[count] = number;
count++;
}
}
}
// return true if number exists in the array
bool check( int number, const int array[], int array_sz ) {
for( int i = 0; i < array_sz ; ++i ) { // for each value in the array
if( array[i] == number ) return true;
}
return false;
}
void entry(int &userInput) {
int i;
cout << "Enter your lottery number guess: ";
cin >> userInput;
cout << "Your number was " << userInput << endl;
}
void printOut( const int wins[], int Size ) {
cout << "Winning numbers in lottery are" << endl;
for (int i = 0; i < Size; ++i) {
cout << wins[i] << " ";
}
}
|