recursion

J

J. Wesolowski

Hello,
This is my first approach to comp.lang.c, so hello to everyone.
I learn c programming from "Teach yourself c " by Aitken & Jones. So far
everything is going well and I'm quite excited about being able to write
my own simple programs, but I'm stuck at one point. I can't figure out
recursion of a function in a simple way. Do you have any system that
when traing to follow recursion everthing is clear? When recursion is at
one level deep, it's ok, bit deeper, uoaaa...
Thanks for help
lutnicx
 
E

Ed Morton

Hello,
This is my first approach to comp.lang.c, so hello to everyone.
I learn c programming from "Teach yourself c " by Aitken & Jones. So far
everything is going well and I'm quite excited about being able to write
my own simple programs, but I'm stuck at one point. I can't figure out
recursion of a function in a simple way. Do you have any system that
when traing to follow recursion everthing is clear? When recursion is at
one level deep, it's ok, bit deeper, uoaaa...
Thanks for help
lutnicx

Here's a resursive way to print "hello world":

#include <stdio.h>

const char txt[] = "hello world";
unsigned int idx = 0;

void prt1char() {
if (idx < sizeof txt) {
putchar(txt[idx++]);
prt1char();
} else {
putchar('\n');
}
}

int main() {
prt1char();
return 0;
}

Play with that a bit (add printfs on prt1char()s entry and exit, try moving the
"putchar('\n')" outside of the "else", try surrounding the output string in
double quotes, etc.) and maybe it'll help.

Ed.
 
A

amanayin

run this trough your debugger, It should help

/* LIST4-5.C DEMONSTRATES FUNCTION RECURSION. CALCULATES THE */
/* FRACTOTIAL OF A NUMBER */

#include<stdio.h>

unsigned int f, x;
unsigned int fractorial(unsigned int a);

int main(void)
{
puts("Enter an integer value between 1 and 14: ");
scanf("%d", &x);

if(x > 14 || x < 1)
{
printf("only values from 1 to 14 are acceptable!\n");
}
else
{
f = fractorial(x);
printf("%u fractorial equals %u\n",x ,f);
}
return 0;
}

unsigned int fractorial(unsigned int a)
{
if(a == 1)
return 1;
else
{
a *= fractorial(a-1);
return a;
}
}
 
J

Julian V. Noble

J. Wesolowski said:
Hello,
This is my first approach to comp.lang.c, so hello to everyone.
I learn c programming from "Teach yourself c " by Aitken & Jones. So far
everything is going well and I'm quite excited about being able to write
my own simple programs, but I'm stuck at one point. I can't figure out
recursion of a function in a simple way. Do you have any system that
when traing to follow recursion everthing is clear? When recursion is at
one level deep, it's ok, bit deeper, uoaaa...
Thanks for help
lutnicx

Have a look at the URL

http://www.phys.virginia.edu/classes/551.jvn.fall01/CiSE_progs/Cprogs.htm

In particular, you can see my article "Recurses!" in *.pdf format, as well as
some recursive example programs in C, suitable for running under gcc. I also
have iterative versions of several of the algorithms for comparison (by theorem,
recursion can always be replaced by iteration--it is not always easy, however).


--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
 
L

lutnicx

Thanks for help
JW


Julian V. Noble said:
Have a look at the URL

http://www.phys.virginia.edu/classes/551.jvn.fall01/CiSE_progs/Cprogs.htm

In particular, you can see my article "Recurses!" in *.pdf format, as well as
some recursive example programs in C, suitable for running under gcc. I also
have iterative versions of several of the algorithms for comparison (by theorem,
recursion can always be replaced by iteration--it is not always easy, however).


--
Julian V. Noble
Professor Emeritus of Physics
(e-mail address removed)
^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top