A lottery ticket buyer purchases 10 tickets a week, always playing the same 10 5-digit "lucky" combinations. Write a program that initializes an 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 tickets is a winner this week. Here are the numbers:13579 26791 26792 33445 5555562483 77777 79422 85647 93121

Respuesta :

Answer:

#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;

numbers= new int[N];

cout<<"\nEnter winning lottery ticket's numbers...\n"< for (count=0; count {

cout<<"Winning Number Lottery ticket #"<<(count+1)<<": ";

cin>>numbers[count];

}

for (count=0; count<10; count++)

{

cout<<"\nEnter your lottery number:";

cin>>results;

if (results == 0)

{

cout<<"Quitting Program\n";

break;

}

int k=binarySearch(numbers,N,results);

if (k == 1)

cout<<"Winner!\n";

else

cout<<"Loser!\n";

}

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;

}

Explanation:

  • Get lottery numbers from users using loops.
  • Check if the variable k is equal to 1, then display winner.
  • Else if the variable k is not equal to 1, then display loser.
ACCESS MORE
EDU ACCESS