Lottery Winners Modification Modify the program you wrote for Programming Challe
ID: 3621006 • Letter: L
Question
Lottery Winners ModificationModify the program you wrote for Programming Challenge 2 (Lottery Winners) so it performs a binary search instead of a linear search.
I will attach the code I already have for the linear search. If someone could help me figure out how to do the binary search I would greatly appreciate it!
//This program allows a lottery ticket buyer who purchases 10 tickets a week
//to enter 5 numbers to determine if he or she's number's is a winner or loser.
#include<iostream>
using namespace std;
int main()
{
int winningNumbers[]={13579,26791,26792,33445,55555,62483,77777, 79422,85647, 93121}; //"Lucky" lottery numbers
int numbers; //To Hold lottery numbers
int count; //Used to check numbers
do
{
cout<<"Enter 5 numbers and I will tell you if you won: "; //Allows user to enter 5 numbers
cin>> numbers;
if(numbers<00000||numbers>99999)
//Input Validation, lets user know the did not put in enough or too many numbers
cout<<"Input Validation: You need to put in 5 numbers. ";
}
while(numbers<00000||numbers>99999); //Used to determine if correct numbers were used
{
count= (winningNumbers + numbers) / 2; //Used to check the numbers
if(numbers==winningNumbers[count]) //Used to determine the winner
{
cout<<"Congratulations, those are the winning numbers! "; //Lets the user know the numbers are winners
return 0;
}
cout<<"Sorry, those are not the winning numbers. "; //Lets user know the numbers are not winners
return 0;
}
Explanation / Answer
please rate - thanks
/This program allows a lottery ticket buyer who purchases 10 tickets a week
//to enter 5 numbers to determine if he or she's number's is a winner or loser.
#include<iostream>
using namespace std;
int main()
{
int winningNumbers[]={13579,26791,26792,33445,55555,62483,77777, 79422,85647, 93121}; //"Lucky" lottery numbers
int numbers; //To Hold lottery numbers
int count; //Used to check numbers
int max=10,low=0,mid;
do
{
cout<<"Enter 5 numbers and I will tell you if you won: "; //Allows user to enter 5 numbers
cin>> numbers;
if(numbers<00000||numbers>99999)
//Input Validation, lets user know the did not put in enough or too many numbers
cout<<"Input Validation: You need to put in 5 numbers. ";
}while(numbers<00000||numbers>99999); //Used to determine if correct numbers were used
max--;
while(low<=max )
{mid=(low+max)/2;
if(winningNumbers[mid]<numbers)
low = mid + 1;
else
{if( winningNumbers[ mid ]>numbers)
max = mid - 1;
else
{max=-1;
}
}
}
if(max<0)
cout<<"Congratulations, those are the winning numbers! "; //Lets the user know the numbers are winners
else
cout<<"Sorry, those are not the winning numbers. "; //Lets user know the numbers are not winners
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.