English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have to implement a root finding algorithm using a recursive and a non-recursive function in c++, what is the difference?

2006-12-05 00:31:21 · 4 answers · asked by Markus W 2 in Computers & Internet Programming & Design

4 answers

Rawlyn is 100% right and should get the points.

Here's some background reading
http://en.wikipedia.org/wiki/Recursion#Recursion_in_computer_science
///

2006-12-05 00:35:12 · answer #1 · answered by jan 7 · 4 0

a recursive function is a function which calls itself . for example...
void main()
{
main();
}

the above code may result in an infinite loop...so many a times recursive functions are not advised to be used...but u have some very famous applications of recursive functions like calculation of factorial, tower of hanoi etc.

The other functions i.e. the functions which dont call themselves are known as non recursive functions...these may or may not call other functions..

2006-12-05 08:37:15 · answer #2 · answered by jigvesh b 2 · 0 0

A recursive function calls itself. A non-recursive function does not.

2006-12-05 08:32:04 · answer #3 · answered by Anonymous · 2 0

1. A recursive function directly or indirectly makes a call to itself.
2. A recursive function makes use of the stack to store intermediate values.
3. A recursive function should have a terminating condition to end recursion.

A root is defined as a node that does not have a parent. Hence to find the root of a tree, we need to check if the parent pointer is NULL.

Assuming a node is defined like this:
struct node {
int data;
node *child;
node *parent;
};

void print_root(node *a_node) {
if (a_node == NULL) {
printf("NULL node provided\n");
}
else if(a_node->parent == NULL) {
printf("This is the root node %d\n", a_node->data);
}
else {
print_root(a_node->parent); // recursive call
}
}

Non recursive solution:
void print_root(node *a_node) {
if( a_node == NULL ) {
printf("NULL node provided\n");
}
else {
while( a-> parent != NULL) {
a = a->parent;
}
printf("This is root %d\n", a->data);
}

HTH

2006-12-05 08:44:28 · answer #4 · answered by swami060 3 · 0 1

fedest.com, questions and answers