473,471 Members | 1,695 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Recursing code problem

I am using Interactive C with my handboard (68HC11) development system but
I've got a problem that I am asking for help with.
I am not new to C but learning all the time and I just cant see how to
overcome this problem.

Symptom:
Programs fail ' Runtime error' which equates to a stack error.
This is because my code is like going around and around (a snake chasing its
tail is a term I've seen used) but I cant quite come to grips with how to
overcome this problem.

Should I post some of the code up on this group for people with a better
knowledge of this problem to make suggestions - I feel its probably
something not difficult to understand but I just need a little guidance
here.

Jeff
Nov 13 '05 #1
19 3782
snowdy wrote:
I am using Interactive C with my handboard (68HC11) development system but
I've got a problem that I am asking for help with.
I am not new to C but learning all the time and I just cant see how to
overcome this problem.

Symptom:
Programs fail ' Runtime error' which equates to a stack error.
This is because my code is like going around and around (a snake chasing its
tail is a term I've seen used) but I cant quite come to grips with how to
overcome this problem.

Should I post some of the code up on this group for people with a better
knowledge of this problem to make suggestions - I feel its probably
something not difficult to understand but I just need a little guidance
here.

Post the smallest compilable snippet that exhibits the problem you're
having.

HTH,
--ag


--
Artie Gold -- Austin, Texas

Nov 13 '05 #2
snowdy wrote:

I am using Interactive C with my handboard (68HC11) development system but
I've got a problem that I am asking for help with.
I am not new to C but learning all the time and I just cant see how to
overcome this problem.

Symptom:
Programs fail ' Runtime error' which equates to a stack error.
This is because my code is like going around and around (a snake chasing its
tail is a term I've seen used) but I cant quite come to grips with how to
overcome this problem.

Should I post some of the code up on this group for people with a better
knowledge of this problem to make suggestions - I feel its probably
something not difficult to understand but I just need a little guidance
here.

Jeff


Post some code. However, your symptoms sound like you don't
have a proper termination condition. One problem with recursive
code (and I use it a lot and even wrote a column about it) is that
it is easy to create an endless loop that overwrites the stack.

Also, some problems are not well-suited to recursion: the stack
gets too deep and boom! So you ought to analyze your code to
estimate how the stack-depth grows with problem size.

You can find my column, Recurses!, at

http://www.phys.virginia.edu/classes...ogs/Cprogs.htm

--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
Nov 13 '05 #3
"snowdy" <sn****@optusnet.com.au@nospam> wrote in message
news:3f**********************@news.optusnet.com.au ...
....snip...
Symptom:
Programs fail ' Runtime error' which equates to a stack error.
This is because my code is like going around and around (a snake chasing its tail is a term I've seen used) but I cant quite come to grips with how to
overcome this problem.
....snip... Jeff


I've also run into this problem. Here's something simple to try:
==============================
/*
factorial.c
Takes one command-line argument: the number you want the factorial of.
Breaks at around 39! with the following:
run-time error R6000
- stack overflow
*/

#include <stdio.h>
#include <stdlib.h>

double fact (int n);

void main(int argc, char **argv)
{
int n;
double factorial=1;

n = atoi (argv[1]);
fact (n);
}

double fact (int n)
{
static double value=1;

if (n > 1)
value = n * fact (n-1);

printf ("%3.d! = %.0f \n", n, value);

return (value);
}

==============================

So, I have the same question. How can I increase the stack or otherwise make
this work for higher numbers?

--
ArWeGod
Nov 13 '05 #4

"ArWeGod" <Ar*****@sbcglobal.net> wrote in message

/*
factorial.c
Takes one command-line argument: the number you want the factorial > of.
Breaks at around 39! with the following:
run-time error R6000
- stack overflow
*/
double fact (int n)
{
static double value=1;

if (n > 1)
value = n * fact (n-1);

printf ("%3.d! = %.0f \n", n, value);

return (value);
}

So, I have the same question. How can I increase the stack or
otherwise make this work for higher numbers?


This surprises me, because even a relatively puny stack shouldn't overflow
at a depth of 39 functions.
What will overflow quite rapidly is the precision of a double.
Nov 13 '05 #5
In article <3F***************@sun.com>, Eric Sosman wrote:
<rant topicality="zero">
Why do people so often talk about factorials or the Fibonacci
numbers when explaining recursion? Neither is well- suited to
a recursive solution; iteration wins on practically every
measure. And for the Fibonacci numbers, iteration itself is
easily beatable; recursion is at best a poor third choice. But
look up any textbook or lecture about recursion, and what do
you find? Factorials and Fibonacci! Foolishness!

</rant>


Because factorials and fibonacci number's make such wonderfully
simple recursive functions. The crappyness of the resulting
solutions isn't a consideration when the intent is to educate.

A program that is "best" written recursively, like a program that
shows all the possible ways to make a certain amount of change,
may be too complicated for an introduction to recursion.

<rant> I think the word "recursion" should be abolished from
primmers. It just creates consternation. I could create a similar
effect by constantly using the word "ambulation" to describe the
act of going for a walk. </rant>

--
Neil Cerutti
Nov 13 '05 #6
On Fri, 29 Aug 2003 15:40:33 -0400, Eric Sosman <Er*********@sun.com>
wrote:
Why do people so often talk about factorials or the
Fibonacci numbers when explaining recursion?


For the same reason they talk about the bubble sort when introducing
sorting - it's easily understood.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 13 '05 #7

"Neil Cerutti" <ho*****@yahoo.com> wrote in message
news:bi************@ID-60390.news.uni-berlin.de...

(snip regarding recursive functions)
<rant> I think the word "recursion" should be abolished from
primmers. It just creates consternation. I could create a similar
effect by constantly using the word "ambulation" to describe the
act of going for a walk. </rant>


How about reentrant, which isn't quite the same, but related.

Along the same line, there is refreshable and serially reusable.

-- glen
Nov 13 '05 #8
"Malcolm" <ma*****@55bank.freeserve.co.uk> wrote in message
news:bi**********@newsg1.svr.pol.co.uk...

"ArWeGod" <Ar*****@sbcglobal.net> wrote in message

/*
factorial.c
Takes one command-line argument: the number you want the factorial > of.
Breaks at around 39! with the following:
run-time error R6000
- stack overflow
*/
double fact (int n)
{
static double value=1;

if (n > 1)
value = n * fact (n-1);

printf ("%3.d! = %.0f \n", n, value);

return (value);
}

So, I have the same question. How can I increase the stack or
otherwise make this work for higher numbers?


This surprises me, because even a relatively puny stack shouldn't overflow
at a depth of 39 functions.
What will overflow quite rapidly is the precision of a double.


You may have hit the nail on the head.
The value I get for 38! is 523022617466601000000000000000000000000000000.

Q1. Anybody know the limit of a double?

Q2. Anybody know of a bigger number - holder? long double? unsigned double?
etc.

I guess for this sort of thing you need to do your number handling for
overflow, etc.

--
ArWeGod
Nov 13 '05 #9
On Fri, 29 Aug 2003 22:43:33 GMT, "ArWeGod" <Ar*****@sbcglobal.net>
wrote:
This surprises me, because even a relatively puny stack shouldn't overflow
at a depth of 39 functions.
What will overflow quite rapidly is the precision of a double.

You may have hit the nail on the head.
The value I get for 38! is 523022617466601000000000000000000000000000000.

That won't cause a stack overflow.
Q1. Anybody know the limit of a double? Implementation dependent.
Q2. Anybody know of a bigger number - holder? long double? unsigned double?
etc.

Google for "big number" packages.
BTW, I just read your classification of programmers from newby to 5+
years. Apparently that was from observation, not practice?

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 13 '05 #10
"ArWeGod" <Ar*****@sbcglobal.net> writes:
Q1. Anybody know the limit of a double?


DBL_MAX
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.
Nov 13 '05 #11
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...
"ArWeGod" <Ar*****@sbcglobal.net> writes:
Q1. Anybody know the limit of a double?


DBL_MAX


So, is

#define DBL_MAX 1.7976931348623158e+308 /* max value */ // my MSVC
compiler's value

more than

523022617466601000000000000000000000000000000 * 39?

....I _think_ I get an overflow error. If not it's a recursion problem. You
tell me. I'm NOT going to do the math - I have a few critics - you work it
out!

But, please, don't attack me, just post the correct answer to help
everybody. I don't really care - I've never calculated factorials, I just
wrote the program because it was asked it on a job interview and I was
checking my work.

;-)

--
ArWeGod
Nov 13 '05 #12
"ArWeGod" <Ar*****@sbcglobal.net> writes:
"Ben Pfaff" <bl*@cs.stanford.edu> wrote in message
news:87************@pfaff.stanford.edu...
"ArWeGod" <Ar*****@sbcglobal.net> writes:
Q1. Anybody know the limit of a double?


DBL_MAX


So, is

#define DBL_MAX 1.7976931348623158e+308 /* max value */ // my MSVC
compiler's value

more than

523022617466601000000000000000000000000000000 * 39?


Yes. Written out in full, 1.7976931348623158e+308 is
17976931348623158000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 000000000,
more than a googol times larger than
523022617466601000000000000000000000000000000 * 39.

I don't know what your exact problem is, not having examined the
situation closely. Floating-point calculations are notoriously
tricky. But the problem should not be one of overflow.

--
"I hope, some day, to learn to read.
It seems to be even harder than writing."
--Richard Heathfield
Nov 13 '05 #13
R. Rajesh Jeba Anbiah wrote:

Eric Sosman <Er*********@sun.com> wrote in message news:<3F***************@sun.com>...
Why do people so often talk about factorials or the
Fibonacci numbers when explaining recursion? Neither is well-
suited to a recursive solution; iteration wins on practically
every measure. And for the Fibonacci numbers, iteration itself
is easily beatable; recursion is at best a poor third choice.
But look up any textbook or lecture about recursion, and what
do you find? Factorials and Fibonacci! Foolishness!


Factorials and Fibonacci are dealt only in beginner/intermediate
level books. That is because, it is easier to convey the recursive
logic via these simple Factorials and Fibonacci.

Professor has mentioned his recursive programs of
http://www.phys.virginia.edu/classes...ogs/Cprogs.htm
But, most of the programs that he mentioned won't be suited to explain
the recursive logic in simple beginners' book.


The 8 queens problem seems to be a good one to me for recursion,
but that's the only one that I know.

--
pete
Nov 13 '05 #14
Eric Sosman wrote:

ArWeGod wrote:

I've also run into this problem. Here's something simple to try:
[... recursive computation of factorial snipped ...]

So, I have the same question. How can I increase the stack or otherwise make
this work for higher numbers?


It is possible to express any recursive computation in
iterative form, but that doesn't mean iteration is always
the best technique to use. It is possible to express any
iterative computation in recursive form, but that doesn't
mean recursion is always the best technique to use.

If you need to compute factorials (an iffy business
at best, by the way), iteration is a better approach than
recursion.

<rant topicality="zero">

Why do people so often talk about factorials or the
Fibonacci numbers when explaining recursion? Neither is well-
suited to a recursive solution; iteration wins on practically
every measure. And for the Fibonacci numbers, iteration itself
is easily beatable; recursion is at best a poor third choice.
But look up any textbook or lecture about recursion, and what
do you find? Factorials and Fibonacci! Foolishness!

</rant>


Quite right. In my column "Recurses!" I said essentially the same thing.
Another frequently used example that is almost as bad is string
reversal. Here is an example in M$ QuickBASIC:

FUNCTION rev$ (a$)
c$ = MID$(a$, 1, 1)
IF c$ = "" THEN
rev$ = ""
ELSE
rev$ = rev$(MID$(a$, 2)) + c$
END IF
END FUNCTION

I am sure there must be C versions floating around but I haven't
time to look for one or to translate the above. Someday...
--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"Science knows only one commandment: contribute to science."
-- Bertolt Brecht, "Galileo".
Nov 13 '05 #15
Julian V. Noble <jv*@virginia.edu> scribbled the following:
Quite right. In my column "Recurses!" I said essentially the same thing.
Another frequently used example that is almost as bad is string
reversal. Here is an example in M$ QuickBASIC: FUNCTION rev$ (a$)
c$ = MID$(a$, 1, 1)
This would be more readable as:
c$ = LEFT$(a$, 1)
IF c$ = "" THEN
rev$ = ""
ELSE
rev$ = rev$(MID$(a$, 2)) + c$
END IF
END FUNCTION


--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"C++. C++ run. Run, ++, run."
- JIPsoft
Nov 13 '05 #16

On Sat, 30 Aug 2003, Randy Howard wrote:

In article <3F***************@sun.com>, Er*********@sun.com says...
<rant topicality="zero">


While we're here, a joke...

"Knock, Knock"
"Who's there?"
"Recursion."
"Recursion who?"

"Knock, Knock"
...


No, no, no! ;-)

"Knock, knock."
"Who's there?"
"Iteration."
"Iteration who?"
"Knock knock."
"Who's there?"
"Iteration."
"Iteration who?"
"Knock knock."
"Who's there?"
"Orange."
"Orange who?..."
"Knock knock."
"Who's there?"
"Recursion."
"Recursion who?"
"Recursion knock knock."
"Recursion knock knock who?"
"Recursion knock knock knock."
"Who's there?..."

-Arthur

Nov 13 '05 #17
Eric Sosman <Er*********@sun.com> wrote:
<rant topicality="zero">

Why do people so often talk about factorials or the
Fibonacci numbers when explaining recursion? Neither is well-
suited to a recursive solution; iteration wins on practically
every measure.
Not necessarily so. The following is a re-expression of the fibonacci
sequence recursive formula:

f[0] = 0
f[1] = 1
f[2] = 1
f[n] = f[(n+1)/2]^2 + f[(n-1)/2]^2 ; if n is odd
= f[n/2+1]^2 - f[n/2-1]^2 ; if n is even

Which implies a recursion of depth about ln(n) steps. Now if you do
the math, you will realize that the total number of calls does in fact
lead to O(n) which might lead you to believe that iterative is faster
(since its less complicated). However, if you cache the values as you
compute them, then this should run in about O((ln(n))^2) which should
*SKUNK* the iterative solution for large enough n.
[...] And for the Fibonacci numbers, iteration itself
is easily beatable;
By a complete table look up of course! (No this will not blow out your
memory; think about it.)
[...] recursion is at best a poor third choice.
As I pointed out above, depends on which recursive formula you are
talking about.
But look up any textbook or lecture about recursion, and what
do you find? Factorials and Fibonacci! Foolishness!


They are the easiest examples -- they can't explain trees until they
explain recursion.

--
Paul Hsieh
http://www.pobox.com/~qed/
http://bstring.sf.net/
Nov 13 '05 #18
qe*@pobox.com (Paul Hsieh) writes:
Eric Sosman <Er*********@sun.com> wrote:
And for the Fibonacci numbers, iteration itself is easily beatable;


By a complete table look up of course!


One easy way to calculate the `n'-th Fibonacci number `f' without a table
is this:

const double sqrt5 = sqrt (5.0);
const double golden = 0.5 + 0.5 * sqrt5;

int f = (int)(pow (golden, (double)n) / sqrt5 + 0.5);

Martin
Nov 13 '05 #19
On Fri, 29 Aug 2003 22:43:33 GMT, "ArWeGod" <Ar*****@sbcglobal.net> wrote:
The value I get for 38! is 523022617466601000000000000000000000000000000.


The correct value is 523022617466601111760007224100074291200000000

You aren't suffering from overflow, but from a loss of precision because a
double doesn't hold enough bits to represent the correct value.

(And 39! is 20397882081197443358640281739902897356800000000)

169! (~4.3e305) is the largest value a double can hold without overflow:

42690680090047052749392518888995665380688186360567
36103849163411117977554942180092854323970142716152
65381823013990501222156824856790750177960523574894
55946484708413412107621199803603527401512378815048
78975040568419670360154453585262827477179746400268
93725894862438400000000000000000000000000000000000
00000
--
#include <standard.disclaimer>
_
Kevin D Quitt USA 91387-4454 96.37% of all statistics are made up
Per the FCA, this address may not be added to any commercial mail list
Nov 13 '05 #20

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

Similar topics

3
by: Scott Carlson | last post by:
New to Python. Very new. I need to pass a parameter to the program I create that points to a folder. (How are params passed in?) Then, I need to be able to scroll through each file in the...
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
16
by: Rex | last post by:
Hi All - I have a question that I think MIGHT be of interest to a number of us developers. I am somewhat new to VIsual Studio 2005 but not new to VB. I am looking for ideas about quick and...
3
by: half.italian | last post by:
I'm making a small interface for copying large groups of files around a filesystem. I have a progressbar that counts the items in the destination, and increments as each new file is copied over. ...
4
by: Henrik Goldman | last post by:
I have an application which compiles on a number of different platforms. Now I'm trying to incorporate an external library which is only available to a subset of the platforms that my application...
2
by: python101 | last post by:
I have the code def calculation(n): print 'n =', n if n<=1: return n if n%2==0: print 'Even' return calculation(n-1) + calculation(n-2)...
9
by: OWeb | last post by:
Javascript and recursing subfolders assistance ------------------------- I have this script that is a free extra download from SlideShowPro. It's a great script but I feel it needs to be...
4
by: Edwin Velez | last post by:
http://msdn.microsoft.com/en-us/library/806sc8c5.aspx The URL above gives sample code for use within a Console Application. What I would like to do is use this code within a Windows Form. That...
0
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
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...
0
agi2029
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.