//generate 5000 random number.
//ask the user for the number they want to search in the array.
//send the instruction to the function
//function search for the number in random number list stored in an Array
//if the number is in the list of the random number function return value
//else display the statement not found.
#include <iostream>
#include <iomanip>//if else statement
#include<ctime>//random number
#include <cstdlib>//random number
usingnamespace std;
int find(constint, int,int);//function prototype
int main()
{
constint size=5000;//array size
int numbertosearch,arrnum[size];
srand(unsigned(NULL));
cout<<"Enter the number you want to find"<<endl;
cin>>numbertosearch;//user entry
for(int i=0; i<size;i++)//for loop to generate random ##
{
arrnum[i]=(rand()%100000+1);//random number formula
bool found=find(arrnum[i],size,numbertosearch);//funtion
if(found==true)//if result from function is equal to user entry
cout<<"The number you asked for is in array"<<found<<endl;//print result
elseif(found==false)//if the result is not equal
cout<<"The number you entered is not in the list"<<endl;//print
}
system ("pause");
return 0;
}
int find(constint Array[], int ArraySize, int ItemToSearch)//function prototype
{
for (int i = 0; i < ArraySize; ++i)//for loop to find the number user asked for
{
if(Array[i]==ItemToSearch)//if the user entry is found in the return true[i]
returntrue;
elseif (Array[i]!=ItemToSearch)//if the user entry is found in the return false[i]
returnfalse;
}
}
Well you return after checking the first element. Also what happens if arraySize == 0? It will not return anything. Your best bet is probably to return false after the loop unless it finds the element somewhere in the loop.
Will you be a little more descriptive please? What exactly are you confused on?
[edit]If it was on my last post maybe this will clear up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
for(int i = 0; i < size; ++i)
{
if(array[i] == search)
returntrue; //it is found
}
returnfalse; //searched all elements and was not found
//or with 1 return statement
bool result = false;
for(int i = 0; i < size && !result; ++i)
{
result = array[i] == search; //if found result == true
}
return result;