answersLogoWhite

0


Best Answer

int recursiveNFactorial (int n) {

if (n < 2) return 1;

if (n == 2) return n;

else return n * recursiveNFactorial (n - 1);

}

int iterativeNFactorial (int n) {

int result = 2;

if (n < 2) return 1;

while (n > 2) result *= n--;

return result;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Find an example or a recursive procedure and represent it as an iterative procedure?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you choose between recursion and iteration?

Some problems cry out for recursion. For example, an algorithm might be defined recursively (e.g. the Fibonacci function). When an algorithm is given with a recursive definition, the recursive implementation is straight-forward. However, it can be shown that all recursive implementations have an iterative functional equivalent, and vice versa. Systems requiring maximum processing speed, or requiring execution within very limited resources (for example, limited stack depth), are generally better implemented using iteration.


What are the advantages of using a recursive function in c?

Advantages:Through Recursion one can Solve problems in easy way whileits iterative solution is very big and complex.Ex : tower of HanoiYou reduce size of the code when you use recursive call.Disadvantages :Recursive solution is always logical and it is verydifficult to trace.(debug and understand)Before each recursive calls current values of the variblesin the function is stored in the PCB, ie process controlblock and this PCB is pushed in the OS Stack.So sometimes alot of free memory is require for recursivesolutions.Remember : whatever could be done through recursion could bedone through iterative way but reverse is not true.


What is iteration in c programming?

Iteration means what it says: the act of repeating a process. Each repetition of the process is itself an iteration, and the results of one iteration can be applied in the next iteration. Counting from 1 to 10 is an example of an iterative process because each iteration increments the counter by 1. Iteration should not be confused with recursion. Although similar, a recursion occurs when a function calls itself. Such functions are known as recursive functions and these make use of the call stack to remember the "state" of each function prior to the next call, thus allowing those states to be restored when the recursive calls return (known as "unwinding"). Since the call stack must be maintained regardless of the depth of calls, recursive routines that do not need to remember the state of each recursion are inefficient, and are often better implemented as iterative loops. However, this may require nested iterations and, if the depth is too variable or complex to be calculated at compile time, or the maximum depth would be greater than 16 then the cost of recursion will often be preferred to the increased code size iteration would incur. Even so, recursive functions can often be inline expanded by the compiler as iterative functions, thus simplifying the source code without sacrificing the performance.


What is recursive call in terms of algorithm?

A recursive call in an algorithm is when a function (that implements this algorithm) calls itself. For example, Quicksort is a popular algorithm that is recursive. The recursive call is seen in the last line of the pseudocode, where the quicksort function calls itself. function quicksort('array') create empty lists 'less' and 'greater' if length('array') &le; 1 return 'array' // an array of zero or one elements is already sorted select and remove a pivot value 'pivot' from 'array' for each 'x' in 'array' if 'x' &le; 'pivot' then append 'x' to 'less' else append 'x' to 'greater' return concatenate(quicksort('less'), 'pivot', quicksort('greater'))


What is recursive algorithm?

Algorithm can be defined as an interpretable, finite set of instructions for dealing with contigencies and accompanying task that has recognizable end-points for given inputs. It is a tool for solving a well computational problem. A recursive algorithm is one which calls itself.

Related questions

How do you choose between recursion and iteration?

Some problems cry out for recursion. For example, an algorithm might be defined recursively (e.g. the Fibonacci function). When an algorithm is given with a recursive definition, the recursive implementation is straight-forward. However, it can be shown that all recursive implementations have an iterative functional equivalent, and vice versa. Systems requiring maximum processing speed, or requiring execution within very limited resources (for example, limited stack depth), are generally better implemented using iteration.


What are the advantages of using a recursive function in c?

Advantages:Through Recursion one can Solve problems in easy way whileits iterative solution is very big and complex.Ex : tower of HanoiYou reduce size of the code when you use recursive call.Disadvantages :Recursive solution is always logical and it is verydifficult to trace.(debug and understand)Before each recursive calls current values of the variblesin the function is stored in the PCB, ie process controlblock and this PCB is pushed in the OS Stack.So sometimes alot of free memory is require for recursivesolutions.Remember : whatever could be done through recursion could bedone through iterative way but reverse is not true.


Are all patterns recursive?

No, patterns with terms that are not based upon previous terms are not recursive. Example: i * i where i is the nth term of the pattern.


What do you mean by recursive function explain with suitable example?

Type your answer here...


What is operate?

A procedure, particularly a surgical procedure or military procedure.


What are the example of procedure?

an example of a procedure that involves a inspection is


What is iteration in c programming?

Iteration means what it says: the act of repeating a process. Each repetition of the process is itself an iteration, and the results of one iteration can be applied in the next iteration. Counting from 1 to 10 is an example of an iterative process because each iteration increments the counter by 1. Iteration should not be confused with recursion. Although similar, a recursion occurs when a function calls itself. Such functions are known as recursive functions and these make use of the call stack to remember the "state" of each function prior to the next call, thus allowing those states to be restored when the recursive calls return (known as "unwinding"). Since the call stack must be maintained regardless of the depth of calls, recursive routines that do not need to remember the state of each recursion are inefficient, and are often better implemented as iterative loops. However, this may require nested iterations and, if the depth is too variable or complex to be calculated at compile time, or the maximum depth would be greater than 16 then the cost of recursion will often be preferred to the increased code size iteration would incur. Even so, recursive functions can often be inline expanded by the compiler as iterative functions, thus simplifying the source code without sacrificing the performance.


What is the difference between iterative queries and recursive queries of dns?

Recursive queries When a client system sends a recursive query to a local name server, that local name server must return the IP address for the friendly name entered, indicate that it can't find an address, or return an error saying that the requested address does not exist. Name servers do not refer the client system requesting a recursive query to other DNS servers. When answering recursive queries, the originating client does not receive address information directly from any DNS server other than the local name server. Typically, the local name server will first check DNS data from its own boot file, cache, database, or reverse lookup file. If the server is unsuccessful in obtaining the answer from those local sources, it may contact other DNS servers for assistance using iterative queries and then pass the information it receives back to the client that originated the name resolution request. Iterative queries In iterative queries, name servers return the best information they have. Although a DNS server may not know the IP address for a given friendly name, it might know the IP address of another name server likely to have the IP address being sought, so it sends that information back. The response to an iterative query can be likened to a DNS server saying, "I don't have the IP address you seek, but the name server at 10.1.2.3 can tell you." The process is straightforward. Here's one example in which a local name server uses iterative queries to resolve an address for a client: The local name server receives a name resolution request from a client system for a friendly name (such as www.techrepublic.com). The local name server checks its records. If it finds the address, it returns it to the client. If no address is found, the local name server proceeds to the next step. The local name server sends an iterative request to the root (the "." in .com) name server. The root name server provides the local name server with the address for the top-level domain (.com, .net, etc.) server. The local name server sends an iterative query to the top-level domain server. The top-level domain server replies with the IP address of the name server that manages the friendly name's domain (such as techrepublic.com). The local name server sends an iterative request to the friendly name's domain name server. The friendly name's domain name server provides the IP address for the friendly name (www.techrepublic.com) being sought. The local name server passes that IP address to the client. It seems complicated, but the process completes in a matter of moments. Or, if an address isn't found, a 404 error message is returned to the client.


What are the examples of procedure?

an example of a procedure that involves a inspection is


What is recursive methods?

A recursive method is a method that can invoke itself. The classical example is N factorial...int nfact (int N) {if (N == 2) return N else return N * nfact (N - 1);}The example suffers from truncation issues, because N Factorial gets very large, very quickly, with relatively small values of N, and ordinary integers do not support that. The answer, however, is sufficient for the question of "what is a recursive method?"


How do you do recursive pattern rule?

a recursive pattern is when you always use the next term in the pattern... for example 4,(x2+1) 9,(x2+1) 19,(x2+1) 39,(x2+1) 79,(x2+1) 159


What is is a recursive formula?

A recursive formula is one that references itself. The famous example is the Fibonacci function: fib(n) := fib(n-1) + fib(n-2), with the terminating proviso that fib(0) = 0 and fib(1) = 1.