CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.

Slides:



Advertisements
Similar presentations
COSC2007 Data Structures II
Advertisements

AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
EXAMPLES (Arrays). Example Many engineering and scientific applications represent data as a 2-dimensional grid of values; say brightness of pixels in.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
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.
Eight queens puzzle. The eight queens puzzle is the problem of placing eight chess queens on an 8×8 chessboard such that none of them are able to capture.
Backtracking What is backtracking?
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Chapter 7 – Arrays.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
Fibonacci Numbers A simple example of program design.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
Additional control structures. The if-else statement The if-else statement chooses which of two statements to execute The if-else statement has the form:
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Programming Fundamentals I (COSC-1336), Lecture 8 (prepared after Chapter 7 of Liang’s 2011 textbook) Stefan Andrei 4/23/2017 COSC-1336, Lecture 8.
Problem Solving using the Java Programming Language May 2010 Mok Heng Ngee Day 5: Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
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.
Contents of Chapter 7 Chapter 7 Backtracking 7.1 The General method
Two Dimensional Arrays
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Working with arrays (we will use an array of double as example)
Two Dimensional Arrays. Two-dimensional Arrays Declaration: int matrix[4][11]; 4 x 11 rows columns
CS 100Lecture 171 CS100J Lecture 17 n Previous Lecture –Programming concepts n Binary search n Application of the “rules of thumb” n Asymptotic complexity.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Array - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/19/20151.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
CS 100Lecture 171 CS100A Lecture 17 n Previous Lecture –Programming concepts n Two-dimensional arrays –Java Constructs n Constructors for two-dimensional.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
Contest Algorithms January 2016 Pseudo-code for backtracking search, and three examples (all subsets, permutations, and 8- queens). 4. Backtracking 1Contest.
CS 180 Recitation 7 Arrays. Used to store similar values or objects. An array is an indexed collection of data values of the same type. Arrays are the.
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 Tirgul 11: Recursion & Backtracking. 2 Elements of a recursive solution (Reminder) A base case that is so simple we need no computation to solve it.
Chapter 8 – Arrays and Array Lists
CS100J Lecture 19 Previous Lecture This Lecture
Sit-In Lab 1 Ob-CHESS-ion
TK1114 Computer Programming
Building Java Programs
Algorithm Design and Analysis (ADA)
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
Operators Laboratory /11/16.
A simple example of program design
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
python.reset() Also moving to a more reasonable room (CSE 403)
AKA the birth, life, and death of variables.
Lecture Notes – Week 4 Chapter 5 (Loops).
Week 4 Lecture-2 Chapter 6 (Methods).
CIS 110: Introduction to Computer Programming
Algorithms: Design and Analysis
Lecture 13 Introduction to High-Level Programming (S&G, §§7.1–7.6)
Repetition Statements
Two dimensional arrays.
CS100J Lecture 18 Previous Lecture Programming concepts This Lecture
Announcements Assignment #4 is due tonight. Last lab program is going to be assigned this Wednesday. ◦ A backtracking problem.
Two dimensional arrays.
Presentation transcript:

CS 100Lecture 191 CS100J Lecture 19 n Previous 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. n This Lecture –Representation Rules of Thumb. n Transform problems to simpler equivalent problems n Maintain duplicate representations if helpful n Choose representations that limit search spaces n Find representations that yield uniform algorithms.

CS 100Lecture 192 Think // Print the sum of the integers from 1 through n. System.out.println(______________________________); n Don’t use brute force just because the computer is a brute.

CS 100Lecture 193 Ricocheting Bullet n A 1-by-1 box has an opening of width d. Shoot a gun into the box at angle . How far does the bullet travel? n Transform problems to simpler equivalent problems. 1 foot  d

CS 100Lecture 194 Ricocheting Bullet, continued n Transform problems to simpler equivalent problems.

CS 100Lecture 195 Ricocheting Bullet, continued n Transform problems to simpler equivalent problems y x

CS 100Lecture 196 Ricocheting Bullet, continued /* == the x corresponding to y and th. */ static double x( double y, double th ) { return (y / Math.tan( th )) ; } /* == the smallest even y > 0 for which the fractional part of the corresponding x is not larger than d.*/ static double min_y( double d, double th ) { int y = 2; while ( (x(y,th) - Math.floor(x(y,th))) > d ) y = y + 2; return y; } /* == x^2 */ static double sqr( double x ) { return x * x; } /* == distance traveled by bullet. */ static double distance( double d, double th ) { double y = min_y(d, th ); return Math.sqrt( sqr( x(y,th) ) + sqr(y) ); }

CS 100Lecture 197 Tic Tac Toe n Maintain duplicate representations if helpful B MovesX sumX BB

CS 100Lecture 198 Magic Square

CS 100Lecture 199 Eight Queens n Choose representations that limit the search space B R

CS 100Lecture 1910 Eight Queens, continued /* Solve the Eight Queens problem. */ static void main(String args[])) { /* R[c] is row of queen in column c, for 0 <= c <= 7. */ for 0 <= c <= 7. */ int [] R = { 0, 1, 2, 3, 4, 5, 6, 7 }; int [] R = { 0, 1, 2, 3, 4, 5, 6, 7 }; /* Consider each permutation of R until one is found that represents a solution, or loop forever. */ or loop forever. */ while ( same_diagonal(R) ) while ( same_diagonal(R) ) next_permutation(R); next_permutation(R); /* Output solution R. */...}

CS 100Lecture 1911 Eight Queens, continued B Positive diagonal index is row+column

CS 100Lecture 1912 Eight Queens, continued Negative diagonal index is column-row B

CS 100Lecture 1913 Eight Queens, continued // == 1 if R has two queens on same diagonal, else 0 static boolean same_diagonal( int [] R ) { boolean [] PosDiag = new boolean[15]; boolean [] PosDiag = new boolean[15]; boolean [] NegDiag = new boolean[15]; boolean [] NegDiag = new boolean[15]; // Set PosDiag and NegDiag to all false. // Set PosDiag and NegDiag to all false. for (int i = 0; i<=14; i++) { PosDiag[i] = false; NegDiag[i] = false; PosDiag[i] = false; NegDiag[i] = false; } // Set same to true if R has 2 queens on same diag. // Set same to true if R has 2 queens on same diag. boolean same = false; int c = 0; // column index while ( c <= 7 && !same ) { if ( PosDiag[ R[c] + c ] || if ( PosDiag[ R[c] + c ] || NegDiag[ c - R[c] + 7 ] ) NegDiag[ c - R[c] + 7 ] ) same = true; same = true; else { else { PosDiag[ R[c] + c ] = true; PosDiag[ R[c] + c ] = true; NegDiag[ c - R[c] + 7 ] = true; NegDiag[ c - R[c] + 7 ] = true; c++; c++; } } return same; }

CS 100Lecture 1914 Checkers n Find representations that yield uniform algorithms B

CS 100Lecture 1915 Checkers, continued B

CS 100Lecture 1916 Checkers, continued B free red moves red shifted right 5