I need the source code in C++ for the following: Write a program that determines
ID: 3662121 • Letter: I
Question
I need the source code in C++ for the following:
Write a program that determines if the user is a winner in a lotto game. A lotto ticket purchaser gets 10 tickets a week, always playing the same 10 5-digit number combos. The program will initialize an array with these numbers and then lets the player enter this week's winning 5-digit number. The program should perform both a binary search and a linear search through the list of the player's numbers and report whether or not one of the tickets is a winner that week. ( I have seen examples of the program with just binary or just linear searches; this needs to integrate both.)
The numbers to be used are as follows and the array must be created with the values in this starting order: 13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121.
Once the program asks the user to input the winning 5 digit number, it should output whether or not they won.
Explanation / Answer
#include<iostream>
using namespace std;
int binarySearch(int [], int. int);
const int SIZE = 10;
int main()
{
int lotteryNbr[SIZE] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};
int results;
int luckyNbr;
cout<<"Enter the winning lottery number for this week";
cin >> luckyNbr;
results = binarySearch(lotteryNbr, SIZE, luckyNbr);
if(results == -1)
cout<<"Im Sorry. You do not have a winning lottery ticket. ";
else
{
cout<<"Congratulations! Your ticket number"<<results<<"is the winning ticket. ";
}
system("PAUSE");
return 0;
}
int binarySearch(int array[], int size, int value)
{
int first = 0, last =size -1, middle, position = -1;
bool found = false;
while(!found && first <= last)
{
middle = (first + last)/2;
if(array[middle]==value)
{
found = true;
position = middle;
}
else if(array[middle] > value)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.