You need to write a function that, using recursion, could find the entered value, without recursion, it seems to have done so. Here is the code

using namespace std; void find(int size, int kluch, int *massiv, int *index) { int j = 0; for (int i = 0; i < size; i++) { j++; if (massiv[i] == kluch) { index[j] = i + 1; } else j--; } if (j > 1) { cout << "\nЧисло >" << kluch << "< с индексами "; for (int k = 1; k <= j; k++) cout << index[k] << " "; cout << "имеется в списке" << endl; } else if (j == 1) { cout << "\nЧисло >" << kluch << "< с индексом "; for (int k = 1; k <= j; k++) cout << index[k] << " "; cout << "имеется в списке" << endl; } else cout << "\nЧисло >" << kluch << "< не найдено в списке" << endl; } void find_recursion(int size, int kluch, int *massiv, int *index)///////////////////////////////////////////////////// { } 

Closed due to the fact that the essence of the question is incomprehensible by the participants aleksandr barakin , ߊߚߤߘ , Vladimir Martyanov , αλεχολυτ , Kromster 18 Apr '17 at 4:50 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • using it_vec = vector <int> :: iterator; it_vec find_rec (it_vec it1, it_vec it2, int k) {return (it1 == it2)? it2: (* it1 == k)? it1: find_rec (++ it1, it2, k); } - vados inferno

1 answer 1

Approach recursive functions like "how to solve this problem, solving a smaller problem"? And - to all functions - with the requirement that the function performs one logical action. Your function both looks for and displays the answer - these are completely different things.

So, there is an array, its length, the key value. How are we looking for an item? Let's go with the end (so as not to suffer with the displacement of the index from the beginning). If the last element of the array is the one we need, we will return it. If not, we will look in the array for one less element:

 int find(int* array, int size, int key) { if (array[size-1] == key) return size-1; // Искомый элемент найден // Иначе ищем его среди `size-1` элементов return find(array,size-1,key); } 

Everything? No, because you still have to decide what to do if the element is not found (in this case, our function will call itself all the time - endless recursion will result). Since there are no indices less than 0, let's return -1 as a sign of absence. When do we know that the item is not? When searching in an empty array. So you need to first add a check.

 if (size == 0) return -1; // не найден 

So finally:

 int find(int* array, int size, int key) { if (size == 0) return -1; // Не найден if (array[size-1] == key) return size-1; // Искомый элемент найден // Иначе ищем его среди `size-1` элементов return find(array,size-1,key); } 

Everything. Now we organize the output in another function, for example,

 void find_message(int* array, int index) { if (index < 0) { cout << "Key not found\n"; return; } cout << "Key " << array[index] << : was found in position " << index << endl; } 

Well, in main() or somewhere else ...

 int array[SIZE] = ... ... int idx = find(array,SIZE,key); find_message(array,idx); 
  • Thank you very much. Very much help out! I sat for several days, I could not figure out how to do it. - Xom9ik
  • If you are satisfied, close the question, marking the answer as accepted. - Harry