Ask your own question, for FREE!
Computer Science 15 Online
OpenStudy (anonymous):

difference between recursion and iteration in c

OpenStudy (rsmith6559):

The difference between recursion and iteration has nothing to do with any language. Recursion is defined as as solving part of a problem such that the solution to the problem is the "sum" of the little solutions. Iteration is usually a more formal "walk" through a set of datum. Iteration is usually more of a for loop setup: int i; for( i =0; i < datumLength; i++ ) { /* do something with each datum */ } A while loop is more like recursion, but is still iteration: while( I < datumLength ) { /* do something with each datum */ } In recursion, the condition of the while statement would be what's called the "base case". Once the base case is reached, the recursion starts returning, called unwinding. You take some of the data, check for the base case, process it or recurse again, recurse or process it, and return it and the results of other recursions. A simple program will be more helpful: #include <stdio.h> #include <stdlib.h> int mkDigit( int number, int base, char* answer ) { char characters[37] = { "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" }; int remainder, index; /* base case */ if( number == 0 ) return( 0 ); remainder = number % base; number /= base; index = mkDigit( number, base, answer ); answer[ index ] = characters[ remainder ]; return( ++index ); } int main( int argc, char *argv[] ) { char answer[19]; int number = atoi( argv[1] ); int base = atoi( argv[2] ); int index; index = mkDigit( number, base, answer ); answer[ index ] = '\0'; puts( answer ); return( 0 ); } This program converts a base 10 number ( argv[1] ) to any base from 2 to 36 ( argv[2] ).

OpenStudy (anonymous):

@rsmith6559 you spanked that question! nice.

OpenStudy (anonymous):

What's important to remember is that recursion is more adapted to certain kinds of problems (they suddenly become much easier to write!). It often depends on the organisation of your data. For example, an array is a consecutive set of values that are usually aligned one next to another in memory. in this case, it's useful to use an iteration that will go through each of the array elements in order. If your data are organised using a tree structure or any hierarchical organization that has some parents and some descendants (that's the case for the description of a skeleton for example: the pelvis having two legs, each leg having a foot, each foot having 5 toes, etc.) then a recursive program that will end on a simple condition (like: "no more descendant") is much more appropriate to visit all your data.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!