OCCURENCES IN ARRAY

write a program to create an array and fill it up with 20 randomly generated integers (range 0–10) and output the array.


Write a function to implement following function prototype:

void search(int array[], int length, int key);

The function takes input an integer array, the size of the array, and a key to search. The function outputs the key value and a message whether or not the key exists in the array. If the key exists, function outputs the number of occurrence of the key in the array.


I have figured most of it. i think. the problem i am having is that when i input a number that is in the array, it still says " is not present".
i also want it to print occurences only for the number present in the array. if its not present it should just print not present.

thank you. any hints would be much appreciated.


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
  #include <iostream>


using namespace std;

void search(int array[], int lenght, int key);


int main()
{
	int key;
	int array[20];

	for (int i = 0; i < 20; i++)

	{
		array[i] = rand() % 11;
		cout << array[i] << " ";
	}

	cout << "Input the number you want to search between 0 and 10. " << endl;
	cin >> key;

	search(array, 20, key);

	system("pause");
	return 0;
}



void search(int array[], int lenght, int key)
{	int occur=0;

for (int i = 0; i < lenght; i++) {

if (array[i] == key) {
	
occur += 1;


}

}
for (int i = 0; i < lenght; i++) {
	if (array[i] == key) {
		
		cout << key << " is present";
		break;
	}
	else (array[i] != key);
	{
		
		cout << key << " is not present";
		break;
	}
}
cout << occur;
}

closed account (48T7M4Gy)
Here is some pseudocode

1
2
3
4
5
6
7
8
9
10
11
occur = 0
for each arrayi
   if arrayi = key
       occur +1

if occur > 0
    print out message and occur
otherwise
    print out message and none found

return to main
Last edited on
THANK YOU! your "pseudocode" was helpful.
I was apparently thinking way too much about this question algorithm.
Topic archived. No new replies allowed.