A lottery ticket buyer purchases 10 tickets a week, always playing the same 10 5
ID: 3536402 • Letter: A
Question
A lottery ticket buyer purchases 10 tickets a week, always playing the same 10 5-digit "luchy" combinations. Write a C++ program that initializes and array or a vector with these numbers and then lets the player enter this week's winning 5-digit number. The program should perform a linear search through the list of the player's numbers and report whether or not one of the tickest is a winner this week. Here are the numbers:
13579 26791 26792 33445 55555
62483 77777 79422 85647 93121
Next, Modify Lottery Winners above so the C++ program written from above will perform a binary search instead of a linear search.
Explanation / Answer
Part 1:
#include<iostream>
using namespace std;
int binarySearch(int [], int, int);
const int size=10;
int main()
{
int *numbers;
int N;
int count;
int results;
cout<<"How many lottery tickets were purchased?:";
cin>> N;
an array
numbers= new int[N];
cout<<" Enter winning lottery ticket's numbers... "< for (count=0; count {
cout<<"Winning Number Lottery ticket #"<<(count+1)<<": ";
cin>>numbers[count];
}
for (count=0; count<10; count++)
{
cout<<" Enter your lottery number:";
cin>>results;
if (results == 0)
{
cout<<"Quitting Program ";
break;
}
int k=binarySearch(numbers,N,results);
if (k == 1)
cout<<"Winner! ";
else
cout<<"Loser! ";
}
delete [] numbers;
numbers = 0;
return 0;
}
int binarySearch(int array[], int size, int value)
{
int middle,first= 0,last=size-1;
int position=-1;
int found=0;
while (!found && first <=last)
{
middle=(first+last)/2;
if (array[middle]== value)
{
found=1;
position=middle;
break;
}
else if (array[middle]>value)
last=middle-1;
else
first=middle+1;
}
return found;
}
I hope this will helps to You !
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.