Recursion and 2-D Arrays
CMSC 104 Spring 2012, Section 02, Lecture 19 Jason Tang
Topics
• Recursion• Recursive Functions• Two-Dimensional Arrays
Recursion
Problem Solving (Review)
• Many problems can be solved by doing the same steps over and over again
• Example: Program that displays all capital letters in the alphabet
Solution with While Loop (Review)
#include <stdio.h> int main(void) { int NUM_LETTERS = 26; int letter = 0; while (letter < NUM_LETTERS) { char c = 'A' + letter; printf("%c", c); letter = letter + 1; } printf("\n"); return 0; }
Initial Condition
Terminating Condition
Update
Recursive Solution
• Same program may be represented via a function that calls itself (so-called recursive function)
• Recursive algorithms also have same “initial condition” / “terminating condition” / “update” structure
Recursive Solution#include <stdio.h> void show_letters(int letter); int main(void) { show_letters(0); return 0; } void show_letters(int letter) { int NUM_LETTERS = 26; if (letter < NUM_LETTERS) { char c = 'A' + letter; printf("%c", c); show_letters(letter + 1); } }
Recursive Solution#include <stdio.h> void show_letters(int letter); int main(void) { show_letters(0); return 0; } void show_letters(int letter) { int NUM_LETTERS = 26; if (letter < NUM_LETTERS) { char c = 'A' + letter; printf("%c", c); show_letters(letter + 1); } }
Initial Condition
Terminating Condition
Update
Recursion Theory
• All repetitions can be rewritten to be recursively, and vice versa
• They are mathematically equivalent
• Same programming mistakes that can occur with repetitions may also occur with recursions
• Some problems are better solved with repetitions, others with recursion
Common Mistake: Missing Update#include <stdio.h> void show_letters(int letter); int main(void) { show_letters(0); return 0; } void show_letters(int letter) { int NUM_LETTERS = 26; if (letter < NUM_LETTERS) { char c = 'A' + letter; printf("%c", c); show_letters(letter); } }
Output from Recursion
• Common use for recursive functions is to calculate and return some value
• Example: Calculate the value of some base raised to exp power This should sound vaguely familiar
Pseudocode Solutionsint pow(<base>, <exp>) <answer> = 1 While <exp> > 0 <answer> = <answer> * <base> <exp> = <exp> - 1 Return <answer>
int pow(<base>, <exp>) If <exp> == 0 Return 1 Return <base> * pow(<base>, <exp> - 1)
Solution using repetition
Solution using recursion
Implementing these in C are left as exercises for the reader
Multidimensional Arrays
• As stated earlier, arrays may hold scalars as well as other arrays
• An array that holds another array is a multidimensional array
• An array that holds an array is a 2-D array (also known as a “matrix”)
• An array that holds an array that holds an array is a 3-D array
2-D Array Declaration
• This declares a 2-D array that has 21 rows and 15 columns
• This is an array of array of chars
• Just like 1-D arrays, 2-D arrays cannot be resized after declaration
• In C, 2-D arrays have “row-major” ordering
char crossword[21][15];
Row-Major Ordering
0 1 2 3
0 [0][0] [0][1] [0][2] [0][3]
1 [1][0] [1][1] [1][2] [1][3]
2 [2][0] [2][1] [2][2] [2][3]
3 [3][0] [3][1] [3][2] [3][3]
Row
Column
Note how the row number is given first
Memory Buffers
• 2-D arrays often used to model an image
• Each pixel in image corresponds to an element in “buffer”
• Changes are made to buffer
• Entire buffer then drawn to screen
Usually, 0 = off and 1 = on
Using 2-D Arrays, Part 1#include <stdio.h> int main(void) { int NUM_ROWS = 8, NUM_COLS = 8, row, col; char board[NUM_ROWS][NUM_COLS]; /* begin setting up board */ for (row = 0; row < NUM_ROWS; row = row + 1) { for (col = 0; col < NUM_COLS; col = col + 1) { if ((row + col) % 2 == 0) { board[row][col] = '-'; } else { board[row][col] = '+'; } } } /* end setting up board */
Save this as board.c
Using 2-D Arrays, Part 2 /* display board */ for (row = 0; row < NUM_ROWS; row = row + 1) { for (col = 0; col < NUM_COLS; col = col + 1) { printf("%c", board[row][col]); } printf("\n"); } return 0; }
Warning: 2-D arrays are not passed into functions the same as with other variables. Usually 2-D arrays are declared as globals.
In-Class Assignment
• Modify board.c to display an entire chessboard
• You may only modify the lines between begin setting up board and end setting up board
• Submit work as chess board.c
RNBQKBNR PPPPPPPP +-+-+-+- -+-+-+-+ +-+-+-+- -+-+-+-+ PPPPPPPP RNBQKBNR