Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

Please Help! A lottery ticket buyer purchases 10 tickets a week- alway.docx

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Nächste SlideShare
GCSE Linear Starters Higher
GCSE Linear Starters Higher
Wird geladen in …3
×

Hier ansehen

1 von 1 Anzeige

Please Help! A lottery ticket buyer purchases 10 tickets a week- alway.docx

Herunterladen, um offline zu lesen

Please Help!
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 C++ 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 55555

62483 77777 79422 85647 93121
Solution
#include using namespace std; int searchList (const int [], int, int); const int SIZE =10; int main () { int tickets [SIZE]= { 13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121 }; int winningNumber; int results; cout << \"\ Please enter this week\'s winning 5-digit number: \"; cin >> winningNumber; results=searchList (tickets, SIZE, winningNumber); if (results==-1) cout << \"None of your tickets is a winner.\ \"; else cout << \"Your ticket is a winner this week.\" << endl; return 0; } int searchList (const int list[], int numElems, int value) { int index=0; int position=-1; bool found=false; while (index < numElems && !found) { if (list[index]==value) { found=true; position=index; } index++; } system (\"pause\"); return position; }
.

Please Help!
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 C++ 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 55555

62483 77777 79422 85647 93121
Solution
#include using namespace std; int searchList (const int [], int, int); const int SIZE =10; int main () { int tickets [SIZE]= { 13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121 }; int winningNumber; int results; cout << \"\ Please enter this week\'s winning 5-digit number: \"; cin >> winningNumber; results=searchList (tickets, SIZE, winningNumber); if (results==-1) cout << \"None of your tickets is a winner.\ \"; else cout << \"Your ticket is a winner this week.\" << endl; return 0; } int searchList (const int list[], int numElems, int value) { int index=0; int position=-1; bool found=false; while (index < numElems && !found) { if (list[index]==value) { found=true; position=index; } index++; } system (\"pause\"); return position; }
.

Anzeige
Anzeige

Weitere Verwandte Inhalte

Weitere von kdavid658 (20)

Anzeige

Aktuellste (20)

Please Help! A lottery ticket buyer purchases 10 tickets a week- alway.docx

  1. 1. Please Help! 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 C++ 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 55555 62483 77777 79422 85647 93121 Solution #include using namespace std; int searchList (const int [], int, int); const int SIZE =10; int main () { int tickets [SIZE]= { 13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121 }; int winningNumber; int results; cout << " Please enter this week's winning 5-digit number: "; cin >> winningNumber; results=searchList (tickets, SIZE, winningNumber); if (results==-1) cout << "None of your tickets is a winner. "; else cout << "Your ticket is a winner this week." << endl; return 0; } int searchList (const int list[], int numElems, int value) { int index=0; int position=-1; bool found=false; while (index < numElems && !found) { if (list[index]==value) { found=true; position=index; } index++; } system ("pause"); return position; }

×