Lottery Winners. Write a program that determines if the user is a winner in the
ID: 666959 • Letter: L
Question
Lottery Winners. Write a program that determines if the user is a winner in the lottery game. A lottery ticket buyer purchases 10 tickets a week, always playing the same 10 5-digit “lucky” combinations. 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 linear search and a binary search through the list of player’s numbers and report whether or not one of the tickets is a winner this week. Here are the numbers, and the array must be created with the values in this order: 13579, 62483, 26791, 77777, 26792, 79422, 33445, 85647, 55555, 93121. NOTE: The numbers are not in order, they must start this way.
Explanation / Answer
#include <stdio.h>
int main()
{
int first, last, middle;
long n;
int flag=0;
long array[10], temp;
array[0]=13579;array[1]=62483;array[2]=26791;array[3]=77777;array[5]=26792;
array[6]=79422;array[7]=33445;array[8]=85647;array[9]=55555;array[10]=93121;
printf("Enter your ticket number ");
scanf("%ld", &n);
printf("%ld",array[0]);
int i=0,j=0;
for(i=0;i<10;i++){
printf("i=%d array=",i);
if(n==array[i]){
printf("Hurray you won the Lottery!!!!!!");
flag=1;
}
}
if(flag==0){
printf("Sorry Better luck next time");
}
for (i = 0 ; i < ( 10 - 1 ); i++)
{
for (j = 0 ; j < 10 - i - 1; j++)
{
if (array[j] > array[j+1]) /* For decreasing order use < */
{
temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < n)
first = middle + 1;
else if (array[middle] == n) {
printf("Hurray!!!! You Won the Lottery");
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Bad Luck Your Number %d didnt get Lottery!!! Better Luck Next Time ", n);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.