Searching for array

My program is intended to take the runcount, which is a variable that stores a large integer like 10000, initialize it in the linearsearch function, and go through a loop of runcount times, creating a random number each time and searching for that random number in the array.

So for example: if my array consists of 10 integers and my run count is 10000, the loop will initialize 10000 times, creating a random number each time and searching for that said number in the array of 10 integers. If the number is found, I have to increment a counter, and return the counter to find the average number of times that number appeared

However, my program keeps outputting 0

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <iostream>
#include <ctime>

using namespace std;

class Myclass
{
private:
	int *ptr; //pointer to 1D dynamic array 
	int size;
public:
	Myclass(int arraySize) //overload constructor 
	{
		size = arraySize;
		ptr = new int[size];
		cout << "Enter data for an array size: " << size << endl;
		for (int i = 0; i < size; i++)
		{
			cin >> ptr[i];
		}
		cout << endl;
	}
	int linearSearch(int searchItem) const; //linear search 
	void print();
	~Myclass()
	{
		delete[] ptr;
		cout << "Memory reallocated!" << endl;
	}
};
int Myclass::linearSearch(int searchItem) const
{
	srand(time(0));
	int random;
	int counter = 0;
	for (int i = 1; i <= searchItem; i++)
	{
		for (int j = 0; j < size; j++)
		{
			random = rand() & 10;
			if (ptr[j] == random)
			{
				counter++;
			}
		}
	}
	return counter;
}
void Myclass::print()
{
	for (int i = 0; i < size; i++)
	{
		cout << ptr[i] << " ";
	}
	cout << endl;
}

double testSuccesfulSearch(const Myclass &obj, long int runCount);

int main()
{
	Myclass B(10);
	float average;
	int count;
	count = 10000;
	average = testSuccesfulSearch(B, count);

	cout << "Array size: 10" << endl;
	cout << "Array content: ";
	B.print();

	cout << "Run count: " << count << endl;
	cout << "Average number of comparisons over " << count << " random searches: " << average << endl;



	system("PAUSE");
	return 0;
}
double testSuccesfulSearch(const Myclass &obj, long int runCount)
{
	int comp;
	comp = obj.linearSearch(runCount);
	return float(comp / runCount); //cast 
}
Line 84 does integer division.

Line 40: what values that can return? Followup from line 19: there is no guarantee that array values are in the same range as the random values.

Lines 38--45. That does not "search a value from an array". It does test each array element against different value.

Line 33. That should be called at most once during the run of the program, so its logical place is at start of main().

Line 36. Why is this "lets repeat a zillion times" within "search". Each function should have one task only.

You convert between int and long int, float and double. Be more systematic. The size_t is logical for counting things.
Topic archived. No new replies allowed.