recursive function

hello
I need an opinion on an exercise to be carried out with a recursive function.
the function must indicate when and how often appears in a vector, a number entered as input. the function must be of the type: function (number, array, length), with arrays of type length.
I tried several ways but I can not figure it out. the solutions proposed by me always fall into an infinite loop.
I think the problem is that the exercise requires a recursion on a composite function of the type g (f (x)) and having no experience, I can not get out.
thx for help.

if i input number=5, the result is:

process returned -1073741819 <0xC0000005>

my solution is this:

la mia soluzione è questa:

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

#include <cassert>

using namespace std;
int maxx=22 ;
int length;
int vettore[]={5,12,33,55,23,12,1,12,45,0,56,6864,78,55,123,456,74,1,0,2,0,1};
int number,booleano;

/***************************** function***********************************/

int fRic(int numero,int array[],int indice)       //HERE ARE THE PROBLEM!!
{
    if (indice==0)                                //base for function
       {
           if (array[indice]==numero)
            return 1;
        }
    else
        return fRic(number,vettore, length-1);    //recursion
}

/************************ start program ***************************************/
int main()
{
    while(true)
    {
    length=maxx;
    cout<<"inserisci il numero da ricercare: "<<'\n';
    cin>>number;
    assert(number>=0);

    cout<<fRic(number,vettore,length)<<'\n';
    }
    return 0;
}
1
2
3
4
size_t count( int value, const int a[], size_t n )
{
   return ( n == 0 ? 0 : ( a[0] == value ) + count( value, a + 1, n - 1 ) );
}
Last edited on
For example

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

size_t count( int value, const int a[], size_t n )
{
    return ( n == 0 ? 0 : ( a[0] == value ) + count( value, a + 1, n - 1 ) );
}

int main()
{
    char si_no;

    std::srand( ( unsigned int )std::time( 0 ) );

    do
    {
        const size_t N = 22;
        int a[N];

        for ( int &x : a ) x = std::rand() % N;
        for ( int x : a ) std::cout << x << ' ';
        std::cout << std::endl;

        std::cout << "Inserisci il numero da ricercare: ";

        int value;
        std::cin >> value;

        std::cout << "Ci sono " << count( value, a, N ) << " di " << value << std::endl;

        std::cout << "Continuare (s/n)? ";
    } while ( std::cin >> si_no && ( si_no == 's' || si_no == 'S' ) );

    return 0;
} 
Last edited on
hi vlad,
thanks for the quick reply. function very elegant.
about this, I must tell you that I understood what you wrote but I have not yet finished the book on which to study C + +, there is a piece of code that I do not know.

the instruction "(n == 0? 0: ...." what exactly does?

in the allocation of initial values ​​of your function,

"Count (value, a + 1, n - 1));" you used the assignment '(a +1)', for the parameter value in a[]. What does it mean exactly?

one last question if I may: my solution, what is the problem?
thank you very much for your help
bye
the instruction "(n == 0? 0: ...." what exactly does?

That's the conditional operator:

condition ? action1 : action2

is equal to:
1
2
3
4
5
6
7
8
if ( condition )
{
    action1
}
else
{
    action2
}
Last edited on
There is so-called conditional operator in the statement below

return ( n == 0 ? 0 : ( a[0] == value ) + count( value, a + 1, n - 1 ) );

It is equivalent to

1
2
3
4
5
6
7
8
if ( n == 0 )
{
    return 0;
}
else
{
    return ( a[0] == value ) + count( value, a + 1, n - 1 );
}


As for the expression a + 1 then any array passed by value to a function is adjusted to the pointer to the first element of the array.

So inside the function a is the pointer to the first (that is with index 0) element of the original array, a + 1 is pointer to the second element and so on.

As for your function it does not summarize elements. It always will return 1 when indice will become equal to 0. For example it could be rewriten the following way.

1
2
3
4
5
size_t fRic( int numero, int array[], size_t n )
{
    if  ( n == 0 ) return 0;
    return ( ( vettore[n - 1] == numero ) + fRic( numero, vettore, n - 1 ) );
}


In my function I am moving from the beginning of an array to its end. In your approach you are moving from the end of an array to its beginning.
Last edited on
thx fransje.
thank you so much Vlad.

you are better than a compiler :)

bye and see you soon.
Topic archived. No new replies allowed.