473,386 Members | 1,694 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

recursion

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

Nov 13 '05 #1
5 4582


On 10/30/2003 3:52 PM, J. Wesolowski wrote:
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.

Nov 13 '05 #2
Ed Morton <mo****************@lucent.com> spoke thus:
Here's a resursive way to print "hello world":


That was evil!! ;)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 13 '05 #3
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;
}
}

Nov 13 '05 #4
"J. Wesolowski" wrote:

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...ogs/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
jv*@spamfree.virginia.edu
^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
Nov 13 '05 #5
Thanks for help
JW
Julian V. Noble <jv*@nowhere.virginia.edu> wrote in message
news:3F***************@nowhere.virginia.edu...
"J. Wesolowski" wrote:

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...ogs/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
jv*@spamfree.virginia.edu
^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

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

Nov 13 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
12
by: da Vinci | last post by:
Greetings. I want to get everyone's opinion on the use of recursion. We covered it in class tonight and I want a good solid answer from people in the "know" on how well recursion is accepted...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
2
by: Csaba Gabor | last post by:
I suppose spring fever has hit at alt.math.undergrad since I didn't get any rise from them when I posted this a week ago, so I am reposting this to some of my favorite programming groups: I'm...
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
19
by: Kay Schluehr | last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
18
by: MTD | last post by:
Hello all, I've been messing about for fun creating a trial division factorizing function and I'm naturally interested in optimising it as much as possible. I've been told that iteration in...
13
by: robert | last post by:
My code does recursion loops through a couple of functions. Due to problematic I/O input this leads sometimes to "endless" recursions and after expensive I/O to the Python recursion exception. What...
20
by: athar.mirchi | last post by:
..plz define it.
35
by: Muzammil | last post by:
int harmonic(int n) { if (n=1) { return 1; } else { return harmonic(n-1)+1/n; } } can any help me ??
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.