#include<iostream.h>

int rek(int i) {
double s=0.9;
if (i)
s+= rek (i/10)+i%10;
return int (s+0.5);
}
int main () {
cout << rek (1234);
}

We have test. This is one of the question from last year. My question is:

From where is solution/result: 15??

I'm beginning, where main () is, und rek(1234) goes to the top, where rek (int i).

And then came if (i) or if (1234). Is it now one recursion, and how it goes on?

> And then came if (i) or if (1234). Is it now one recursion, and how it goes on?
The first recursion is i/10, which in integer arithmetic would be rek(123)

After a couple more divisions by 10, you end up with a rek(0) call which goes no further.

> And then came if (i) or if (1234). Is it now one recursion, and how it goes on?
The first recursion is i/10, which in integer arithmetic would be rek(123)

After a couple more divisions by 10, you end up with a rek(0) call which goes no further.

Ok, but my question is: make I first recursion and then calculation, or?

when I'm by f(0), I'm going back to the top or?

Pls, could you be so kind and explain me only steps in line where s+=rek (i/10)+i%10;

Put cout statements at the beginning of rek() and after the statement that calls rek() again. Print out all the values you're interested in (i and s) and watch what prints out. Then you can see what is happening.

s+= rek (i/10)+i%10;

Each time you arrive at this statement, if i is not 0, rek() is called with i/10.

When i reaches 0, s is rounded and returned. Then the last digit if the current i is added to the returned s, and that value is added to the current s.

Basically, you have a confusing function. To make it a little more understandable, and you can display more values, you could change the function to:

int rek(int i) 
{
    double s=0.9;
    if (i)
    {
        s+= rek (i/10);
        s+= i%10;
    }
    return int (s+0.5);
}

This simply separates the compund statement adding to s. To follow the program, add the cout's:

int rek(int i) 
{
    double s=0.9;
    cout << "entered  rek -- i=" << i << endl;
    if (i)
    {
        s+= rek (i/10);
        cout << "returned rek -- i=" << i << "  s=" << s << endl;
        s+= i%10;
        cout << "     % added --  i=" << i << "  s=" << s << endl;
    }
    return int (s+0.5);
}

Put cout statements at the beginning of rek() and after the statement that calls rek() again. Print out all the values you're interested in (i and s) and watch what prints out. Then you can see what is happening.

s+= rek (i/10)+i%10;

Each time you arrive at this statement, if i is not 0, rek() is called with i/10.

When i reaches 0, s is rounded and returned. Then the last digit if the current i is added to the returned s, and that value is added to the current s.

Basically, you have a confusing function. To make it a little more understandable, and you can display more values, you could change the function to:

int rek(int i) 
{
    double s=0.9;
    if (i)
    {
        s+= rek (i/10);
        s+= i%10;
    }
    return int (s+0.5);
}

This simply separates the compund statement adding to s. To follow the program, add the cout's:

int rek(int i) 
{
    double s=0.9;
    cout << "entered  rek -- i=" << i << endl;
    if (i)
    {
        s+= rek (i/10);
        cout << "returned rek -- i=" << i << "  s=" << s << endl;
        s+= i%10;
        cout << "     % added --  i=" << i << "  s=" << s << endl;
    }
    return int (s+0.5);
}

thanks
unfortunately, i'm having this theory in book, i was asking direct question:

i=1234
how program goes on

practical example, that was my question. You are writing theory here.

Now, it's to late. I'm having test in 15 Min.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.