| | |
Recursion problem
Thread Solved |
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
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 ?
C Syntax (Toggle Plain Text)
/* recur.c -- recursion illustration */ #include <stdio.h> void up_and_down(int); int main(void) { up_and_down(1); return 0; } void up_and_down(int n) { printf("Level %d: n location %p\n", n, &n); /* 1 */ if (n < 4) up_and_down(n+1); printf("LEVEL %d: n location %p\n", n, &n); /* 2 */ }
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 ?
At the tip, you've got
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.
C Syntax (Toggle Plain Text)
up_and_down(1) up_and_down(2) up_and_down(3) up_and_down(4)
As you return from each recursive call, you get back the previous value of n.
•
•
Join Date: Jul 2009
Posts: 1
Reputation:
Solved Threads: 0
•
•
•
•
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
here it printsC Syntax (Toggle Plain Text)
/* recur.c -- recursion illustration */ #include <stdio.h> void up_and_down(int); int main(void) { up_and_down(1); return 0; } void up_and_down(int n) { printf("Level %d: n location %p\n", n, &n); /* 1 */ if (n < 4) up_and_down(n+1); printf("LEVEL %d: n location %p\n", n, &n); /* 2 */ }
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 ?
![]() |
Similar Threads
- (RECURSION PROBLEM)Very difficult C++ problem.. Picking coin game.., no idea.. need s (C++)
- Recursion problem (Java)
- Snowflake and recursion problem (C++)
- Recursion Problem (Java)
- Recursion problem (Java)
- Recursion Problem (Java)
- how do you reverse a linked list using recursion and return it as a string? (Java)
- recursion problem (C)
- help with recursion (C)
- C++ Beginner - #include recursion problem (C++)
Other Threads in the C Forum
- Previous Thread: The C Programming Language
- Next Thread: How to Disable UNICODE in VC++ 2008
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory dynamic execv feet fgets file fork frequency function getlasterror getlogicaldrivestrin givemetehcodez global grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft mqqueue number odf opendocumentformat owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scanf segmentationfault sequential shape single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






