CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional.

Slides:



Advertisements
Similar presentations
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Advertisements

CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
A Review. a review of lessons learned so far… ( 2 steps forward - 1 step back) Software Development Cycle: design, implement, test, debug, document Large.
Backtracking COP Backtracking  Backtracking is a technique used to solve problems with a large search space, by systematically trying and eliminating.
CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 More on Arrays Passing arrays to or from methods Arrays of objects Command line arguments Variable length parameter lists Two dimensional arrays Reading.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
Introduction to Methods
Lecture 4 Loops.
Matrices Jordi Cortadella Department of Computer Science.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Lecture 5: Backtracking Depth-First Search N-Queens Problem Hamiltonian Circuits.
CS 100Lecture 51 CS100J Lecture 5 n Previous Lecture –Programming Concepts n Rules of thumb –learn and use patterns –inspiration from hand-working problem.
2 dimensional arrays Steven Wood ©2005. Arrays dimensions Java allows arrays with many subscripts 2-D examples Chess board Excel spreadsheet.
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 17 More Recursive Backtracking reading: "Appendix R" on course web site slides created by Marty Stepp and Hélène Martin
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
1 CS 410 Mastery in Programming Chapter 10 Hints for Simple Sudoku Herbert G. Mayer, PSU CS Status 7/29/2013.
Chapter 2: Java Fundamentals
CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity.
CS100A, Fall 1997, Lecture 91 CS100A, Fall 1997 Lecture 9, Tuesday, 30 September Input/Output & Program Schema System.in, class Text, Some basic data processing,
For loops, nested loops and scopes Jordi Cortadella Department of Computer Science.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Introduction to Programming (in C++) Multi-dimensional vectors Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
The for loop.
CSE 143 Lecture 18 More Recursive Backtracking slides created by Marty Stepp
Grid References.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
CS1101: Programming Methodology Preparing for Practical Exam (PE)
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Two Dimensional Arrays. Students will be able to: code algorithms to solve two- dimensional array problems. use 2-D arrays in programs. pass two-use 2-D.
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Windows Programming Lecture 03. Pointers and Arrays.
CSG3F3/ Desain dan Analisis Algoritma
Two-Dimensional Arrays
Department of Computer Science
CS100J Lecture 19 Previous Lecture This Lecture
For loops, nested loops and scopes
Java Software Structures: John Lewis & Joseph Chase
Case Study 2 – Marking a Multiple-choice Test
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
Two-Dimensional Arrays
Presented By: David Miller
Scope of variables class scopeofvars {
Building Java Programs
EECE.2160 ECE Application Programming
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
Backtracking.
Building Java Programs
Presentation transcript:

CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional arrays n Initializers for arrays n final n This Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use of comments as high-level specifications: –as high-level commands, –as representation invariants. –Incremental development and testing. –Use of sentinels. –Static declarations. –Local declarations, scope, and the reuse of names. –Heuristic algorithms.

CS 100Lecture 172 Knight’s Tour n Chess is played on an 8-by-8 board. n A knight can move 2 in one direction (horizontal or vertical) and 1 in the other direction (vertical or horizontal). For example, Kn can move to each square marked X. n Problem. Write a program that tries to find a "knight's tour", e.g., starting in the upper left corner, the knight visits each of the 64 squares exactly once. Kn X X X X X X XX

CS 100Lecture 173 Possible Solution

CS 100Lecture 174 Representation n Rule of Thumb. Avoid using literal constants in the code; define and use symbolic constants. n Static class declarations visible to all methods. class Knight { /* Chess board B is N-by-N int array, for N == 8. */ final static int N = 8; static int [][] B = new int [N][N]; /* Unvisited squares are Blank. */ static final int Blank = 0; /* Board subscripts range from lo to hi. */ static final int lo = 0; static final int hi = 7; /* UNDEFINED, an illegal coordinate. */ static final int UNDEFINED = -1; }

CS 100Lecture 175 Main /* Try to find a Knight's Tour. */ static void main(String args[]) { /* Set B to all Blank. */ Initialize(); /* Set B to arrangement of the integers 1,2,3,... representing consecutive positions of the knight during the tour. Squares that the knight cannot reach remain Blank. */ Solve(); /* Print B in an 8-by-8 grid. */ Display();}

CS 100Lecture 176 Initialize /* Set B to all Blank. */ static void Initialize() { for (int r = lo; r <= hi; r++) for (int c = lo; c <= hi; c++) for (int c = lo; c <= hi; c++) B[r][c] = Blank; B[r][c] = Blank;}

CS 100Lecture 177 Solve /* Set B to arrangement of the integers 1,2,3,... representing consecutive positions of the knight during the tour. Squares that the knight cannot reach remain Blank. */ static void Solve() { /* This procedure "stub" permits us to test the main program, Initialize, and Display routines. */ }

CS 100Lecture 178 Display /* Print B in an 8-by-8 grid. */ static void Display() { for (int r = lo; r <= hi; r++) { for (int c = lo; c <= hi; c++) for (int c = lo; c <= hi; c++) System.out.print(B[r][c] + ” ”); System.out.print(B[r][c] + ” ”);System.out.println();}} n n Rule of Thumb. Develop your program using small procedures. Test it incrementally.

CS 100Lecture 179 while ( ________________________) { } Sample Intermediate State

CS 100Lecture 1710 Solve { /* Initial configuration. */ /* Initial configuration. */ int move = 1; // moves so far. int move = 1; // moves so far. int r = lo; // current row of Kn. int r = lo; // current row of Kn. int c = lo; // current column of Kn. int c = lo; // current column of Kn. B[r][c] = 1; B[r][c] = 1; while (r != UNDEFINED) { while (r != UNDEFINED) { /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */ /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */ if (r != UNDEFINED) { if (r != UNDEFINED) { move++; move++; B[r][c] = move; B[r][c] = move; } }}

CS 100Lecture 1711 Better Solve { /* Initial configuration. */ /* Initial configuration. */ int move = 0; // moves so far. int move = 0; // moves so far. int r = lo; // current row of Kn. int r = lo; // current row of Kn. int c = lo; // current column of Kn. int c = lo; // current column of Kn. while (r != UNDEFINED) { while (r != UNDEFINED) { move++; move++; B[r][c] = move; B[r][c] = move; /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */ /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */ }}

CS 100Lecture 1712 Choosing a Neighbor to Visit /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */ int unvisitedR = UNDEFINED; int unvisitedC = UNDEFINED; for (int k = 0; k < 8; k++) { int Nrow = _____________________________; int Ncol = _____________________________; if ( B[Nrow][Ncol] == Blank ){ unvisitedR = Nrow; unvisitedC = Ncol; }} r = unvisitedR; c = unvisitedC;

CS 100Lecture 1713 Neighbors // int deltaR[] = {-1, -2, -2, -1, 1, 2, 2, 1}; int deltaC[] = { 2, 1, -1, -2, -2, -1, 1, 2}; Kn

CS 100Lecture 1714 Boundary Conditions n where X can be any non-zero value Must revise Initialize method to fill in border. (Exercise) Must revise Initialize method to fill in border. (Exercise) x x x x x x x x x x x x x x x x x x x

CS 100Lecture 1715 Representation Revisited Static class declarations visible to all methods. Static class declarations visible to all methods. /* Chess board B is N-by-N int array, for N == 12. */ final int N = 12; int [][] B = new int [N][N]; /* Unvisited squares are Blank. */ final int Blank = 0; /* Board subscripts range from lo to hi. */ final int lo = 2; final int hi = 9; /* UNDEFINED, an illegal coordinate. */ final int UNDEFINED = -1; /* is offset to neighbor k. */ final static int deltaR[] = {-1,-2,-2,-1, 1, 2,2,1}; final static int deltaC[] = {2, 1,-1,-2,-2,-1,1,2};

CS 100Lecture 1716 Improvement Using Heuristic /* Let be coordinates of an unvisited "neighbor" of current, or if current square has no unvisited neighbors. */ int fewest = 9; // min unvisited neighbors int neighborR = UNDEFINED; int neighborC = UNDEFINED; for (int k = 0; k < 8; k++) { int Nrow = r + deltaR[k]; int Ncol = c + deltaC[k]; if ( B[Nrow][Ncol] == Blank ){ int n = unvisited(Nrow, Ncol); int n = unvisited(Nrow, Ncol); if (n < fewest ) { if (n < fewest ) { neighborR = Nrow; neighborC = Ncol; neighborR = Nrow; neighborC = Ncol; fewest = n; fewest = n; } } } r = neighborR; c = neighborC; n Go where the options are running out, i.e., go to the unvisited neighbor with the fewest unvisited neighbors.

CS 100Lecture 1717 Unvisited /* == the number of neighbors of square that are unvisited. */ static int unvisited(int r, int c) { int count = 0; // # unvisited neighs. for (int k = 0; k < 8; k++) { int Nrow = r + deltaR[k]; int Ncol = c + deltaC[k]; if ( B[Nrow][Ncol] == Blank ) count++; } return count; }