Recursion problem

Thread Solved

Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Recursion problem

 
0
  #1
Jul 6th, 2009
Hey i m reading tutrial about rucursion and got code whish isnt complicated or anything but i dunno why it prints the way it does
  1. /* recur.c -- recursion illustration */
  2.  
  3. #include <stdio.h>
  4.  
  5. void up_and_down(int);
  6.  
  7.  
  8.  
  9. int main(void)
  10.  
  11. {
  12.  
  13. up_and_down(1);
  14.  
  15. return 0;
  16.  
  17. }
  18.  
  19.  
  20.  
  21. void up_and_down(int n)
  22.  
  23. {
  24.  
  25. printf("Level %d: n location %p\n", n, &n); /* 1 */
  26.  
  27. if (n < 4)
  28.  
  29. up_and_down(n+1);
  30.  
  31. printf("LEVEL %d: n location %p\n", n, &n); /* 2 */
  32.  
  33.  
  34.  
  35. }
here it prints
Level 1: n location 0022FF60
Level 2: n location 0022FF40
Level 3: n location 0022FF20
Level 4: n location 0022FF00
LEVEL 4: n location 0022FF00
LEVEL 3: n location 0022FF20
LEVEL 2: n location 0022FF40
LEVEL 1: n location 0022FF60


first i know why it prints from 1 to 3 but see here it keeps calling itself till number is smaller than 4 right then after it finishes it goes back to that printf after that if statement whish should print the normal i whish is 1 but it doesnt it prints 4 4 3 2 1 why it got decremented there nowhere in the code where it said i-1 even when the function called itself even if we alrdy specified i as 1 and function calling itself somehow decremented it should when i==3 that if statement will terminate whish will print then orginal i ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Recursion problem

 
0
  #2
Jul 6th, 2009
At the tip, you've got
  1. up_and_down(1)
  2. up_and_down(2)
  3. up_and_down(3)
  4. up_and_down(4)
There is no subtraction, just a nested set of values stored as local parameter 'n'.
As you return from each recursive call, you get back the previous value of n.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Recursion problem

 
0
  #3
Jul 6th, 2009
oh so it keep calling function till ruc level 4 then it goes to other function whish level 3 then level 2 then level 1 again whish is the main one ?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Recursion problem

 
0
  #4
Jul 6th, 2009
Exactly.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 212
Reputation: MrNoob has a little shameless behaviour in the past 
Solved Threads: 6
MrNoob's Avatar
MrNoob MrNoob is offline Offline
Posting Whiz in Training

Re: Recursion problem

 
0
  #5
Jul 6th, 2009
oh thanks if only the book would put it in that simple
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 1
Reputation: ankush chander is an unknown quantity at this point 
Solved Threads: 0
ankush chander ankush chander is offline Offline
Newbie Poster

Re: Recursion problem

 
0
  #6
Jul 6th, 2009
Originally Posted by MrNoob View Post
Hey i m reading tutrial about rucursion and got code whish isnt complicated or anything but i dunno why it prints the way it does
  1. /* recur.c -- recursion illustration */
  2.  
  3. #include <stdio.h>
  4.  
  5. void up_and_down(int);
  6.  
  7.  
  8.  
  9. int main(void)
  10.  
  11. {
  12.  
  13. up_and_down(1);
  14.  
  15. return 0;
  16.  
  17. }
  18.  
  19.  
  20.  
  21. void up_and_down(int n)
  22.  
  23. {
  24.  
  25. printf("Level %d: n location %p\n", n, &n); /* 1 */
  26.  
  27. if (n < 4)
  28.  
  29. up_and_down(n+1);
  30.  
  31. printf("LEVEL %d: n location %p\n", n, &n); /* 2 */
  32.  
  33.  
  34.  
  35. }
here it prints
Level 1: n location 0022FF60
Level 2: n location 0022FF40
Level 3: n location 0022FF20
Level 4: n location 0022FF00
LEVEL 4: n location 0022FF00
LEVEL 3: n location 0022FF20
LEVEL 2: n location 0022FF40
LEVEL 1: n location 0022FF60


first i know why it prints from 1 to 3 but see here it keeps calling itself till number is smaller than 4 right then after it finishes it goes back to that printf after that if statement whish should print the normal i whish is 1 but it doesnt it prints 4 4 3 2 1 why it got decremented there nowhere in the code where it said i-1 even when the function called itself even if we alrdy specified i as 1 and function calling itself somehow decremented it should when i==3 that if statement will terminate whish will print then orginal i ?
[B]reply:as the last printf stement s the part of the function up n down it will be executed as many times as the function is called,but before the statement is executed the function is called again and again untill n becomes4. now when the func is excuted for the last time it executes the last printf statement and returns to its calling function to execute it's last printf statement.hence the pattern 4 3 2 1.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC