Outline. CIS 110: Introduction to Computer Programming. Review: Interactive Sum. More Cumulative Algorithms. Interactive Sum Trace (2)

Similar documents
Outline. CIS 110: Intro to Computer Programming. What Do Our Programs Look Like? The Scanner Object. CIS 110 (11fa) - University of Pennsylvania 1

News. Recap: While Loop Example. Reading. Recap: Do Loop Example. Recap: For Loop Example

Terminal Window. 11. Section 7 Exercises Program Memory Exercise 7-1 Swap Values in an Array Working memory Global Memory. 2 nd call 3 rd call

Pass by Reference vs. Pass by Value

A Taste of Java and Object-Oriented Programming

Programming in Fortran 90 : 2017/2018

CMPS 10 Introduction to Computer Science Lecture Notes

Notes on Organizing Java Code: Packages, Visibility, and Scope

High level vs Low Level. What is a Computer Program? What does gcc do for you? Program = Instructions + Data. Basic Computer Organization

Parallelism for Nested Loops with Non-uniform and Flow Dependences

Kent State University CS 4/ Design and Analysis of Algorithms. Dept. of Math & Computer Science LECT-16. Dynamic Programming

Programming Assignment Six. Semester Calendar. 1D Excel Worksheet Arrays. Review VBA Arrays from Excel. Programming Assignment Six May 2, 2017

Esc101 Lecture 1 st April, 2008 Generating Permutation

Introduction to Programming. Lecture 13: Container data structures. Container data structures. Topics for this lecture. A basic issue with containers

KFUPM. SE301: Numerical Methods Topic 8 Ordinary Differential Equations (ODEs) Lecture (Term 101) Section 04. Read

Efficient Distributed File System (EDFS)

For instance, ; the five basic number-sets are increasingly more n A B & B A A = B (1)

Load Balancing for Hex-Cell Interconnection Network

Parallel Numerics. 1 Preconditioning & Iterative Solvers (From 2016)

Sequential search. Building Java Programs Chapter 13. Sequential search. Sequential search

Final Exam. CSE Section M, Winter p. 1 of 16. Family Name: Given Name(s): Student Number:

Harvard University CS 101 Fall 2005, Shimon Schocken. Assembler. Elements of Computing Systems 1 Assembler (Ch. 6)

Overview. CSC 2400: Computer Systems. Pointers in C. Pointers - Variables that hold memory addresses - Using pointers to do call-by-reference in C

Loop Transformations, Dependences, and Parallelization

Tsinghua University at TAC 2009: Summarizing Multi-documents by Information Distance

Conditional Speculative Decimal Addition*

An Optimal Algorithm for Prufer Codes *

Computer models of motion: Iterative calculations

CSCI 104 Sorting Algorithms. Mark Redekopp David Kempe

Computational Expression

This chapter discusses aspects of heat conduction. The equilibrium heat conduction on a rod. In this chapter, Arrays will be discussed.

Outline. Midterm Review. Declaring Variables. Main Variable Data Types. Symbolic Constants. Arithmetic Operators. Midterm Review March 24, 2014

Compiler Design. Spring Register Allocation. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Oracle Database: SQL and PL/SQL Fundamentals Certification Course

Assembler. Building a Modern Computer From First Principles.

Assignment # 2. Farrukh Jabeen Algorithms 510 Assignment #2 Due Date: June 15, 2009.

CS240: Programming in C. Lecture 12: Polymorphic Sorting

Brave New World Pseudocode Reference

printf( Please enter another number: ); scanf( %d, &num2);

K-means and Hierarchical Clustering

Virtual Memory. Background. No. 10. Virtual Memory: concept. Logical Memory Space (review) Demand Paging(1) Virtual Memory

Dijkstra s Single Source Algorithm. All-Pairs Shortest Paths. Dynamic Programming Solution. Performance. Decision Sequence.

BIN XIA et al: AN IMPROVED K-MEANS ALGORITHM BASED ON CLOUD PLATFORM FOR DATA MINING

Vectorization in the Polyhedral Model

9. BASIC programming: Control and Repetition

Lecture 5: Multilayer Perceptrons

Sorting Review. Sorting. Comparison Sorting. CSE 680 Prof. Roger Crawfis. Assumptions

Complex Numbers. Now we also saw that if a and b were both positive then ab = a b. For a second let s forget that restriction and do the following.

CS1100 Introduction to Programming

Arrays. Loops + arrays: challenge problem. Arrays (II) An array is a set of variables of the same type accessed by their index.

Problem Set 3 Solutions

Dijkstra s Single Source Algorithm. All-Pairs Shortest Paths. Dynamic Programming Solution. Performance

ELEC 377 Operating Systems. Week 6 Class 3

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Collaboratively Regularized Nearest Points for Set Based Recognition

MULTISPECTRAL IMAGES CLASSIFICATION BASED ON KLT AND ATR AUTOMATIC TARGET RECOGNITION

Course Introduction. Algorithm 8/31/2017. COSC 320 Advanced Data Structures and Algorithms. COSC 320 Advanced Data Structures and Algorithms

Multiple optimum values

INTEGER PROGRAMMING MODELING FOR THE CHINESE POSTMAN PROBLEMS

Strings. Piech, CS106A, Stanford University

Array transposition in CUDA shared memory

Exercise 1.1 Hello world

Estimation of Parallel Complexity with Rewriting Techniques

Introduction to C. Sami Ilvonen Petri Nikunen. Oct 6 8, CSC IT Center for Science Ltd, Espoo. int **b1, **b2;

Chapter 6 Methods. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

kccvoip.com basic voip training NAT/PAT extract 2008

Solving two-person zero-sum game by Matlab

The Codesign Challenge

CONCURRENT OPTIMIZATION OF MULTI RESPONCE QUALITY CHARACTERISTICS BASED ON TAGUCHI METHOD. Ümit Terzi*, Kasım Baynal

Midterms Save the Dates!

On Some Entertaining Applications of the Concept of Set in Computer Science Course

2D Raster Graphics. Integer grid Sequential (left-right, top-down) scan. Computer Graphics

Full file at

CS-201 Introduction to Programming with Java

CS 268: Lecture 8 Router Support for Congestion Control

NGPM -- A NSGA-II Program in Matlab

Midterms Save the Dates!

Full file at

Assembler. Shimon Schocken. Spring Elements of Computing Systems 1 Assembler (Ch. 6) Compiler. abstract interface.

OPL: a modelling language

Sorting. Sorted Original. index. index

Introduction to Programming, Aug-Dec 2006

Arithmetic and IO. 25 August 2017

Today Using Fourier-Motzkin elimination for code generation Using Fourier-Motzkin elimination for determining schedule constraints

Introduction to Geometrical Optics - a 2D ray tracing Excel model for spherical mirrors - Part 2

Setup and Use. Version 3.7 2/1/2014

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Parallel matrix-vector multiplication

124 Chapter 8. Case Study: A Memory Component ndcatng some error condton. An exceptonal return of a value e s called rasng excepton e. A return s ssue

CS-201 Introduction to Programming with Java

INCOMPETE DATA IN FUZZY INFERENCE SYSTEM

Sorting and Algorithm Analysis

LLVM passes and Intro to Loop Transformation Frameworks

Positive Semi-definite Programming Localization in Wireless Sensor Networks

Nachos Project 3. Speaker: Sheng-Wei Cheng 2010/12/16

Chapter 6: Methods. Objectives 9/21/18. Opening Problem. Problem. Problem. Solution. CS1: Java Programming Colorado State University

The Greedy Method. Outline and Reading. Change Money Problem. Greedy Algorithms. Applications of the Greedy Strategy. The Greedy Method Technique

The example below contains two doors and no floor level obstacles. Your panel calculator should now look something like this: 2,400

Smoothing Spline ANOVA for variable screening

Section 2.2 Your First Program in Java: Printing a Line of Text

Transcription:

Outlne CIS 110: Introducton to Computer Programmng More on Cumulatve Algorthms Processng Text Tacklng Programmng Problems Lecture 11 Text Processng and More On Desgn ( 4.2-4.3) 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 1 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 2 Revew: Interactve Sum More Cumulatve Algorthms 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 3 Can you wrte a program that computes the sum of numbers from 0 to the user's nput mnus 1? Scanner n = new Scanner(System.n); System.out.prnt("n? "); nt n = n.nextint(); System.out.prntln(); Some storage declared outsde the loop. nt sum = 0; for (nt = 0; < n; ++) { sum += ; Update the storage nsde of the loop. System.out.prntln( "Sum of 1 to " + Use the updated storage after the loop. 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 4 Interactve Sum Trace (1) Interactve Sum Trace (2) n 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 5 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 6 CIS 110 (11fa) - Unversty of Pennsylvana 1

Interactve Sum Trace (3) Interactve Sum Trace (4) n n n 5 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 7 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 8 Interactve Sum Trace (5) Interactve Sum Trace (6) n n 5 n n 5 sum 0 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 9 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 10 Interactve Sum Trace (7) Interactve Sum Trace (8) 0 0 n n 5 sum 0 n n 5 sum 0 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 11 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 12 CIS 110 (11fa) - Unversty of Pennsylvana 2

Interactve Sum Trace (9) Interactve Sum Trace (10) 1 1 n n 5 sum 0 n n 5 sum 0 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 13 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 14 Interactve Sum Trace (11) Interactve Sum Trace (12) 1 2 n n 5 sum 1 n n 5 sum 1 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 15 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 16 Interactve Sum Trace (13) Interactve Sum Trace (13) 2 3 n n 5 sum 3 n n 5 sum 3 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 17 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 18 CIS 110 (11fa) - Unversty of Pennsylvana 3

Interactve Sum Trace (14) Interactve Sum Trace (15) 3 4 n n 5 sum 6 n n 5 sum 6 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 19 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 20 Interactve Sum Trace (16) Interactve Sum Trace (17) 4 5 n n 5 sum 10 n n 5 sum 10 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 21 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 22 Interactve Sum Trace (18) n n 5 sum 10 Cumulatve Algorthms and Condtonals Problem: calculate the mnmum of 10 numbers entered by the user. publc statc nt promptfornumber(scanner n, nt ) { System.out.prnt("Enter number " + + ": "); return n.nextint(); publc statc vod man(strng[] args) { Scanner n = new Scanner(System.n); nt mn = promptfornumber(n, 1); // Storage for (nt = 2; <= 10; ++) { nt num = promptfornumber(n, ); // Modfy storage f (num < mn) { mn = num; // Use storage System.out.prntln("The mnmum s " + mn + "."); 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 23 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 24 CIS 110 (11fa) - Unversty of Pennsylvana 4

Recall: charat(ndex) Text Processng Scanner n = new Scanner(System.n); Strng lne = n.nextlne(); for (nt = 0; < lne.length(); ++) { // charat() returns the th character of the strng // Remember strng ndces start at 0... System.out.prntln(lne.charAt()); 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 25 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 26 The char Type A prmtve that represents a sngle character. Represented as a 16-bt nteger (character value). All our prmtve operatons on numbers apply! char c1 = 'c'; f (c1 == 'c') { System.out.prntln(c1 + " s c!"); // Int + Char = Int whch s 32 bts, so we need to // cast to tell the compler we don't mnd the truncaton. char c2 = (char) (c1 + 5); System.out.prntln("5 chars from " + c1 + " s " + c2); // Tests to see f c2 s a lowercase number. f (c2 >= 'a' c2 <= 'z') { System.out.prntln(c2 + " s between 'a' and 'z'!"); 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 27 The Character class Helpful statc methods for dealng wth chars. nt fve = Character.getNumercValue('5'); System.out.prntln("The number value of 5 s " + fve); System.out.prntln("Is 6 a dgt? " + Character.sDgt('6')); System.out.prntln("Is q a letter? " + Character.sLetter('q')); System.out.prntln("Is z lowercase? " + Character.sLowerCase('z')); System.out.prntln("Is R uppercase? " + Character.sUpperCase('R')); System.out.prntln("Lowercase Z s " + Character.toLowerCase('Z')); System.out.prntln("Uppercase a s " + Character.toUpperCase('a')); 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 28 Cumulatve Text Algorthms Smlar to our numerc cumulatve algorthms. // Echos the lne of text, alternatng captal case of the nput. Scanner n = new Scanner(System.n); System.out.prnt("Enter a lne of text: "); Strng lne = n.nextlne(); Strng result = ""; for (nt = 0; < lne.length(); ++) { char letter = lne.charat(); f ( % 2 == 0) { letter = Character.toUpperCase(letter); result += letter; System.out.prntln(result); 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 29 System.out.prntf An alternatve to prntln/prnt that lets you format the output. System.out.prntf("Example of prntf: %d %.2f %s", 12, 1.241, "Chowder"); > Example of prntf: 12 1.24 Chowder Specfers have the form: %<formattng><type> Need to provde one argument per format specfer. They are consumed n-order. A format specfer. A placeholder for a thng to prnt. See p. 260 of the book for more nformaton about format specfers. 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 30 CIS 110 (11fa) - Unversty of Pennsylvana 5

System.out.prntf (example) Scanner n = new Scanner(System.n); for (nt = 0; < 3; ++) { System.out.prnt("Enter the employee's name: "); Strng name = n.nextlne(); System.out.prnt("Enter the employee's salary: "); double salary = n.nextdouble(); System.out.prntf("%10s -> %6.2f\n", name, salary); n.nextlne();// flush the buffer Tacklng Program Problems Enter the employee's name: Joe Enter the employee's salary: 1410.14 Joe -> 1410.14 Enter the employee's name: Lamont Enter the employee's salary: 4104.41 Lamont -> 4104.41 Enter the employee's name: Fred Enter the employee's salary: 10041.4190482 Fred -> 10041.42 See PalndromeChecker.java 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 31 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 32 Sample Problem Problem: wrte a program that reads n a Strng from the user, checks to see f that Strng s a palndrome, and nforms the user of the results of the check. Example output > Enter a strng to check: > abba > The reverse of the lne s: abba > The lne s a palndrome! Our Methodology 1. Try some example nputs to get a feel for the problem. 2. Start wth a skeleton of the soluton. 3. Decompose the problem nto sub-problems. 4. Make helper methods to solve the subproblems. 5. Use those helper methods to solve your man problem. 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 33 10/17/2011 CIS 110 (11fa) - Unversty of Pennsylvana 34 CIS 110 (11fa) - Unversty of Pennsylvana 6