User Tools

Site Tools


Sidebar

projects

wcp1 (due 20220824)
ael0 (due 20220831)
ntr0 (due 20220831)
pct1 (bonus; due 20220831)
wcp2 (due 20220831)
pct2 (due 20220907)
pct3 (bonus; due 20220907)
sln0 (due 20220907)
wcp3 (due 20220907)
sln1 (due 20220914)
wcp4 (due 20220914)
pct4 (due 20220915)
sll0 (due 20220921)
wcp5 (due 20220921)
pct5 (bonus; due 20220922)
gfo0 (due 20220928)
sll1 (due 20220928)
wcp6 (due 20220928)
pct6 (due 20220929)
pct7 (bonus; due 20221005)
sll2 (due 20221005)
wcp7 (due 20221005)
bwp1 (bonus; due 20221019)
pct8 (due 20221019)
sll3 (due 20221019)
sll4 (due 20221019)
wcp8 (due 20221019)
pct9 (bonus; due 20221026)
wcp9 (due 20221026)
dll0 (due 20221027)
dln0 (due 20221027)
gfo1 (due 20221102)
pctA (due 20221102)
wcpA (due 20221102)
dll1 (due 20221103)
pctB (bonus; due 20221109)
wcpB (due 20221109)
dll2 (due 20221110)
dls0 (due 20221110)
pctC (due 20221116)
wcpC (due 20221116)
dlq0 (due 20221117)
bwp2 (bonus; due 20221201)
pctD (bonus; due 20221201)
wcpD (bonus; due 20221201)
pctE (bonus; due 20221207)
wcpE (bonus; due 20221207)
gfo2 (due 20221208)
EoCE (due 20221219)
haas:fall2022:data:projects:sll2:paradigms

Solution Paradigms

Recursion

Recursion is the effective calling (and nesting) of the same function over and over again to derive the solution (i.e. the work is accomplished through each successive function call).

Recursion is often considered more elegant, yet less efficient (the underlying work the computer must perform tends to be greater).

Here is an example of recursion to compute the sum of the first 10 integers (0-9):

1
#include <stdio.h>
 
int increment(int, int);
 
int main()
{
        int i = 0;
        int sum = 0;
 
        sum = increment(i, 10);
 
        fprintf(stdout, "sum is: %d\n", sum);
 
        return(0);
}
 
int increment(int value, int max)
{
        if (value < (max - 1))
                value = value + increment(value+1, max);
 
        return(value);
}

This program, when executed, will output:

$ ./recursion
sum is: 45
$ 

Iteration

On the other hand, iteration is more in line with how we're used to thinking (step by step, with each detail more tangible). The computer tends to work more in an iterative fashion, although solutions done iteratively may not appear as elegant as under other forms (recursion).

Here is an example of iteration to compute the sum of the first 10 integers (0-9):

1
#include <stdio.h>
 
int main()
{
        int i = 0;
        int sum = 0;
 
        for(i = 0; i < 10; i++)
                sum = sum + i;
 
        fprintf(stdout, "sum is: %d\n", sum);
 
        return(0);
}

This program, when executed, will output:

$ ./iteration
sum is: 45
$ 

Perspective

Recursion, like the use of pointers, structs, inheritance, polymorphism, etc. really lends itself well to larger, more complicated problems.

In the current setting, the iterative example likely looks far simpler (less code overall). This in no way implies recursion is inferior, as outside of potential performance issues, it is an effective way to solve problems, and being able to think recursively can improve one's problem solving skills overall.

haas/fall2022/data/projects/sll2/paradigms.txt · Last modified: 2015/10/15 21:11 by 127.0.0.1