Modified Tutorial 8 - Multimedia University

Download Report

Transcript Modified Tutorial 8 - Multimedia University

Recursion
What is recursion?



Function call within its body
Acts like a loop
Therefore needs a guard condition to
stop it calling itself eventually.
Example:Factorial
5!=4!x5
4!=3!x4
3!=2!x3
2!=1!x2
1!=0!x1
0!=1
long factorial(int n){
if (n>0)
return (factorial(n-1)*n);
else
return(1);}
n>0 is the guard condition so it stops when the
function factorial receives the number 0.
From main,receive the
number 4
How Recursion Works
Factorial(4)
4>0? Yes, call
factorial
and give
4-1=3
Return
6*4=24 to
main
3
Factorial(3)
3>0? Yes, call
factorial
and give
3-1=2
Return
2*3=6
Factorial(2)
2>0? Yes, call
factorial
and give
2-1=1
Return 1*2=2
1
Factorial(1)
1>0? Yes, call
factorial
and give
1-1=0
Return 1*1
0 Factorial(0)
0>0? No,
so return 1
Tutorial 8





function
It takes in the x and y coordinates, checks to
see if that spot is X or space.
If it is an X, it stops and returns control.
If it is a space, it overwrites with an X.
The algorithm should check top, bottom, left
and right of the original x and y coordinates
input.
Write fill()
Today’s Program
#include <stdio.h>
/* This program lets you specify a 65x32 text file representing a
* picture and 2 numbers representing coordinates and then does a
* recursive area fill. (Note that 65 includes the carriage return
* so actually it looks like just 64 characters wide.) */
#define XMAX 65
#define YMAX 32
char screen[XMAX][YMAX];
void printScreen(void);
void fill(int, int);
Today’s Program
int main(int argc, char *argv[])
{ FILE *in;
int x, y;
if (argc!= 4)
{ fprintf(stderr,"Usage:\n\t%s <65x32 text file> <x>
<y>\n",argv[0]);
exit(1);
}
in = fopen(argv[1],"r");
if (in == NULL)
{ perror("opening input file");
exit(1);
}
Today’s Program
/* Read the 65x32 text file */
for (y=0; y<YMAX; y++)
{ for (x=0; x<XMAX; x++)
screen[x][y] = fgetc(in);
}
if (fclose(in))
{ perror("closing input file");
exit(1);
}
Today’s Program
printScreen();
x = atoi(argv[2]);
y = atoi(argv[3]);
fill(x,y);
printScreen();
}
Today’s Program
void printScreen(void)
{ int x,y;
for (y=0; y<YMAX; y++)
for (x=0; x<XMAX; x++)
putchar(screen[x][y]);
}
fill(x,y)
void fill(int x,int y)
{
if (screen[x][y]!=‘ ‘)
return;
screen[x][y]=‘X’;
fill(x,y+1);
fill(x,y-1);
fill(x+1,y);
fill(x-1,y);
}
Demo of Algo on 6x6 image
0
1
2
3
4
5
6
1
X
X
X
X
X
X
2
X
X
3
X
X
4
X
X
5
X
X
6
X
X
X
X
X
X
fill(x,y)
{
if (screen[x][y]!=‘ ‘)
return;
screen[x][y]=‘X’;
fill(x,y+1);
fill(x,y-1);
fill(x+1,y);
fill(x-1,y);
}
Modified Tutorial 8


now takes a character to fill with
(instead of only filling with X)
E.g.
fill()
t8-alt t8data 7 4 c

results in filling with
the letter c
Start with the t8-alt.c
you can copy from the
tutor’s floppy disk
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
X
X
X
X
X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXX
X
X
XccccccccccccccccccccccccccccX
XcccccX
X
X
XcccccccccccccccccccccccccccccX
XcccccX
X
X
XXXXXXXXXXXXccccXXXXXXXXXXcccccX
XcccccX
X
X
XccccX
XcccccX
XcccccX
X
X
XccccX
XcccccX
XcccccX
X
X
XccccX
XcccccX
XcccccX
X
X
XccccX
XcccccX
XcccccX
X
X
XccccX
XcccccX XcccccX
X
X
XccccX
XcccccXcccccX
X
X
XccccX
XcccccccccX
X
X
XccccX
XcccccccX
X
X
XccccX
XcccccX
X
X
XccccX
XcccccccX
X
X
XccccX
XcccccccccX
X
X
XccccX
XcccccXcccccX
X
X
XccccX
XcccccX XcccccX
X
X
XccccX
XcccccX
XcccccX
X
X
XccccX
XcccccX
XcccccX
X
X
XccccX
XcccccX
XcccccX
X
X
XccccX
XcccccX
XcccccX
X
X
XXXXXXXXXXXXccccXXXXXXXXXXcccccX
XcccccX
X
X
XcccccccccccccccccccccccccccccX
XcccccX
X
X
XccccccccccccccccccccccccccccX
XcccccX
X
X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXX
X
X
X
X
X
X
X
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX