473,770 Members | 1,642 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursive functions

Hi all,

1)I need your help to solve a problem.
I have a function whose prototype is

int reclen(char *)

This function has to find the length of the string passed to it.But
the conditions are that no local variable or global variable should be
used.I have to use recursive functions.

2)sizeof(int) is 2 bytes in turboC.It is 4 bytes in case of gcc.why
different compilers allocate different amount of memory?what is the
reason behind it?

Apr 1 '07
41 3381

"David Thompson" <da************ @verizon.netha scritto nel messaggio
news:6k******** *************** *********@4ax.c om...
On Thu, 05 Apr 2007 00:08:02 +0000, Richard Heathfield
<rj*@see.sig.in validwrote:
>Keith Thompson said:

<snip>
How did you first learn about recursion?

I found someone standing nearer to Douglas Hofstadter than me, and asked
him "how did you first learn about recursion?"

<OTAside: IIRC IME the base case was Steele or maybe Minsky, but the
principle obviously is invariant under a 'translation' like that.

Do you mean distance(someon e,Hofstadter) < distance(you,Ho fstadter),
which is a good example of recursion, but should properly be written
in your sentence form with 'I', or ...

distance(someon e,Hofstadter) < distance(someon e,you) which is better
as an example of heuristic or at least depth-first search?

;-o </>
That's how my teacher implements the function to add an item at the
end of a list (I copied and pasted, translated the identifiers from
Italian to English -- keeping the capitalization style, removed his
comments and added mine):

typedef struct LI {
InfoType Info;
struct LI *Next;
} ListItemType, *ListType;
#include <stdlib.h>
int EmptyList (ListType List)
/* Why don't directly use (List == NULL) ? */
{
if (List == NULL)
return 1;
else
return 0;
}
void InsertAtEnd (ListType *List, InfoType Info)
{
ListType Ptr;
if (EmptyList (*List))
{
Ptr = malloc(sizeof(T ipoElemLista));
Ptr->Next = NULL;
/* I've heard that, on 26 December 2006, someone used a
* program with that function on a DS9K on board of a small
* boat somewhere in the Pacific Ocean, and the malloc above
* had failed. */
Ptr->Info = Info;
/* What if InfoType is an array type? */
*List = Ptr;
}
else InsertAtEnd(&(( *List)->Next), Info);
/* This could easily be the ugliest looking line of code I've
* ever seen in any language, and is the most effective way of
* convincing one's students that linked lists are evil, and
* to prevent them from ever using linked lists. */
}

Why? Because this is the *only* way our textbook implements such a
function. (Actually, it uses
typedef enum {false, true} boolean;
and declares EmptyList to return such a type.) Even if the teacher
also gave an almost sane iterative version of the function (yes, it
uses a while when a for would do that, and it doesn't check the
result of malloc, but it is much less insane than the one in the
book), the only fact that he copied such a function from that book
and showed it to us caused *all* the students which took the only
exam in which he ever asked to write a function to add an element
at the end of a list, to fail.
Apr 15 '07 #41

"Army1987" <pl********@for .itha scritto nel messaggio
news:ev******** **@tdi.cu.mi.it ...
typedef struct LI {
InfoType Info;
struct LI *Next;
} ListItemType, *ListType;
#include <stdlib.h>
int EmptyList (ListType List)
/* Why don't directly use (List == NULL) ? */
{
if (List == NULL)
return 1;
else
return 0;
}
void InsertAtEnd (ListType *List, InfoType Info)
{
ListType Ptr;
if (EmptyList (*List))
{
Ptr = malloc(sizeof(T ipoElemLista));
That is, ListItemType.
Ptr->Next = NULL;
/* I've heard that, on 26 December 2006, someone used a
* program with that function on a DS9K on board of a small
* boat somewhere in the Pacific Ocean, and the malloc above
* had failed. */
Ptr->Info = Info;
/* What if InfoType is an array type? */
*List = Ptr;
}
else InsertAtEnd(&(( *List)->Next), Info);
/* This could easily be the ugliest looking line of code I've
* ever seen in any language, and is the most effective way of
* convincing one's students that linked lists are evil, and
* to prevent them from ever using linked lists. */
}

Apr 15 '07 #42

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

Similar topics

4
1920
by: Magnus Lie Hetland | last post by:
Hi! I've been looking at ways of dealing with nested structures in regexps (becuase I figured that would be faster than the Python parsing code I've currently got) and came across a few interesting things... First there is this, mentioning the (?<DEPTH>) and (?<-DEPTH>) constructs of the .NET regexp matcher: http://www.rassoc.com/gregr/weblog/archive.aspx?post=590
7
567
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
7
6132
by: Aloo | last post by:
Dear friends, If we declare a recursive function as 'inline' then does it actually convert to an iterative form during compilation ( the executable code is iterative)? Is it possible ? Please reply.
9
16844
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script type='text/javascript'> function myFunc(lev) { // if (lev) return myFunc(lev-1); var aStack=; nextFunc = arguments.callee;
5
4943
by: Digital Puer | last post by:
I got this on an interview: Is it possible to write inline recursive functions? I said yes, but there is no guarantee that the compiler will definitely inline your code even if you write "inline". Is this a right answer? It seems to be independent of whether or not the function is recursive. Another question: how can you tell if the compiler has inlined your
10
2570
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive search....would any of you be so kind to explain it to me...maybe include some examples. I would GREATLY appreciate it!!!
9
2637
by: pereges | last post by:
Hello I need some ideas for designing a recursive function for my ray tracing program. The idea behind ray tracing is to follow the electromagnetic rays from the source, as they hit the object.The object is triangulated. The rays can undergo multiple reflections, diffractions etc of the same object i.e. a ray hits a surface of the object, undergoes reflection resulting in a reflected ray which can again hit a surface, corner or edge...
3
4242
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular reference, which means it is only collected by the mark-and-sweep collector, not by reference counting. Here is some code that demonstrates it: === def outer():
6
9614
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function serialization The code itself generates a list of strings comprised of random numbers. No number will be repeated within a string, and no string will be repeated within the list of strings. Following the code is a brief discussion of how the above...
0
9595
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10232
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10059
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6682
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5454
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3974
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 we have to send another system
2
3578
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2822
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.