Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D...

19
Recursion and 2-D Arrays CMSC 104 Spring 2012, Section 02, Lecture 19 Jason Tang

Transcript of Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D...

Page 1: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

Recursion and 2-D Arrays

CMSC 104 Spring 2012, Section 02, Lecture 19 Jason Tang

Page 2: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

Topics

• Recursion• Recursive Functions• Two-Dimensional Arrays

Page 3: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

Recursion

Page 4: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 5: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 6: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 7: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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); } }

Page 8: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 9: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 10: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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); } }

Page 11: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 12: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 13: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 14: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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];

Page 15: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 16: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 17: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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

Page 18: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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.

Page 19: Recursion and 2-D Arrays - Inspiring Innovationjtang/archives/cs104.s14/lectures/L19... · 2-D Array Declaration • This declares a 2-D array that has 21 rows and 15 columns •

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