Connecting Tech Pros Worldwide Help | Site Map

recursion

jw
Guest
 
Posts: n/a
#1: Dec 1 '05
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
#2: Dec 1 '05

re: recursion


jw wrote:[color=blue]
> 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);
> }[/color]

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
#3: Dec 1 '05

re: recursion


* jw:[color=blue]
> 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.[/color]

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.

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

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

[color=blue]
> int sum(int n){
> if(n==1)
> return 1;
> else
> return sum(n-1)+n;
> }
> void main(){[/color]

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

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


[color=blue]
> cout<<sum(5);
> }[/color]

--
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
#4: Dec 1 '05

re: recursion


* Josh Mcfarlane:[color=blue]
> jw wrote:[color=green]
> > 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);
> > }[/color]
>
> 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?[/color]

I think the problem is you solved jw's homework problem instead of what
he literally asked for.

--
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
#5: Dec 1 '05

re: recursion


jw wrote:[color=blue]
> 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);
> }
>[/color]
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
#6: Dec 1 '05

re: recursion


"jw" <jackwht@gmail.com> wrote in message
news:1133476524.692164.269870@o13g2000cwo.googlegr oups.com...[color=blue]
> 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);
> }
>[/color]

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
#7: Dec 1 '05

re: recursion


Alf P. Steinbach wrote:[color=blue]
> I think the problem is you solved jw's homework problem instead of what
> he literally asked for.[/color]

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
#8: Dec 1 '05

re: recursion



Josh Mcfarlane wrote:[color=blue]
> Alf P. Steinbach wrote:[color=green]
> > I think the problem is you solved jw's homework problem instead of what
> > he literally asked for.[/color]
>
> 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[/color]

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..

Closed Thread