DOC PREVIEW
UIUC CS 101 - lect25

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

25-2 • Recursive Functions • Related Chapter: ABC 5.14, 5.1525-3 Recursive functions are defined in terms of themselves; i.e., a function is recursive if it contains calls back to itself or to another function that calls the original function. Recursive programming usually requires more memory and runs more slowly than non-recursive programs. This is due to the cost of implementing the “stack”. However, recursion is often a natural way to define a problem.25-4 1. Problem Definition Write a factorial function. 0! = 1 and n! = n*(n-1)!. Use “recursion” to implement the factorial function. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = Non-negative integers as input. Output= return the factorial of the input value. Note: that 69! = 1.711224524... x 1098 so our function will only work for small integer values. It would be better to return a value of data-type double (Why?)25-5 double fact(int n) { if (n = =0) /* Voom! */ return 1.0; else return (n * fact(n - 1)); /* recursive call */ } In main we can call the factorial function by the following command: printf("%lf", fact(3)); or by (if x is of data-type int and y is of data-type double) : x = 3; y = fact(x);(push) return ( 3 * fact(2)); (n = 3) (push) return ( 2 * fact(1)); (n = 2) (push) return ( 1 * fact(0)); (n = 1) (push/pop) return 1.0; (n = 0) (pop) return ( 1 * 1.0); (n = 1) (pop) return ( 2 * 1.0); (n = 2) (pop) return ( 3 * 2.0); (n = 3) return 1.0; (n = 0) return ( 1 * fact(0)); (n = 1) return ( 2 * fact(1)); (n = 2) return ( 3 * fact(2)); (n = 3) (STACK) return ( 1 * 1.0); (n = 1) return ( 2 * fact(1)); (n = 2) return ( 3 * fact(2)); (n = 3) return ( 2 * 1.0); (n = 2) return ( 3 * fact(2)); (n = 3) return ( 3 * 2.0); (n = 3) printf("%lf", fact(3) );25-7 1. Problem Definition Write a solve_maze function. Read the maze from a file, “maze.txt” and display one path that traverses the maze. Use “recursion” to implement the maze function. 2. Refine, Generalize, Decompose the problem definition (i.e., identify sub-problems, I/O, etc.) Input = The file “maze.txt” contains the following ********** * * **** * *O* * * *** * * *X* * ** *** * * * * * *** ** * * * * ********** Your program will have to find its way through a 10x10 maze where the symbols “ * ” , “O” and “X” denote: “ * ” are solid walls through which you cannot travel "O" denotes the starting position, "X" is the exit for which you are looking for* * * * * * * * * * * * * * * * * * O * * * * * * * * * X * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *25-9 2. Refine, Generalize, Decompose the problem definition Input (continued): Read the maze into the 2D array, ********** * * **** * *O* * * *** * * *X* * ** *** * * * * * *** ** * * * * ********** char maze[NUMROWS][NUMCOLS]; where NUMROWS and NUMCOLS are constants with value 10. The upper left-hand corner of the maze has the value maze[0][0] . If we want to test whether the cell in the fourth row and fourth column contains a wall then it's enough to use an if statement like this: if (maze[3][3] == ‘ * ')25-10 2. Refine, Generalize, Decompose the problem definition Output : Display a solution as follows: ********** * OOOOO* ****O* *O* * *O* *** * *O* *X* * **O***O* * OO *O* * ***O**O* * * OOOO* **********25-11 3. Develop Algorithm (processing steps to solve problem) Step 1 Read in the “maze” and find the starting Row and Column (the position of the “O”). Use variables “curRow” and “curCol” to keep track of the current position as we traverse through the maze (the 2D matrix “maze”). Step 2 Display the maze. Step 3 Check to see if current position is an “X” then we are done. Otherwise first try to go up and if not then down and if not then left and if not then right and if you can go up/down/left/right then go back to Step 2. Otherwise, go back to the previous position [curRow,curCol] and try another direction.#include <stdio.h> #include <stdlib.h> #define NUMROWS 10 #define NUMCOLS 10 /* prototypes */ int read_maze (char[][], int *, int *); void display_maze (char[][]); void solve_maze (char[][], int, int);void main (void) { int startRow, startCol; /* Starting point in maze. */ char maze[NUMROWS][NUMCOLS]; /* Stores maze read from input file. */ if (read_maze(maze, &startRow, &startCol) == 0) { printf ("Error reading maze from file maze.txt!\n"); return; } solve_maze(maze, startRow, startCol); } /*end of main */void solve_maze(char maze[][NUMCOLS], int curRow, int curCol) { int i; display_maze(maze); /* Check if solution found. */ if ((maze[curRow - 1][curCol] == 'X') || (maze[curRow + 1][curCol] == 'X') || (maze[curRow][curCol + 1] == 'X') || (maze[curRow][curCol - 1] == 'X')) exit (0); /* Recurse in each possible direction that is empty. */ /* Move up */ if (maze[curRow - 1][curCol] == ' ') { maze[curRow - 1][curCol] = 'O'; solve_maze(maze, curRow - 1, curCol); maze[curRow - 1][curCol] = ' '; } /* continued on next slide *//* Move down */ if (maze[curRow + 1][curCol] == ' ') { maze[curRow + 1][curCol] = 'O'; solve_maze (maze, curRow + 1, curCol); maze[curRow + 1][curCol] = ' '; } /* Move left */ if (maze[curRow][curCol - 1] == ' ') { maze[curRow][curCol - 1] = 'O'; solve_maze (maze, curRow, curCol - 1); maze[curRow][curCol - 1] = ' '; } /* Move right */ if (maze[curRow][curCol + 1] == ' ') { maze[curRow][curCol + 1] = 'O'; solve_maze (maze, curRow, curCol + 1); maze[curRow][curCol + 1] = ' '; } usleep (600000); return; }/* Display the maze passed as a parameter to standard output. */ void display_maze (char maze[ ][NUMCOLS]) { int i, row, col; for (row = 0; row < NUMROWS; row++) { for (col = 0; col < NUMCOLS; col++) { printf ("%c", maze[row][col]); } printf ("\n"); } usleep (600000); printf ("\n"); }int read_maze (char maze[ ][NUMCOLS], int *sRow, int *sCol) { FILE *fpMaze; int row, col; char endofline; /* end of line character */ /* Open maze text file, make sure it opens OK. */ if ((fpMaze = fopen ("maze.txt", "r")) == NULL) return 0; for (row = 0; row <


View Full Document

UIUC CS 101 - lect25

Documents in this Course
Notes

Notes

114 pages

lect2223

lect2223

35 pages

lect2223

lect2223

35 pages

lect1920

lect1920

23 pages

lect1920

lect1920

23 pages

lect1617

lect1617

25 pages

lect1617

lect1617

25 pages

lect1314

lect1314

34 pages

lect1314

lect1314

34 pages

lect0607

lect0607

25 pages

lect0607

lect0607

25 pages

lect24

lect24

15 pages

lect21

lect21

25 pages

lect21

lect21

25 pages

lect18

lect18

22 pages

lect18

lect18

22 pages

lect15

lect15

37 pages

lect15

lect15

37 pages

lect12

lect12

31 pages

lect12

lect12

31 pages

lect11

lect11

28 pages

lect11

lect11

28 pages

lect10

lect10

28 pages

lect09

lect09

24 pages

lect09

lect09

6 pages

lect08

lect08

23 pages

lect08

lect08

23 pages

lect05

lect05

26 pages

lect05

lect05

26 pages

lect04

lect04

36 pages

lect04

lect04

36 pages

lect03

lect03

26 pages

lect03

lect03

26 pages

lect02

lect02

36 pages

lect02

lect02

36 pages

lect01

lect01

32 pages

lect01

lect01

32 pages

lect00

lect00

23 pages

lect00

lect00

23 pages

Load more
Download lect25
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view lect25 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view lect25 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?