Number of times in the array number

I found an exercise in a C ++ book that says: "Write a function that will count the number of times a number appears in an array." Everything is in order, the program works. But the exercise also says that the function must be recursive.

How can I make such a recursive function?

#include <iostream> int count(int number, int array[], int length) { int counter = 0; for(int i = 0; i < length; i++) if(array[i] == number) counter++; return counter; } int main() { int numbers[10] = {3,4,1,2,4,5,6,5,4,5}; int number_to_search = 5; std::cout << number_to_search << " appears " << count(number_to_search, numbers, 10) << " times in the array."; return 0; } 
+4
source share
6 answers

Instead of the current loop and counter, return 0 + recursive_step or 1 + recursive_step , where recursive_step is a call to count(...) , where you increased the array pointer and decreased length . Your base register is when length == 0 , after which you simply return 0 .

A good way to understand recursion is to learn how the calculation is performed, step by step. In your example, you will do something like this:

 count(5, {3,4,1,2,4,5,6,5,4,5}) 0+count(5, {4,1,2,4,5,6,5,4,5}) 0+0+count(5, {1,2,4,5,6,5,4,5}) 0+0+0+count(5, {2,4,5,6,5,4,5}) 0+0+0+0+count(5, {4,5,6,5,4,5}) 0+0+0+0+0+count(5, {5,6,5,4,5}) 0+0+0+0+0+1+count(5, {6,5,4,5}) 0+0+0+0+0+1+0+count(5, {5,4,5}) 0+0+0+0+0+1+0+1+count(5, {4,5}) 0+0+0+0+0+1+0+1+0+count(5, {5}) 0+0+0+0+0+1+0+1+0+1+count(5,{}) 0+0+0+0+0+1+0+1+0+1+0 <---The last 0 is the base case 3 

If you are allowed to change the specification of a function, you can also do something cool called tail recursion. Instead of return 1 + count(...) add the accumulated number to evaluate the counter: int count(int number, int array[], int length, int acc)

And do return count(..., acc+1) or return count(..., acc+0) . Then, some compilers can perform tail call optimization, turning them into a loop in the compiled code. This saves memory compared to regular recursion.

+3
source

Use this count function:

 int count(int number, int array[], int length) { if (length == 0) return 0; return (number == *array) + count(number, array+1, length-1); } 
+4
source

How to try like this: -

 int count(int num, int* arr, int length) { if (!length) return 0; int c = count(num, arr+1, length-1); return arr[0] == num? c + 1: c; } int main(void) { int arr[10] = {3,4,1,2,4,5,6,5,4,5}; std::cout << count(2, arr, 10); return 0; } 
+2
source

This is what you are doing (I would not show you any code so as not to spoil your exercise).

First, recall that in order to be recursive, your function must call itself. Then consider these two points:

  • When the length parameter is zero, the return value of count(...) should be zero
  • If the length parameter is nonzero, consider the return value of count(...) for array + 1 and length-1 ; call him prior . The return value of the current count(...) will be prior+1 if array[0] is equal to number or prior when array[0] not equal to number .

When you add your code from this description, notice how you have if at the beginning of your recursive function. This if breaks your code into a base register ( length == 0 ) and a recursive step (calculating the result based on a recursive call). This is the general structure of recursive functions: you will have to reproduce this structure every time you write recursive code.

+1
source
 #include <iostream> void count(int number, int array[], int length, int &occurence) { if (*array == number) ++occurence; if (length == 1) return; else count(number, array+1, length-1, occurence); } int main() { int numbers[10] = {3,4,1,2,4,5,6,5,4,5}; int number_to_search = 5; int occurence = 0; count(number_to_search, numbers, 10,occurence); std::cout << number_to_search << " appears " << occurence << " times in the array."; } 
+1
source
  #include <iostream> #include <ctime> #include <cstdlib> using namespace std; int i, j,n,c=0, k=0; int a[1000], b[1000]; class Array { public: void input () { cout<<"Enter how many values: "; cin>>n; } void arraySeries () { cout<<"Array elements: "; srand(time(0)); for (i=0; i<n; i++) { a[i]=rand()%100+1; cout<<a[i]<<" "; } cout<<endl<<endl; cout<<"Odd elements of array: "; for (i=0; i<n; i++) { if(a[i]%2==1) { b[i]=a[i]; k++; cout<<b[i]<<" "; } } } // i want to find out how many times an odd number is found in b[i] // but i am not being able to do so. SOMEONE PLEASE HELP!! void OddSearch () { cout<<endl<<endl; for (int k=1;k<100;k++) { c=0; for (i=0;i<n; i++) { if (b[i]==k) { c++; } cout<<b[i]<<"occurs"<<c<<"times"<<endl; } } } }; int main () { Array obj; obj.input(); obj.arraySeries(); obj.OddSearch(); return 0; } 
0
source

Source: https://habr.com/ru/post/1499902/


All Articles