### 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.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` `````` #include 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

 ``1234567891011`` ``````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