Discussion 9 CS1101X Group 5. Lab7 Minor note that default value of int[][] are all 0 default value...

24
Discussion 9 Discussion 9 CS1101X Group 5

Transcript of Discussion 9 CS1101X Group 5. Lab7 Minor note that default value of int[][] are all 0 default value...

Discussion 9Discussion 9CS1101X Group 5

Lab7Minor note that

default value of int[][] are all 0default value of boolean[][] are all false

Discussion PlanGeneral notes on RecursionRecursion practicePE Problem solving

RecursionBase caseRecursive case

n! = n * (n-1) * (n-2) * … * 2 * 1 for n > 0 (recursive case)

0! = 1 (base case)

int fac(int n){

if (n == 0) return 1;

else{ return n * fac(n-1); }

}

RecursionGeneral approachesThink recursive

◦Split the problem into simple cases◦Work in the opposite way

Work from a base case See what you need to do for the 2nd last

case etc

Recursion PracticeCan you write a “while” method

that does what the while loop is doing

Recursion PracticeCan you write a “while” method

that does what the while loop is doing

public static void While (int i){ if (cond){ //code of while body While(i); }}

Recursion Practicewhile(i > 0){

System.out.println(i);

i--;

}

public static void While (int i){

if (i > 0 ){

System.out.println(i);

i--;

While(i);

}

}

Recursion PracticeDownload Practical Test

http://www.comp.nus.edu.sg/~cs1101x/3_ca/labs.html

Can you write a recursive solution for both questions?

PE Problem Solving2006/07Houses in a townBinary image and pattern

detection

Houses in a townNeed a current_maxDistance and

current_maxPointMethod to find the distance from (0,0) to

(x,y)Using the final current_maxDistance as

radius, find area

Binary image and pattern detectionBasically the problem seems very

difficult but the general skeleton has been given to you

Stuff you might want to try out:rotate90CW()

PE Problem Solving2004/05PluralsCandlesTeams

PluralsCheck out String api

public boolean endsWith(String suffix)

Just a series of if-else and endsWith and substring

TeamsMethod 1:

◦ArrayList and count and remove Method 2:

◦Keep a String[] and each time you allocate a player, set the index to null and go to the next index (index + 1) % n

◦Do until allocate n players◦Only increment the “count” when

you are at non-null String[location]

Other past year questions…

Only got the final sourcecode, lost the questions

Snake

Snake“Board” class

◦void move(char direction)◦boolean isBlank

Main◦create a “menu”

Snake“Board” class

◦void move(char direction)◦boolean isBlank

Main◦create a “menu”

Crossword Puzzle

Boggle

Kings

•Can’t really remember what it does

•But basically chess game

Do you know…toStringgetClassArray – 1D, 2DArrayList – contains, indexOf, get, set, add,

remove, removeFirst, removeLast,…

String – indexOf, lastIndexOf, startsWith, endsWith, substring, charAt, split

Math – min, max, floor, ceilClasses – declare and use

Do you know…Recursion – good to know in case

it can be easily solve withPoint classScanner – how to get tokensnew Scanner(“some_string”)Regular Expression