random number/function

I am getting a couple of errors in this program...please helpp..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  //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

using namespace std;

int find(const int, int,int);//function prototype

int main()
{
	const int 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
	else if(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(const int 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]
	return true;

	else if (Array[i]!=ItemToSearch)//if the user entry is found in the return false[i]
	return false;
	}
}
It would help if you mentioned the errors so we don't have to search for them ourselves...

Though one noticeable error is:

1
2
int find(const int, int,int);//function prototype
int find(const int Array[], int ArraySize, int ItemToSearch)//function prototype\ 
the second isn't a prototype anyways.
oh..but that is just documentation..
errors: 'find' not all control paths return a value
int forcing to bool true or false(performance warning)
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.
i am kinda confused right now
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)
          return true; //it is found
}
return false; //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;


By the way, you should return bool not int.
Last edited on
i am confused because to the program looks right and its not working.
Topic archived. No new replies allowed.