A Peek at Programming or, problem solving in Computer Science Aaron Tan

Slides:



Advertisements
Similar presentations
CS1010: Programming Methodology
Advertisements

© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 7: Recursion Java Software Structures: Designing and Using.
CS1010 Programming Methodology
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Agenda Questions? Problem set 5 Parts A Corrected  Good results Problem set 5.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Programming Practicum Day 1: Introduction and Problem Solving Aaron Tan NUS School of Computing.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
CS1101: Programming Methodology
CS1101: Programming Methodology Aaron Tan.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Invitation to Computer Science, Java Version, Second Edition.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
Recursion Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Recursion Jordi Cortadella Department of Computer Science.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
JAVA Advanced Control Structures. Objectives Be able to use a switch statement for selective execution. Be able to implement repetition using alternative.
# 1# 1 VBA Recursion What is the “base case”? What is the programming stack? CS 105 Spring 2010.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
By the end of this session you should be able to...
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 3.
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
11-1 Recursive Thinking A recursive definition is one which uses the word or concept being defined in the definition itself When defining an English word,
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
 Prentice Hall. All rights reserved. 1 CS 1120 – Computer Science II Recursion (Ch.15) Many slides modified by Prof. L. Lilien (even many without.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
 Prentice Hall. All rights reserved. 1 Recursion (Section 7.13 & Exercise7.40 from ed.3) (Sections 6.15, 6.16 from ed.1) Many slides modified.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Programming Appreciation Camp
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
UNIT 17 Recursion: Towers of Hanoi.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Recursion by Ender Ozcan. Recursion in computing Recursion in computer programming defines a function in terms of itself. Recursion in computer programming.
Recursion.
COMP 51 Week Fourteen Recursion.
CS1010 Programming Methodology
INTRODUCTION TO PROBLEM SOLVING
Abdulmotaleb El Saddik University of Ottawa
GC211Data Structure Lecture2 Sara Alhajjam.
Chapter 5: Control Structures II
Introduction To Flowcharting
Java Lecture Recursion 29th feb 2005
Chapter 8: Recursion Java Software Solutions
Java Software Structures: John Lewis & Joseph Chase
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Jordi Cortadella Department of Computer Science
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Chapter 8: Recursion Java Software Solutions
Chapter 11 Recursion.
Chapter 8: Recursion Java Software Solutions
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Week 4 Lecture-2 Chapter 6 (Methods).
Dr. Sampath Jayarathna Cal Poly Pomona
Java Software Solutions Foundations of Program Design Sixth Edition
Recursive Thinking.
Presentation transcript:

A Peek at Programming or, problem solving in Computer Science Aaron Tan

2 Contents What is Computer Science (CS)? What is Problem Solving? What is Algorithmic Problem Solving? What is Programming?  Control structures  Recursion [A Peek at Programming, June 2010]

3 What is Computer Science? Computing Curricula 2001 (Computer Science) Report identifies 14 knowledge focus groups  Discrete Structures (DS)  Programming Fundamentals (PF)  Algorithms and Complexity (AL)  Architecture and Organization (AR)  Operating Systems (OS)  Net-Centric Computing (NC)  Programming Languages (PL)  Discrete Structures (DS)  Programming Fundamentals (PF)  Algorithms and Complexity (AL)  Architecture and Organization (AR)  Operating Systems (OS)  Net-Centric Computing (NC)  Programming Languages (PL)  Human-Computer Interaction (HC)  Graphics and Visual Computing (GV)  Intelligent Systems (IS)  Information Management (IM)  Social and Professional Issues (SP)  Software Engineering (SE)  Computational Science (CN)  Human-Computer Interaction (HC)  Graphics and Visual Computing (GV)  Intelligent Systems (IS)  Information Management (IM)  Social and Professional Issues (SP)  Software Engineering (SE)  Computational Science (CN) 3 [A Peek at Programming, June 2010] P = NP ? O(n2)O(n2)

4 Problem Solving Exercises The exercises in the next few slides are of varied nature, chosen to illustrate the extent of general problem solving. Different kinds of questions require different domain knowledge and strategies. Apply your problem solving skills and creativity here! [A Peek at Programming, June 2010]

5 Warm-up #1: Glasses of milk Six glasses are in a row, the first three full of milk, the second three empty. By moving only one glass, can you arrange them so that empty and full glasses alternate? [A Peek at Programming, June 2010]

6 Warm-up #2: Bear A bear, starting from the point P, walked one mile due south. Then he changed direction and walked one mile due east. Then he turned again to the left and walked one mile due north, and arrived at the point P he started from. What was the colour of the bear? [A Peek at Programming, June 2010]

7 Warm-up #3: Mad scientist A mad scientist wishes to make a chain out of plutonium and lead pieces. There is a problem, however. If the scientist places two pieces of plutonium next to each other, KA- BOOM!!! [A Peek at Programming, June 2010] In how many ways can the scientist safely construct a chain of length 6? General case: What about length n?

8 Warm-up #4: Silver chain A traveller arrives at an inn and intends to stay for a week. He has no money but only a chain consisting of 7 silver rings. He uses one ring to pay for each day spent at the inn, but the innkeeper agrees to accept no more than one broken ring. [A Peek at Programming, June 2010] How should the traveller cut up the chain in order to settle accounts with the innkeeper on a daily basis?

9 Warm-up #5: Dominoes Figure 1 below shows a domino and Figure 2 shows a 4  4 board with two squares at opposite corners removed. How do you show that it is not possible to cover this board completely with dominoes? Figure 1. A domino. Figure 2. A 4  4 board with 2 corner squares removed.  General case: How do you show the same for an n  n board with the two squares at opposite corners removed, where n is even?  Special case: How do you show the same for an n  n board with the two squares at opposite corners removed, where n is odd? [A Peek at Programming, June 2010]

10 Warm-up #6: Triominoes Figure 3 below shows a triomino and Figure 4 shows a 4  4 board with a defect (hole) in one square. How do you show that the board can be covered with triominoes?  General case: How do you show that a 2 n  2 n board (where n  1) with a hole in one square (anywhere on the board) can be covered with triominoes? Figure 3. A triomino. Figure 4. A 4  4 board with a hole. [A Peek at Programming, June 2010]

11 [A Peek at Programming, June 2010] Problem Solving Process (1/5) Analysis Design Implementation Testing Determine the inputs, outputs, and other components of the problem. Description should be sufficiently specific to allow you to solve the problem.

12 Problem Solving Process (2/5) Analysis Design Implementation Testing Describe the components and associated processes for solving the problem. [A Peek at Programming, June 2010]

13 Problem Solving Process (3/5) Analysis Design Implementation Testing Develop solutions for the components and use those components to produce an overall solution. [A Peek at Programming, June 2010]

14 Problem Solving Process (4/5) Analysis Design Implementation Testing Test the components individually and collectively. [A Peek at Programming, June 2010]

15 Problem Solving Process (5/5) [A Peek at Programming, June 2010] Analysis Design Implementation Testing Determine problem features Write algorithm Produce code Check for correctness and efficiency Rethink as appropriate

16 Algorithmic Problem Solving An algorithm is a well-defined computational procedure consisting of a set of instructions, that takes some value or set of values, as input, and produces some value or set of values, as output. Algorithm InputOutput ExactTerminateEffectiveGeneral [A Peek at Programming, June 2010]

17 Programming [A Peek at Programming, June 2010] Java constructs Problem solving Program

18 A Java Program (Bingo.java) [A Peek at Programming, June 2010] // Display a message. public class Bingo { public static void main(String[] args) { System.out.println("B I N G O !"); } Comment Class name Method name Method body Output

19 Another Java Program (Welcome.java) [A Peek at Programming, June 2010] // Author: Aaron Tan // Purpose: Ask for user’s name and display a welcome message. import java.util.*; public class Welcome { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("What is your name? "); String name = scanner.next(); System.out.println("Hi " + name + "."); System.out.println("Welcome!"); } API package Creating a Scanner object Input An object of class String

20 Control Structures Control structures determine the flow of control in a program, that is, the order in which the statements in a program are executed/evaluated. 20 [A Peek at Programming, June 2010] Sequence (default) Branching/ Selection if-else switch Loop/ Repetition for while do while

21 Algorithm: Example #1 Compute the average of three integers. 21 [A Peek at Programming, June 2010] A possible algorithm: enter values for num1, num2, num3 ave  ( num1 + num2 + num3 ) / 3 print ave num1 Variables used: num2num3 ave Another possible algorithm: enter values for num1, num2, num3 total  ( num1 + num2 + num3 ) ave  total / 3 print ave num1 Variables used: num2num3 ave total

22 Algorithm: Example #2 Arrange two integers in increasing order (sort). 22 [A Peek at Programming, June 2010] Algorithm A: enter values for num1, num2 // Assign smaller number into final1, // larger number into final2 if ( num1 < num2 ) then final1  num1 final2  num2 else final1  num2 final2  num1 // Transfer values in final1, final2 back to num1, num2 num1  final1 num2  final2 // Display sorted integers print num1, num2 Variables used: num1num2 final1final2

23 Algorithm: Example #2 (cont.) Arrange two integers in increasing order (sort). 23 [A Peek at Programming, June 2010] Algorithm B: enter values for num1, num2 // Swap the values in the variables if necessary if ( num2 < num1 ) then temp  num1 num1  num2 num2  temp // Display sorted integers print num1, num2 Variables used: num1num2 temp

24 Algorithm: Example #3 Find the sum of positive integers up to n (assuming that n is a positive integer). 24 [A Peek at Programming, June 2010] Algorithm: enter value for n // Initialise a counter count to 1, and ans to 0 count  1 ans  0 while ( count  n ) do ans  ans + count// add count to ans count  count + 1// increase count by 1 // Display answer print ans Variables used: n count ans

25 Algorithmic Problem Solving #1: Maze 25 [A Peek at Programming, June 2010]

26 Algorithmic Problem Solving #2: Sudoku 26 [A Peek at Programming, June 2010]

27 Algorithmic Problem Solving #3: MasterMind (1/2) Sink: Correct colour, correct position Hit: Correct colour, wrong position 27 [A Peek at Programming, June 2010] Secret code SinksHits Guess #1Guess # Guess #3 22 Guess #4 40 Guess # Secret code SinksHits Guess #2Guess #3Guess #4

28 Algorithmic Problem Solving #3: MasterMind (2/2) 6 colours: R: Red B: Blue G: Green Y: Yellow C: Cyan M: Magenta 28 [A Peek at Programming, June 2010] Given a secret code (secret) and a player’s guess (guess), how do we compute the number of sinks and hits?

29 Recursion 29 [A Peek at Programming, June 2010]

30 Recursive Definitions A definition that defines something in terms of itself is a recursive definition.  The descendants of a person are the person’s children and all of the descendants of the person’s children.  A list of numbers is A number, or A number followed by a list of numbers. A recursion algorithm is one that invokes itself to solve smaller or simpler instance(s) of the problem. 30 [A Peek at Programming, June 2010]

31 Factorial Can be defined as: 31 [A Peek at Programming, June 2010] Or, by recursive definition:

32 Recursive Methods A recursive method generally has 2 parts:  A termination part that stops the recursion This is called the base case Base case should have simple solution Possible to have more than one base case  One or more recursive calls This is called the recursive case The recursive case calls the same method but with simpler or smaller arguments 32 [A Peek at Programming, June 2010] if ( base case satisfied ) { return value; } else { make simpler recursive call(s); }

33 Recursive Method for Factorial 33 [A Peek at Programming, June 2010] public static int factorial(int n) { if (n == 0) return 1; else return n * factorial(n-1); } Base case. Recursive case deals with a simpler (smaller) version of the same task.

34 Recursive Method for Factorial A recursive method generally has 2 parts:  A termination part that stops the recursion This is called the base case Base case should have simple solution Possible to have more than one base case  One or more recursive calls This is called the recursive case The recursive case calls the same method but with simpler or smaller arguments 34 [A Peek at Programming, June 2010]

35 Exercise: North-East Paths (1/2) Find the number of north-east paths between two points. North-east (NE) path: you may only move northward or eastward. How many NE-paths between A and C? C A A A A 35 [A Peek at Programming, June 2010] Let x and y be the rows and columns apart between the two points. Write recursive method ne(x, y) ne(1, 1) = 2 ne(1, 2) = 3 ne(2, 2) = ? ne(4, 6) = ?

36 Exercise: North-East Paths (2/2) 36 [A Peek at Programming, June 2010] public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter rows and columns apart: "); int rows = scanner.nextInt(); int cols = scanner.nextInt(); System.out.println("Number of North-east paths = " + ne(rows, cols)); } public static int ne(int x, int y) { } +

37 Towers of Hanoi (1/10) The classical “Towers of Hanoi” puzzle has attracted the attention of computer scientists more than any other puzzles. Invented by Edouard Lucas, a French mathematician, in There are 3 poles (A, B and C) and a tower of disks on the first pole A, with the smallest disk on the top and the biggest at the bottom. The purpose of the puzzle is to move the whole tower from pole A to pole C, with the following rules:  Only one disk can be moved at a time.  A bigger disk must not rest on a smaller disk. 37 [A Peek at Programming, June 2010]

38 Towers of Hanoi (2/10) We attempt to write a program to generate instructions on how to move the disks from pole A to pole C. Example: A tower with 3 disks. Output generated by program is as follows. It assumes that only the top disk can be moved. 38 [A Peek at Programming, June 2010] Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C

39 Towers of Hanoi (3/10) 39 [A Peek at Programming, June 2010] Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

40 Towers of Hanoi (4/10) 40 [A Peek at Programming, June 2010] Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

41 Towers of Hanoi (5/10) 41 [A Peek at Programming, June 2010] Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

42 Towers of Hanoi (6/10) 42 [A Peek at Programming, June 2010] Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

43 Towers of Hanoi (7/10) 43 [A Peek at Programming, June 2010] Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

44 Towers of Hanoi (8/10) 44 [A Peek at Programming, June 2010] Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC

45 Towers of Hanoi (9/10) 45 [A Peek at Programming, June 2010] Move disk from A to C Move disk from A to B Move disk from C to B Move disk from A to C Move disk from B to A Move disk from B to C Move disk from A to C ABC VIOLA!

46 Towers of Hanoi (10/10) 46 [A Peek at Programming, June 2010] public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print( "Enter number of disks: " ); int disks = scanner.nextInt(); towers(disks, 'A', 'B', 'C'); } public static void towers(int n, char source, char temp, char dest) { }  Check this out: +

47 Books on Computer Science/Algorithms Some recommended readings  How to Think about Algorithms Jeff Edmonds, Cambridge, 2008  Algorithmics: The Spirit of Computing David Harel, 2 nd ed, Addison-Wesley (3 rd ed. available)  Introduction to Algorithms T.H. Cormen, C.E. Leiserson, R.L. Rivest, C. Stein, 2 nd ed, MIT Press  The New Turing Omnibus: 66 Excursions in Computer Science A.K. Dewdney, Holt 47 [A Peek at Programming, June 2010]

48 THE END [A Peek at Programming, June 2010]