//this is C++ program how to modify the programm so it fulfill the new requierem
ID: 674345 • Letter: #
Question
//this is C++ program how to modify the programm so it fulfill the new requieremnts?
// Modify the function so that it returns the position in the array of its third parameter
// or -1 if its third parameter does not appear in the array.
#include <iostream>
using namespace std;
const int nchoices = 11;
int choices[] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};
int search (int [], int, int);
int main()
{
int choice;
cout<<"Please enter this week's 5-digit winning lottery number: ";
cin >> choice;
int where = search(choices, nchoices, choice);
if (where == -1)
{
cout<<"you did not win."<<endl;
}
else
{
cout<<"You win!" <<endl;
}
system("pause");
return 0;
}
int search (int elts[], int size, int target)
{
for (int index = 0; index < size - 1; index++)
if (elts[index] == target)
return 0;
return -1;
}
Explanation / Answer
#include <iostream>
using namespace std;
const int nchoices = 11;
int choices[] = {13579, 26791, 26792, 33445, 55555, 62483, 77777, 79422, 85647, 93121};
int search (int [], int, int);
int main()
{
int choice;
cout<<"Please enter this week's 5-digit winning lottery number: ";
cin >> choice;
int where = search(choices, nchoices, choice);
if (where == -1)
{
cout<<"you did not win."<<endl;
}
else
{
cout<<"You win!" <<endl;
}
system("pause");
return 0;
}
int search (int elts[], int size, int target)
{
for (int index = 0; index < size - 1; index++)
if (elts[index] == target)
return index;
return -1;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.