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 5 4583
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.
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.
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. 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".
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". This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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....
|
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...
|
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;
|
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...
|
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...
|
by: Kay Schluehr |
last post by:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496691
|
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...
|
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...
|
by: athar.mirchi |
last post by:
..plz define it.
|
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 ??
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |