Randomized array number search??

Having a bit of trouble coming up with the functions, any help would be nice.

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
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <ctime>
using namespace 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;
	const int arraySize = 10;
	const int 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;
		}
	}
// ------------------------------------------------------------------------
wzrd wrote:
1
2
//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(unsigned int i=0;i<SIZE;i++)
   {
     if (a[i]==user_number) return true;
  return false;
}
//then in 
int main()
{
//.....
if (is_exists(array_name, user_input, SIZE))
//you got it right!
else
// oops!
}
Last edited on
Topic archived. No new replies allowed.