answersLogoWhite

0

What else can I help you with?

Continue Learning about Engineering

What is a recursive function?

A recursive function is one that calls upon itself until a given result in the original call is met. Take a look at this example. Program Recursion; Uses crt; Var number:longint; Function Factorial(number:longint):longint; Begin if number > 0 then factorial:=number*factorial(number-1) else factorial:=1; End; Begin clrscr; readln(number); writeln(factorial(number)); readln; End. Note how the function factorial calls itself.


What is simulation recursion in C?

The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) { if (n==1) return 1 else return n* factorial( n-1) ; }


What is Repetition and its types data structure and algorithm?

In programming, there are two types of repetition: iterative and recursive. Both are a type of loop.Iterative loops have three general types:while loopdo loopfor loopThe while loop is the simplest loop. It tests a conditional expression at the start of each iteration. If the expression holds true, the body of the loop executes for one iteration. The loop continues to iterate until the expression does not hold true.The do loop is similar to a while loop except the conditional expression is placed at the end of each iteration. Thus the body of the loop will always execute at least once. A do loop is also known as a do until loop.The for loop is typically used to perform counted iterations. Unlike the while and do loops, a conditional expression is optional (evaluating true when omitted). In addition, a for loop can initialise a variable upon first entry to the loop and can perform an action at the end of each iteration. All clauses of the for loop are optional.Within the body of any iterative loop, the programmer can also use additional conditional expressions in conjunction with continue, break, return or goto statements. A continue statement skips the remaining portion of the loop and begins a new iteration (testing the controlling conditional expression beforehand). A break exits the loop. A return exits the function and returns control to the caller. A goto jumps to the named label. Although goto statements are frowned upon, a goto is the most efficient way of breaking out of a nested loop (a loop within a loop).Recursive loops make use of recursive function calls. That is; a function that calls itself. Recursive functions are typically used in conquer-and-divide algorithms where a large problem can be restated as one or more smaller problems of the same type, until the problem is small enough that it can be processed. This results in one or more small solutions that combine to form the larger solution.To better explain the concept, let us consider the naturally recursive function to calculate the factorial of a number. Factorials determine the number of permutations within a given set. That is, a set of {abc} has 3 elements and therefore 6 permutations: {{abc},{acb},{bac},{bca},{cab},{cba}}. For any given set of n elements, the number of permutations is the product of all positive integers in the closed range [1:n]. Thus the first few factorials are as follows:f(0) = 1f(1) = 1f(2) = 1x2 = 2f(3) = 1x2x3 = 6f(4) = 1x2x3x4 = 24Note that 0! denotes the empty set which has one permutation, the same as a set of 1. However, for all values n>1 we can see that the result is the same as saying:f(n) = f(n-1) x nIn other words, it is a recursive function. The end point is reached when n is less than 2 thus we can program this function as follows:unsigned f (unsigned n) {if (n>1)return f(n-1)*n;return 1;}If n were 4, then the first instance of the function will be invoked as f(4). This in turn invokes f(3), f(2) and finally f(1) which returns 1 to f(2), which returns 2 to f(3) which returns 6 to f(4) which finally returns 24 to the caller.[It should be noted that factorials produce huge numbers and are only practical for calculating f(12) using 32-bit integrals and f(20) using 64-bit integrals. If you need larger factorials, you must use a user-defined type capable of handling larger integrals.]Unlike an iterative loop which simply cycles from one state to the next never to return to a previous state, a recursive loop maintains state between calls and revisits that state upon each return. This technique is the essence of all backtracking algorithms and is made possible by automatically pushing local variables onto the call stack with each function call and popping those same variables upon return, along with the result of the call. We also refer to this as "winding" and "unwinding" the stack, analogous to winding a clock spring with each recursion and unwinding with each return.With each call to the function, we increase the depth of recursion. With each return, we decrease the depth of recursion. If we do not provide an end point in our function, the depth of recursion will be such that all stack space will eventually be consumed and our program will crash. However, a recursive function may increase and decrease its depth of recursion several times over during a single calculation and this is the essence of divide-and-conquer algorithms such as the quicksort algorithm. That is, each call of the function divides an unsorted set into two subsets around a single pivot (or several contiguous pivots of the same value). The pivot is in the correct place with respect to the two subsets, but those subsets are still unsorted. Thus quicksort is recursively applied to each of these subsets in turn. Eventually a subset will have fewer than 2 elements which represents the endpoint for that particular recursion. When all subsets have fewer than 2 elements, the entire set is sorted.Given that the two subsets in a quicksort may not be of equal size, the larger subset will inevitably incur more recursions than the smaller subset. Thus it pays to recurse over the smaller subset and make a tail call for the larger one. A tail call is where the final statement in a function is a recursive call to the same function. Given that state does not need to be maintained for a tail call (because it is the last statement of the function), the same instance of the function can be invoked (with modified parameters) rather than going to the expense of making an otherwise unnecessary function call. Modern compilers generally include tail-call optimisation to take advantage of this.Although iteration can be more efficient than recursion, even with naturally recursive functions, modern compilers are capable of inline expanding recursive functions to a certain degree (or rather depth), particularly if the depth can be calculated at compile time and the increased code size does not adversely affect performance. If the depth cannot be calculated in advance, inline expansion can still be achieved up to the predefined depth with a standard recursive call for any additional recursions that may be required at runtime. That being the case there is generally no reason to try and create an iterative solution to what would otherwise be a naturally recursive algorithm.


What happens if you create a loop in C program that never ends?

then your program will never ends, too unless you pressing the ctrl+c or kill it through your os. can i know the purpose of you creating the loop that never ends? is it just a mistake or are you doing it on purpose?


A program to find the sum of n numbers in visual basic?

//Script by aakash30jan <script type="text/vbscript"> For count = 1 to 10 sum=sum+count document.write("<br />" & sum) Next </script>

Related Questions

What is a recursive function?

A recursive function is one that calls upon itself until a given result in the original call is met. Take a look at this example. Program Recursion; Uses crt; Var number:longint; Function Factorial(number:longint):longint; Begin if number > 0 then factorial:=number*factorial(number-1) else factorial:=1; End; Begin clrscr; readln(number); writeln(factorial(number)); readln; End. Note how the function factorial calls itself.


Write a Recursive Function in C plus plus?

void infinte (long l) { printf ("I will never stop #%ld\n", l); infinte (l+1); }


What is a recursive formula and what is it used for Geometric and Arithmetic?

A recursive definition is any definition that uses the thing to be defined as part of the definition. A recursive formula, or function, is a related formula or function. A recursive function uses the function itself in the definition. For example: The factorial function, written n!, is defined as the product of all the numbers, from 1 to the number (in this case "n"). For example, the factorial of 4, written 4!, is equal to 1 x 2 x 3 x 4. This can also be defined as follows: 0! = 1 For any "n" > 0, n! = n x (n-1)! For example, according to this definition, the factorial of 4 is the same as 4 times the factorial of 3. Try it out - apply the recursive formula, until you get to the base case. Note that a base case is necessary; otherwise, the recursion would never end.


Did Moses ever see God's face during his encounters with Him?

No, according to the Bible, Moses never saw God's face during his encounters with Him.


What is simulation recursion in C?

The factorial f(n) = n * (n-1) * (n-2) * .. 1. For example factorial 5 (written as 5!) = 5 x 4 x 3 x 2 x 1 = 120. The function below returns the factorial of the parameter n. int factorial( int n) { if (n==1) return 1 else return n* factorial( n-1) ; }


Is a circle graph a function?

No, a circle graph is never a function.


Is The inverse of a function is a function always sometimes or never?

sometimes


What happened to Roy as he talked about his experiences in Close Encounters Of The Third Kind?

I don't know. Never seen the movie.


Why do people fake UFO encounters?

Most of the time people fake UFO encounters because they are attention seekers. That is not saying there are not real UFO's out there, though I have never experienced seeing one. because they like to fool everybody


Is this pattern recursive 1 11 111 1111 11111?

Oh honey, that pattern is as recursive as a broken record playing the same old tune. Each number just adds another 1 to the end, like a never-ending saga of ones. So yeah, it's recursive alright, just like that annoying jingle that gets stuck in your head for days.


What does the word unrecognizable mean?

not able to identify, not identified from previous encounters or knowledge. never seeing something till just then, not remembering....


Does a function always require a parameter?

No. For example, function getpid never requires a parameter.