answersLogoWhite

0


Best Answer

void foo (unsigned int x)

{

static count {0};

++count;

if (x != 0)

return foo (--x);

return count;

}

int main()

{

int cnt = foo (10);

assert (cnt==10);

cnt = foo (5);

assert (cnt==15);

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a recursive function that can count its own occurrence?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is a recursive call. Which data structure is used in it?

Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows whom to return when the function has to return. Recursion makes use of system stack for storing the return addresses of the function calls. Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written, explicit stack is to be used.


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's a JAVA program for Euler's totient function?

Implement this method: public static int totient(int n) { int count = 0; for(int i = 1; i < n; i++) if(gcd(n,i) == 1) count++; return count; } You can implement your own gcd(int a, int b) method, or find one of the many online.


What is a recursive rule?

It is a term for sequences in which a finite number of terms are defined explicitly and then all subsequent terms are defined by the preceding terms. The best known example is probably the Fibonacci sequence in which the first two terms are defined explicitly and after that the definition is recursive: x1 = 1 x2 = 1 xn = xn-1 + xn-2 for n = 3, 4, ...


What are the advantages of the recursion in c plus plus?

Any problem that can be solved by dividing the problem into smaller problems of the same type is a good candidate for recursion. However, there are actually very few applications that cannot be resolved more efficiently with an iterative loop. It therefore pays to know when recursion is unavoidable and when it is optional. The main benefit to recursion is that each instance of the function maintains its own set of non-static local variables completely independently of all other instances. But if these variables are of no use to the function when a recursive call returns, then an iterative implementation would be more efficient. Function calls are expensive in terms of memory consumption and performance, so the fewer we make, the better. A classic example of recursive application is the quicksort algorithm. In simple terms, quicksort is a function that accepts a subset of data. The data is usually stored in an array and the function accepts the left and right index of the subset to be sorted. Initially this will be lower and upper bounds of the entire array, but if the indices indicate a subset with fewer than 2 items, the function immediately exits. This effectively defines the return path from the recursions. Assuming there are 2 or more items, the function selects one of the items (the pivot) and then sorts the array such that items less than the pivot are placed to its left, and items greater or equal to its right. This moves the pivot into its final position, but the items on either side may still be unsorted. Thus the function calls itself twice, once for each of these subsets, which gradually reduces the problem down into smaller and smaller subsets until a subset has fewer than 2 items, at which point the recursion unwinds to the previous instance. Recursion is required because when the first recursive call returns, the subset to the left of the pivot is guaranteed to be sorted, but the subset to the right is not. This means we must maintain local variables in order to determine the lower and upper bounds of that subset. Although quicksort is an elegant application of recursion, there is still room for improvement. Firstly, it is better to make a recursive call upon the smaller of the two subsets. The smaller the subset, the fewer recursions that will be incurred sorting it. Secondly, since the second recursion is also the last statement in the function, there is no need to maintain the local variables when it returns. Thus the second recursion can be implemented as a tail call. This effectively means we modify the existing local variables to suit and then recall the same instance (with a goto). This reduces the depth of recursion by one function call per recursion which will quickly add up to a significant boost in efficiency.

Related questions

What is recursive rule?

A recursive rule is one which can be applied over and over again to its own output


What is the recursive foreign key?

References its own relation


What is a recursive call. Which data structure is used in it?

Stack. Because of its LIFO (Last In First Out) property it remembers its 'caller' so knows whom to return when the function has to return. Recursion makes use of system stack for storing the return addresses of the function calls. Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written, explicit stack is to be used.


What do countesses do?

Countess is a title of nobility. A countess can be the wife of a Count or can be a noblewoman in her own right. Countesses, like other nobles have no specific function they are expected to perform in modern society. In medieval times the Count and Countess were the nobles who managed a County.


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 an act of God?

An act of God is an unforeseen occurrence beyond someone's own control.


What's a JAVA program for Euler's totient function?

Implement this method: public static int totient(int n) { int count = 0; for(int i = 1; i < n; i++) if(gcd(n,i) == 1) count++; return count; } You can implement your own gcd(int a, int b) method, or find one of the many online.


Can you count your own teeth?

yes use a marker and color each tooth you count


What does function mean in your own words?

Function is how something works; if something is working. In my own words.


What is a recursive rule?

It is a term for sequences in which a finite number of terms are defined explicitly and then all subsequent terms are defined by the preceding terms. The best known example is probably the Fibonacci sequence in which the first two terms are defined explicitly and after that the definition is recursive: x1 = 1 x2 = 1 xn = xn-1 + xn-2 for n = 3, 4, ...


What is a count and a countess?

a count and countess own a castle and are like the king and queen but do not have as much power


How do you calculate nutrients on your own?

You count em' -_- gosh...