Connecting Tech Pros Worldwide Help | Site Map

recursion

 
LinkBack Thread Tools Search this Thread
  #1  
Old December 1st, 2005, 09:45 PM
jw
Guest
 
Posts: n/a
Default recursion

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);
}


  #2  
Old December 1st, 2005, 09:55 PM
Josh Mcfarlane
Guest
 
Posts: n/a
Default 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?

  #3  
Old December 1st, 2005, 09:55 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default 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?
  #4  
Old December 1st, 2005, 09:55 PM
Alf P. Steinbach
Guest
 
Posts: n/a
Default 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?
  #5  
Old December 1st, 2005, 09:55 PM
Ron Natalie
Guest
 
Posts: n/a
Default 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.
  #6  
Old December 1st, 2005, 09:55 PM
Cy Edmunds
Guest
 
Posts: n/a
Default 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();
}


  #7  
Old December 1st, 2005, 10:05 PM
Josh Mcfarlane
Guest
 
Posts: n/a
Default 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

  #8  
Old December 1st, 2005, 10:15 PM
jw
Guest
 
Posts: n/a
Default 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..

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.