CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity.

Slides:



Advertisements
Similar presentations
CS 1031 Recursion (With applications to Searching and Sorting) Definition of a Recursion Simple Examples of Recursion Conditions for Recursion to Work.
Advertisements

Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT5: Array (1D and 2D) CS2311 Computer Programming.
CS 100Lecture 61 CS100J Lecture 6 n Previous Lecture –Programming Concepts n Programming by stepwise refinement –a pattern –sequential refinement –case.
CS100A, Fall 1997, Lecture 111 CS100A, Fall 1997 Lecture 11, Tuesday, 7 October Introduction to Arrays Concepts: Array declaration and allocation Subscripting.
ECE122 L14: Two Dimensional Arrays March 27, 2007 ECE 122 Engineering Problem Solving with Java Lecture 14 Two Dimensional Arrays.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Multidimensional Arrays in Java Vidhu S. Kapadia.
Chapter 8 Search and Sort Asserting Java ©Rick Mercer.
CS 100Lecture 41 CS100J Lecture 4 n Previous Lecture –Programming Concepts n iteration n programming patterns (templates) –Java Constructs n while-statements.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Introduction of Arrays. Arrays Array form an important part of almost all programming language. It provides a powerful feature and can be used as such.
CS 4731: Computer Graphics Lecture 8: 3D Affine transforms Emmanuel Agu.
Addressing Modes Chapter 11 S. Dandamudi To be used with S. Dandamudi, “Fundamentals of Computer Organization and Design,” Springer,  S.
Week 11 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CS 100Lecture 51 CS100J Lecture 5 n Previous Lecture –Programming Concepts n Rules of thumb –learn and use patterns –inspiration from hand-working problem.
COSC 1P03 Introduction to Data Structures 1.1 COSC 1P03  Audience  planning to major in COSC (prereq. 1P02 60%)  Lectures (AS202, AS217), labs (J301),
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.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Working with arrays (we will use an array of double as example)
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,
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
CS 100Lecture 21 CS100J: Lecture 2 n Previous Lecture –Programming Concepts n problem, algorithm, program, computer, input, output, sequential execution,
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.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Arrays of Arrays An array can represent a collection of any type of object - including other arrays! The world is filled with examples Monthly magazine:
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional.
CS 100Lecture 111 CS100J Lecture 11 n Previous Lecture –Scope of names and the lifetime of variables n blocks and local variables n methods and parameters.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 100Lecture71 CS100J Lecture 7 n Previous Lecture –Computation and computational power –Abstraction –Classes, Objects, and Methods –References and aliases.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
CS 100Lecture 11 Introduction to Programming n What is an algorithm? n Input and output n Sequential execution n Conditional execution Reading: Chapter.
Chapter 9 Nested Loops and Two-Dimensional Arrays Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin,
©2004 Joel Jones 1 CS 403: Programming Languages Lecture 3 Fall 2004 Department of Computer Science University of Alabama Joel Jones.
Java Programming Language Lecture27- An Introduction.
Windows Programming Lecture 03. Pointers and Arrays.
Arrays Chapter 7.
The need for Programming Languages
Two-Dimensional Arrays
CS100J Lecture 19 Previous Lecture This Lecture
OPERATORS (1) CSC 111.
Chapter 12 Recursion (methods calling themselves)
Number Patterns.
CS100J Lecture 11 Previous Lecture This Lecture
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
CS100J Lecture 7 Previous Lecture This Lecture Java Constructs
CS100J Lecture 3 Previous Lecture This Lecture Programming Concepts
College Algebra Chapter 6 Matrices and Determinants and Applications
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS100J Lecture 15 Previous Lecture This Lecture Sorting
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
CS100J Lecture 16 Previous Lecture This Lecture Programming concepts
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity –Java Constructs n Conditional Expressions n This Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional arrays n Initializers for arrays n final –Reading n Lewis & Loftus Section 6.3 n Savitch, Section 6.5

CS 100Lecture 172 The 15 Puzzle (Sam Loyd 1878) initial configuration n 15 numbered tiles in a 4-by-4 frame: final configuration Problem. Given a sequence of “moves” as input, say whether or not the sequence of moves changes the initial configuration into the final configuration.

CS 100Lecture 173 Representation of Moves n How should a move be represented in the input data? MoveNameMeans 1moveDownfill the blank from above 2moveUpfill the blank from below 3moveRightfill the blank from left 4moveLeftfill the blank from right 0endMoveend of data n Named Constants in Java final int endMove = 0; final int moveDown = 1; final int moveUp = 2; final int moveRight = 3; final int moveLeft = 4;

CS 100Lecture 174 Representation of the Tiles n How should the tiles be represented? n We need 15 values to represent the tiles and one to represent the blank? // Use for the tiles and 0 for blank. final int Blank = 0; or // Use for the tiles and 16 for blank. final int Blank = 16; n Rule of Thumb. Choose uniform representations if at all possible

CS 100Lecture 175 Representation of the Frame n We need 16 variables, one to represent the tile at each position in the frame. n We could use the declaration: int[] puz = new int[16]; to get variables puz[0],..., puz[15], but this is unwieldy and unnatural. n Rule of Thumb. Unless there is a compelling reason algorithmic reason to do otherwise, use data that is structured similarly to the physical device being simulated. n We use a two-dimensional array.

CS 100Lecture 176 Two Dimensional Arrays n Declaration of two-dimensional array: // Create 4-by-4 array named puz. int [][] puz = new int[4][4]; n which is equivalent to: // Create 4-by-4 array named puz. int [][] puz = new int[4][]; puz[0] = new int[4]; puz[1] = new int[4]; puz[2] = new int[4]; puz[3] = new int[4]; puz

CS 100Lecture 177 Rows and Columns n Vocabulary –row-major order in a 2-D array is 1st row, then 2nd row, then 3rd row, etc. –column-major order in a 2-D array is 1st column, then 2nd column, then 3rd column, etc. n The default interpretation of a 2-D array is to consider the first index the row, and the second index the column: –puz[r][c] is the variable in row r and column c –puz[r] is row r

CS 100Lecture 178 Program Outline /* If the sequence of moves transforms the puzzle from the initial configuration into the final configuration, output “yes”, else output “no”. */ static void test() { TokenReader in = new TokenReader(System.in); final int Blank = 16; final int endMove = 0; final int moveDown = 1; final int moveUp = 2; final int moveRight = 3; final int moveLeft = 4; /* Let puz be the initial configuration, the integers in row major order. */... /* Update puz according to each move. */... /* Say whether puz is final configuration, i.e., in column major order. */... }

CS 100Lecture 179 Initialization n Solution 1: /* Let puz be the initial configuration, the integers in row major order. */ int [][] puz = new int [4][4]; for (int r = 0; r < 4; r++) for (int c = 0; c < 4; c++) for (int c = 0; c < 4; c++){ puz[r][c] = ____________________; puz[r][c] = ____________________;} n Solution 2: /* Let puz be the initial configuration, the integers in row major order. */ int [][] puz = new int [4][4]; for (int r = 0; r < 4; r++) for (int c = 0; c < 4; c++) for (int c = 0; c < 4; c++) puz[r][c] = ____________________________;

CS 100Lecture 1710 Puzzle Updating n Rule of Thumb. Use relevant patterns. /* Update puz according to each move. */ int m;// current move m = in.readInt(); while ( m != endMove ) { // Process move m m = in.readInt(); }

CS 100Lecture 1711 Relative Coordinate System Introduce variables rowB, colB to track the blank. Introduce variables rowB, colB to track the blank. /* rowB,colB is position of blank, initially the lower-right hand corner. */ int rowB = 3; int colB = 3; n Picture the frame relative to the position of blank (in general, after the blank has moved around puz[rowB-1] [colB-1] puz[rowB] [colB-1] puz[rowB+1] [colB-1] puz[rowB-1] [colB] puz[rowB] [colB] puz[rowB+1] [colB] puz[rowB-1] [colB+1] puz[rowB] [colB+1] puz[rowB+1] [colB+1]

CS 100Lecture 1712 Exhaustive Solution // Process move m. if ( m==moveDown ){ // fill from above puz[rowB][colB] = puz[rowB-1][colB]; puz[rowB-1][colB] = Blank; rowB--;} else if ( m==moveUp ) { // fill from below puz[rowB][colB] = puz[rowB+1][colB]; puz[rowB+1][colB] = Blank; rowB++;} else if ( m==moveRight) {// fill from left puz[rowB][colB] = puz[rowB][colB-1]; puz[rowB][colB-1] = Blank; colB--;} else {// fill from right puz[rowB][colB] = puz[rowB][colB+1]; puz[rowB][colB+1] = Blank; colB++;}

CS 100Lecture 1713 Puzzle Updating: Uniform Treatment n Rule of Thumb. Avoid case analysis if possible. /* Update puz according to each move. */ int m;// current move m = in.readInt(); while ( m != endMove ) { // Process move m. // Blank moves to rowNew, colNew. int rowNew = _________________________; int colNew = _________________________; puz[rowB][colB] = puz[rowNew][colNew]; puz[rowNew][colNew] = Blank; rowB = rowNew; colB = colNew; m = in.readInt(); }

CS 100Lecture 1714 Array Initializers n A 1-dimensional array can be initialized with: { comma-separated list of expressions } n Example: /* Offsets to new position of blank on move m are delatR[m] and deltaC[m]. */ int [] deltaR = {0, -1, 1, 0, 0}; int [] deltaR = {0, -1, 1, 0, 0}; int [] deltaC = {0, 0, 0, -1, 1}; int [] deltaC = {0, 0, 0, -1, 1}; n A 2-dimensional array can be initialized with: { comma-separated list of array-initializers } n Example (solution 3 for setting the initial configuration): int [][] puz = { {1,2,3,4}, {5,6,7,8},{9,10,11,12}, {13,14,15,16} };

CS 100Lecture 1715 Puzzle Updating: Final Version /* Update puz according to each move. */ /* Offsets to new position of blank on move m are delatR[m] and deltaC[m]. */ int [] deltaR = {0, -1, 1, 0, 0}; int [] deltaC = {0, 0, 0, -1, 1}; /* rowB,colB is position of blank, initially the lower-right hand corner. int rowB = 3; int colB = 3; int m;// current move m = in.readInt(); while ( m != endMove ) { // Process move m. // Blank moves to rowNew, colNew. int rowNew = rowB + deltaR[m]; int rowNew = rowB + deltaR[m]; int colNew = colB + deltaC[m]; puz[rowB][colB] = puz[rowNew][colNew]; rowB = rowNew; colB = colNew; m = in.readInt(); } puz[rowB][colB] = Blank;