I NEED TO REWRITE THE PROGRAM IN VECTOR . THEN WHEN THE PROGRAM IS RUNNING I NEE
ID: 3605280 • Letter: I
Question
I NEED TO REWRITE THE PROGRAM IN VECTOR.
THEN WHEN THE PROGRAM IS RUNNING I NEED TO MODIFY THE VECTOR SOLUTION BY DECLARING AN EMPTY VECTOR AND THEN USING push_back() METHOD TO LOAD UP YOUR VECTOR WITH THE DATA.
THE ORIGINAL QUESTION AND SOLUTION ARE BELOW:
Question: A lottery ticket buyer purchases 10 tickets a week, always playing the same 10 5-digit "luchy" co...
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
Orgirinal Program in array code:
// This program demonstrates the searchList function, which
// performs a linear search on an integer array.
#include <iostream>
using namespace std;
// Function prototype
int searchList(const int [], int, int);
const int SIZE = 5;
int main()
{
int tests[SIZE] = {87, 75, 98, 100, 82};
int results;
// Search the array for 100.
results = searchList(tests, SIZE, 100);
// If searchList returned -1, then 100 was not found.
if (results == -1)
cout << "You did not earn 100 points on any test ";
else
{
// Otherwise results contains the subscript of
// the first 100 in the array.
cout << "You earned 100 points on test ";
cout << (results + 1) << endl;
}
return 0;
}
//***********************************************************
// The searchList function performs a linear search on an
*
// integer array. The array list, which has a maximum of numElems *
// elements, is searched for the number stored in value. If the *
// number is found, its array subscript is returned. Otherwise, *
// -1 is returned indicating the value was not in the array.
*
//***********************************************************
int searchList(const int list[], int numElems, int value)
{
int index = 0;
// Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if the value was found
while (index < numElems && !found)
{
if (list[index] == value) // If the value is found
{
found = true;
// Set the flag
position = index;
// Record the value's subscript
}
index++;
// Go to the next element
}
return position;
// Return the position, or -1
}
Explanation / Answer
//The array has been replaced with Vector
#include <iostream>
#include <vector>
using namespace std;
// Function prototype
int searchList(vector<int> list, int, int);
const int SIZE = 5;
int main()
{
vector<int> tests;
tests.push_back(87);
tests.push_back(75);
tests.push_back(98);
tests.push_back(100);
tests.push_back(82);
int results;
// Search the array for 100.
results = searchList(tests, SIZE, 100);
// If searchList returned -1, then 100 was not found.
if (results == -1)
cout << "You did not earn 100 points on any test ";
else
{
// Otherwise results contains the subscript of
// the first 100 in the array.
cout << "You earned 100 points on test ";
cout << (results + 1) << endl;
}
return 0;
}
//***********************************************************
// The searchList function performs a linear search on an
// integer array. The array list, which has a maximum of numElems *
// elements, is searched for the number stored in value. If the *
// number is found, its array subscript is returned. Otherwise, *
// -1 is returned indicating the value was not in the array.
//***********************************************************
int searchList(vector<int> list, int numElems, int value)
{
int index = 0;
// Used as a subscript to search array
int position = -1; // To record position of search value
bool found = false; // Flag to indicate if the value was found
while (index < numElems && !found)
{
if (list.at(index) == value) // If the value is found
{
found = true;
// Set the flag
position = index;
// Record the value's subscript
}
index++;
// Go to the next element
}
return position;
// Return the position, or -1
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.