Velocity Reviews > C++ > recursion

# recursion

jw
Guest
Posts: n/a

 12-01-2005
Hi all,i have a recursive function and in main for ex it takes 5 as a
parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
why doesnt the result equal to 1 because of this=>
if(n==1)
return 1

-----
int sum(int n){
if(n==1)
return 1;
else
return sum(n-1)+n;
}
void main(){
cout<<sum(5);
}

Josh Mcfarlane
Guest
Posts: n/a

 12-01-2005
jw wrote:
> Hi all,i have a recursive function and in main for ex it takes 5 as a
> parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
> why doesnt the result equal to 1 because of this=>
> if(n==1)
> return 1
>
>
> -----
> int sum(int n){
> if(n==1)
> return 1;
> else
> return sum(n-1)+n;
> }
> void main(){
> cout<<sum(5);
> }

So output should be
sum(5-1) + 5
5 + 4 + sum(4-1) and onward
So you should end up with
5 + 4 + 3 + 2 + 1, or 15 if my math serves me correctly.

What's the problem?

Alf P. Steinbach
Guest
Posts: n/a

 12-01-2005
* jw:
> Hi all,i have a recursive function and in main for ex it takes 5 as a
> parameter,n'll equal to 5 and than decrease.

In a sense the parameter's value decreases, but don't take that too
literally.

Each call of the function has it's own parameter value.

> At the end it ll be 1 and
> why doesnt the result equal to 1 because of this=>
> if(n==1)
> return 1

That specifies only the result for that call of the function.

> int sum(int n){
> if(n==1)
> return 1;
> else
> return sum(n-1)+n;
> }
> void main(){

The 'main' return type must be 'int'.

See FAQ item 29.3, "Should I use void main or int main?".

> cout<<sum(5);
> }

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Alf P. Steinbach
Guest
Posts: n/a

 12-01-2005
* Josh Mcfarlane:
> jw wrote:
> > Hi all,i have a recursive function and in main for ex it takes 5 as a
> > parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
> > why doesnt the result equal to 1 because of this=>
> > if(n==1)
> > return 1
> >
> >
> > -----
> > int sum(int n){
> > if(n==1)
> > return 1;
> > else
> > return sum(n-1)+n;
> > }
> > void main(){
> > cout<<sum(5);
> > }

>
> So output should be
> sum(5-1) + 5
> 5 + 4 + sum(4-1) and onward
> So you should end up with
> 5 + 4 + 3 + 2 + 1, or 15 if my math serves me correctly.
>
> What's the problem?

I think the problem is you solved jw's homework problem instead of what

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Ron Natalie
Guest
Posts: n/a

 12-01-2005
jw wrote:
> Hi all,i have a recursive function and in main for ex it takes 5 as a
> parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
> why doesnt the result equal to 1 because of this=>
> if(n==1)
> return 1
>
>
> -----
> int sum(int n){
> if(n==1)
> return 1;
> else
> return sum(n-1)+n;
> }
> void main(){
> cout<<sum(5);
> }
>

Main returns int by the way.

While the sun(1) is the last one CALLED, it's not the one that
returns to main, it returns to the sum(2) call.

Cy Edmunds
Guest
Posts: n/a

 12-01-2005
"jw" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Hi all,i have a recursive function and in main for ex it takes 5 as a
> parameter,n'll equal to 5 and than decrease.At the end it ll be 1 and
> why doesnt the result equal to 1 because of this=>
> if(n==1)
> return 1
>
>
> -----
> int sum(int n){
> if(n==1)
> return 1;
> else
> return sum(n-1)+n;
> }
> void main(){
> cout<<sum(5);
> }
>

The code above is the same as:

int sum1() {
return 1;
}

int sum2() {
return sum1()+2;
}

int sum3() {
return sum2()+3;
}

int sum4() {
return sum3()+4;
}

int sum5() {
return sum4()+5;
}

int main(){ // note: the return type of main() is int, not void
cout<<sum5();
}

Josh Mcfarlane
Guest
Posts: n/a

 12-01-2005
Alf P. Steinbach wrote:
> I think the problem is you solved jw's homework problem instead of what

Whoops, I figured he was wondering why it didn't output 1, and figured
showing him step by step what it was doing would be easiest way to
understand.

Josh McFarlane

jw
Guest
Posts: n/a

 12-01-2005

Josh Mcfarlane wrote:
> Alf P. Steinbach wrote:
> > I think the problem is you solved jw's homework problem instead of what
> > he literally asked for.

>
> Whoops, I figured he was wondering why it didn't output 1, and figured
> showing him step by step what it was doing would be easiest way to
> understand.
>
> Josh McFarlane

it is not my homework i saw it in a book and wondered,and if it had
been a homework i wouldnt have asked it to the group because the best
way to learn is doing homeworks without any help.thanks to people who
helped me..