// Programming Assignment : Lottery Application
// Programming Environment: Bloodshed
// Program Description: This program generates a non repetitive random numbers
// for the lottery Application using rand() & srand(). It asks user their choice
// of lucky numbers which is unique. Then it processes the result how many matches
// the user has and gives congratulations if they manage to match 5 out of 5.
// Date/Time: 2/25/2010 3:08:47 AM
//---------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
//---------------------------------------------------------------------------
// Named Constants
const int MAX_ELEMENTS = 5;
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
bool Unique_Lottery_Value(int[], int, int);
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int main (void)
{
cout.setf (ios::fixed, ios::floatfield);
cout.setf (ios::showpoint);
cout.precision (2);
int Lottery[5];
int User[5];
for(int i = 0; i < 5; ++i) {
Lottery[i] = rand() % 10;
}
int current_index = 0;
unsigned seed = time(0);
srand(seed);
while (current_index < 5) {
int lottery_value = rand() % 10;
// Unique_Lottery_Value(Lottery, current_index, lottery_value);
// To get computer unique value
if(Unique_Lottery_Value(Lottery, current_index, lottery_value)) {
Lottery[current_index] = lottery_value;
++ current_index;
}
}
// Getting the unique value from the user.
int Last_Index = -1;
// Value is 0 after one iteration!! (Maintains the index of last element loaded).
int Search_Index; // Used to check or search array for duplicates.
int User_Num; // Value entered by user.
for (int count = 1; count <= MAX_ELEMENTS; ++count) {
cout << endl << "Enter Your Lucky Number: ";
cin >> User_Num;
if (Last_Index >= 0) {
Search_Index = 0;
while (Search_Index <= Last_Index && User_Num != User[Search_Index]) {
++ Search_Index;
}
if (Search_Index > Last_Index) {
User[Search_Index] = User_Num;
++ Last_Index;
} else {
cout << endl << "Your Input Duplicates Entry Number"
<< " "<< (Search_Index + 1) << " " << "Please Re-Enter" << endl;
--count;
}
} else {
User[0] = User_Num;
Last_Index = 0;
}
}
//This was my first attempt to ge the unique value from the user
/*
do {
cout << endl << endl << "Enter your 1st lucky num: ";
cin >> User[0];
}
while (User[0] > 9 || User[0] < 0);
do {
cout << endl << "Enter your 2nd lucky no without repetition: ";
cin >> User[1];
}
while (User[1] == User[0] || (User[1] > 9 || User[1] < 0));
do {
cout << endl << "Enter your 3rd Lucky no without repetition: ";
cin >> User[2];
}
while (User[2] == User[1] || User [2] == User[0] || (User[2] > 9 || User[2] < 0) );
do {
cout << endl << "Enter your 4rd Lucky no without repetition: ";
cin >> User[3];
}
while (User[3] == User[2] || User[3] == User[1] || User [3] == User[0] || (User[3] > 9 || User[3] < 0));
do {
cout << endl << "Enter your 5rd Lucky no without repetition: ";
cin >> User[4];
}
while (User[4] == User[3] || User[4] == User[2] || User [4] == User[1] || User [4] == User [0] || (User[4] > 9 || User[4] < 0));
*/
cout << endl << endl << "The Numbers you Entered: ";
for(int i = 0; i < 5; ++i) {
cout << User[i] << " ";
}
cout << endl << "Today's Winning Numbers: ";
for(int i = 0; i < 5; ++i) {
cout << Lottery[i] << " ";
}
int match = 0;
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
if (Lottery[i] == User[j]) {
++ match;
}
}
}
cout << endl << endl << "Your Luck Today!!" << endl << "You Managed to match: "
<< match << "/5.";
if (match == 5) {
cout << endl << "Congratulations: You won $99,000,000";
}
cout << endl << endl << endl << "Depress Any Key to Continue...";
_getch ();
return 0;
}
//---------------------------------------------------------------------------
// Function Definitions follow.
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
// Checks if the random numbers generated in unique or not.
bool Unique_Lottery_Value(int array[], int index, int lottery_pick){
if(index > 0) {
for(int i = 0; i < index; ++i) {
if (lottery_pick == array[i] || lottery_pick == array[i - 1] || lottery_pick == array[i + 1]){
return false;
}
}
}
return true;
}
//---------------------------------------------------------------------------
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.