#include <iostream>
#include <ctime>
usingnamespace std;
//Program will load an integer array with random numbers and ask the user for a number to search for
//Then tell the user if the number is found or not
bool findIt(int* start, int* end, int value);
void randomizeIt(int* start, int length, int MaxNumber);
int main(){
//define variables
bool keepGoing = true;
constint arraySize = 10;
constint bigNumber = 100;
int myArray[arraySize];
int userValue = -1;
srand(time(0));
do{
if(userValue == -1)
randomizeIt(myArray, arraySize, bigNumber);
cout << "Enter number between 1 and " << bigNumber
<< " to search for (0=quit, -1=randomize array): ";
cin >> userValue;
if(userValue > 0){
if(findIt(myArray, &myArray[arraySize-1], userValue))
cout << " Found " << userValue << endl;
else
cout << userValue << " not found" << endl;
}else{
if(userValue == 0) keepGoing = false;
}
}while(keepGoing);
return 0;
}
// ---------------------------------------------------------------------
// here
void randomizeIt(int* start, int length, int MaxNumber) {
for(int i = 0; i < length; i++ ) {
*start = (rand()% MaxNumber) + 1;
start++;
}
}
bool findIt(int* start, int* end, int value) {
bool b = false;
while (!b){
b = true;
for(int i = 0; i < *end; i++) {
if (start[i] == value) {
b = true;
}
}
return b;
}
}
// ------------------------------------------------------------------------
//Program will load an integer array with random numbers and ask the user for a number to search for
//Then tell the user if the number is found or not
Declare an array of any constant size, fill in the array with random-generated numbers, shuffle the array. Ask the user to enter an integer number, test with this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
bool is_exists(int a[], int user_number, int SIZE)
{
for(unsignedint i=0;i<SIZE;i++)
{
if (a[i]==user_number) returntrue;
returnfalse;
}
//then in
int main()
{
//.....
if (is_exists(array_name, user_input, SIZE))
//you got it right!
else
// oops!
}