473,385 Members | 2,015 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,385 software developers and data experts.

Recursion

Write a function that produces the same output as MyFunction but without the
use of recursion.
( perfect C# is not a requirement, you may write in pseudo code )
Aug 13 '07 #1
14 1946
I'm sorry, no one told me there would be a test today....
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"seema" <se***@discussions.microsoft.comwrote in message
news:AD**********************************@microsof t.com...
Write a function that produces the same output as MyFunction but without
the
use of recursion.
( perfect C# is not a requirement, you may write in pseudo code )


Aug 13 '07 #2
This is not a test I just took some samples from book and trying to solve it
.. Please help me.

"seema" wrote:
Write a function that produces the same output as MyFunction but without the
use of recursion.
( perfect C# is not a requirement, you may write in pseudo code )

Aug 13 '07 #3
Can you clarify what MyFunction does? What is it's output?

"seema" wrote:
This is not a test I just took some samples from book and trying to solve it
. Please help me.

"seema" wrote:
Write a function that produces the same output as MyFunction but without the
use of recursion.
( perfect C# is not a requirement, you may write in pseudo code )
Aug 13 '07 #4
Nevermind. I see your previous post. To do it without recursion, just
figure out what it is really doing. You can do this with a loop.

"ModelBuilder" wrote:
Can you clarify what MyFunction does? What is it's output?

"seema" wrote:
This is not a test I just took some samples from book and trying to solve it
. Please help me.

"seema" wrote:
Write a function that produces the same output as MyFunction but without the
use of recursion.
( perfect C# is not a requirement, you may write in pseudo code )
>
>
Aug 13 '07 #5
Hi,

where is Myfunction code?
"seema" <se***@discussions.microsoft.comwrote in message
news:AD**********************************@microsof t.com...
Write a function that produces the same output as MyFunction but without
the
use of recursion.
( perfect C# is not a requirement, you may write in pseudo code )


Aug 13 '07 #6


"ModelBuilder" <Mo**********@discussions.microsoft.comwrote in message
news:EC**********************************@microsof t.com...
Nevermind. I see your previous post. To do it without recursion, just
figure out what it is really doing. You can do this with a loop.

"ModelBuilder" wrote:
>Can you clarify what MyFunction does? What is it's output?

"seema" wrote:
This is not a test I just took some samples from book and trying to
solve it
. Please help me.

"seema" wrote:

Write a function that produces the same output as MyFunction but
without the
use of recursion.
( perfect C# is not a requirement, you may write in pseudo code )

Without giving away one possible solution....I will write this in
descriptive, non pseudo, non actual code :)

I would not use a loop in my own code, rather I would rely on the framework
for doing it for me. You can do this using the Reverse method of the Array
class, after converting the string to a character array, then pass the
results of the Reverse method to the constructor of the string class.
Voila, results!

:)

HTH,
Mythran
Aug 13 '07 #7
According to my textbook on Pascal, written by Robert L. Kruse, any
recussive problem can (and should) be made into a more efficient non-
recursive problem.

The solution is to employ a stack, do stuff you need to do in a loop,
then pop off the stack stuff that you put off. It's straightforward,
once you get used to it. In fact I find recursion harder to understand
than a loop.

In fact I'm writing code for traversing an N-ary tree non-recursively
right now.

Just put your mind to it.

RL

Aug 13 '07 #8
raylopez99 wrote:
According to my textbook on Pascal, written by Robert L. Kruse, any
recussive problem can (and should) be made into a more efficient non-
recursive problem.

The solution is to employ a stack, do stuff you need to do in a loop,
then pop off the stack stuff that you put off. It's straightforward,
once you get used to it. In fact I find recursion harder to understand
than a loop.
If you refactor a recursive algorithm so that it uses an explicit stack,
it's still essentially recursive.

Many algorithms implemented recursively can in fact be implemented
iteratively, _and_ without using a stack. Tail recursion is the
classic, obvious example. But if all you're doing is moving the stack
from the compiler-generated code to your own code, then you've done
little else but to obfuscate the underlying algorithm and create more
work for yourself, without changing the basic recursive nature of the
algorithm.

Pete
Aug 13 '07 #9
Okay. (BTW, it would correctly be referred to as MyMethod):

public static MyMethod MyMethod(MyMethod myMethod)
{
return myMethod;
}

Works for me, friend! Garbage in, garbage out....

--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"seema" wrote:
Write a function that produces the same output as MyFunction but without the
use of recursion.
( perfect C# is not a requirement, you may write in pseudo code )

Aug 14 '07 #10
seema wrote:
Write a function that produces the same output as MyFunction but without the
use of recursion.
( perfect C# is not a requirement, you may write in pseudo code )
Hint: look at string.ToCharArray() and Array.Reverse().
--
Tom Porterfield
Aug 14 '07 #11
On Aug 14, 11:18 am, Peter Bromberg [C# MVP]
<pbromb...@yahoo.yohohhoandabottleofrum.comwrote :
Okay. (BTW, it would correctly be referred to as MyMethod):

public static MyMethod MyMethod(MyMethod myMethod)
{
return myMethod;

}

Works for me, friend! Garbage in, garbage out....

--
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"seema" wrote:
Write a function that produces the same output as MyFunction but without the
use of recursion.
( perfect C# is not a requirement, you may write in pseudo code )- Hide quoted text -

- Show quoted text -
I liked this response.
I just looked up the definition of recursion:

Recursion (noun): See 'Recursion'

Aug 14 '07 #12

-----Original Message-----
From: raylopez99 [mailto:ra********@yahoo.com]
Posted At: Tuesday, 14 August 2007 8:30 AM
Posted To: microsoft.public.dotnet.languages.csharp
Conversation: Recursion
Subject: Re: Recursion

According to my textbook on Pascal, written by Robert L. Kruse, any
recussive problem can (and should) be made into a more efficient non-
recursive problem.

The solution is to employ a stack, do stuff you need to do in a loop,
then pop off the stack stuff that you put off. It's straightforward,
once you get used to it. In fact I find recursion harder to understand
than a loop.

In fact I'm writing code for traversing an N-ary tree non-recursively
right now.

Just put your mind to it.
I reckon that a good example of recusion is writing a function that
processes an include directive in a file and that the include files can
contain includes.

Parsing directory structures and trees also lend themselves naturally to
recursion.

(The example often used of factorials doesn't).
Aug 14 '07 #13
>
I reckon that a good example of recusion is writing a function that
processes an include directive in a file and that the include files can
contain includes.

Parsing directory structures and trees also lend themselves naturally to
recursion.

(The example often used of factorials doesn't).
Hi,

Back in my univ days I was taugh recursion using Hanoi towers :)
Aug 14 '07 #14
On Aug 13, 3:43 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.comwrote:
raylopez99wrote:
Many algorithms implemented recursively can in fact be implemented
iteratively, _and_ without using a stack. Tail recursion is the
classic, obvious example.
And many cannot I suppose? :-)

I was thinking--and so far with my N-ary tree it seems to be working--
of storing the 'state variables' of the algorithm (in my case the
nodes that are next to go through the iterative loop)--on the stack.
So you're not really wasting memory, since these nodes are tiny, nor
are you replicating the itteration, insofar as I can tell. And I
can't figure out any other way of traversing and/or building the N-ary
tree, without adding pointers within the nodes, which, come to think
of it, is not a bad idea (though storing pointers within the nodes
also is a cost, in terms of extra memory used by each node), but not
the way I'm proceeding at the moment.

RL
Aug 14 '07 #15

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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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:
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
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,...
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.