random number/array

//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
int main()
{
const int size=5000;//array size
int numbertosearch=0,arrnum[size],user;
do
{
cout<<"Enter the number you want to find between 1 to 100000"<<endl;
cin>>numbertosearch;//user entry

srand(unsigned(0));

for(int i=0; i<size;i++)//for loop to generate random ##
{
arrnum[i]=(rand()%100000+1);//random number formula
cout<<arrnum[i]<<endl;
}

int found=find(arrnum,size,numbertosearch);//funtion

if(found==numbertosearch)//if result from function is equal to user entry
cout<<"The number you asked for is in array "<<endl;//print result

else if(found!=numbertosearch)//if result from function is equal to user entry
cout<<"The number you asked for is not in the array"<<endl;//print result

cout<<"Enter 1 if you want to find more number or enter 0 to exit"<<endl;
cin>>user;//ask the user if he/she wants to continue
}while(user==1);

if(user!=1)//if user does not wants to continue program ends
cout<<"Thank you"<<endl;

system ("pause");
return 0;
}

int find(const int Array[], int ArraySize, int ItemToSearch)//function
{
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 Array[i];//if found return.
return -1;//if not found return -1.
}
Last edited on
Hey there!

Could you please format your code within [ c o d e] [ / c o d e ] (w/o spaces) tags?

Also, srand(unsigned(0)); should be srand(time(0));.
Are you sending the entire array to the function or are you just sending one element of the array? The prototype says it's getting a single integer value but the actual function definition says it's getting an array.

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

int find(const int Array[], int ArraySize, int ItemToSearch)//function
i am sending the entire array.
Topic archived. No new replies allowed.