-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA17lottery.cpp
102 lines (78 loc) · 2.05 KB
/
A17lottery.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// Lottery
// By Emily Dayanghirang
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
void displayLotteryNumbers(const int [], int);
void displayUserNumbers(const int [], int);
int countMatchingDigits(const int [], const int [], int);
void results(int);
int main()
{
const int NUM_DIGITS = 5;
const int UPPER_LIMIT = 9;
const int LOWER_LIMIT = 0;
const int RANGE = UPPER_LIMIT - LOWER_LIMIT + 1;
int lottery[NUM_DIGITS];
int user[NUM_DIGITS];
int matchingDigits;
srand(time(0));
for(int i = 0; i < NUM_DIGITS; i++)
{
lottery[i] = rand() % RANGE + LOWER_LIMIT;
}
cout << "This is the lottery game."
<< "\nEnter 5 digits (from 0 through 9)\n";
for(int j = 0; j < NUM_DIGITS; j++)
{
int userInput;
do
{
cout << "\nEnter #: ";
cin >> userInput;
if(userInput < 0 || userInput > 9)
cout << "\nEnter numbers 1-9 only.\n";
}while(userInput < 0 || userInput > 9);
user[j] = userInput;
}
displayLotteryNumbers(lottery, NUM_DIGITS);
displayUserNumbers(user, NUM_DIGITS);
matchingDigits = countMatchingDigits(lottery, user, NUM_DIGITS);
results(matchingDigits);
return 0;
}
void displayLotteryNumbers(const int lottery[], int size)
{
cout << "\nLottery Digits: ";
for (int i = 0; i < size; i++)
{
cout << lottery[i] << " ";
}
}
void displayUserNumbers(const int user[], int size)
{
cout << "\nYour Digits: ";
for (int i = 0; i < size; i++)
{
cout << user[i] << " ";
}
}
int countMatchingDigits(const int lottery[], const int user[], int size)
{
int count = 0;
for (int i = 0; i < size; i++)
{
if (lottery[i] == user[i])
count++;
}
cout << "\n\nThere is/are " << count << " matching digit/s.";
return count;
}
void results(int count)
{
if(count == 5)
cout << "\n\nYou are the grand prize winner!\n";
else
cout << "\n\nYou are not the grand prize winner.\n";
}