Your description of what the function does is actually not correct. You mention that the function:
return[s] all the indices where an element is to be found in an array. If it occurs twice or more, all the respective indices will be returned, for now it returns them in natural counting (1,2,3...) and not how the computer sees them (0,1,2,...)
However, your function declaration is this:
void searchList(int theArray[], int sizeOfTheArray, int findFor);
As you can see, the function doesn't actually return anything.
Since this is C++, you should stay away from using C-style arrays and use either std::array or std::vector instead. In this way, you won't need to supply the size of the provided array into the function. Also, as @Jerry Coffin mentioned, you should use size_t as the type to hold array indexes:
vector<size_t> searchList(const std::vector<int>& theArray, int findFor)
{
vector<size_t> indexes;
for (size_t i = 0; i < theArray.size(); ++i){
if (theArray[i] == findFor){
indexes.push_back(i + 1);
}
}
return indexes;
}
You can then handle all I/O in the main function. like this:
int main()
{
vector<int> test {87, 75, 98, 100, 100, 234, 265, 9};
auto indexes = searchList(test, 99);
if (indexes.empty()) {
cout << "Not found in array";
} else {
for (const auto &index : indexes) {
cout << index << " ";
}
}
}