Outline. CIS 110: Introduction to Computer Programming. Announcements. For Loops. Redundancy in Patterns. A Solution with Our Current Tools

Similar documents
CIS 110: Introduction to Computer Programming

Outline. CIS 110: Introduction to Computer Programming. What is Computer Science? What is computer programming? What is computer science?

CIS 110: Introduction to Computer Programming

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

CIS 110: Introduction to Computer Programming

Chapter 4: Control structures. Repetition

Chapter 4: Control structures

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 8: SEP. 29TH INSTRUCTOR: JIAYIN WANG

Building Java Programs Chapter 2

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

Building Java Programs Chapter 2. bug. Primitive Data and Definite Loops. Copyright (c) Pearson All rights reserved. Software Flaw.

Building Java Programs Chapter 2

Recap: Assignment as an Operator CS 112 Introduction to Programming

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Top-Down Program Development

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Definite Loops. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Using a Variable for Counting

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

CS 112 Introduction to Programming

CIS 110: Introduction to Computer Programming. Lecture 2 Decomposition and Static Methods ( 1.4)

Building Java Programs

Syntax of Variable Declarations. Variables Usages as Expressions. Self-assignment Statements. Assignment. Scoping and Naming Rules

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

CS 177 Week 15 Recitation Slides. Review

Loops. Variable Scope Remapping Nested Loops. Donald Judd. CS Loops 1. CS Loops 2

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Give one example where you might wish to use a three dimensional array

Repetition with for loops

STUDENT LESSON A12 Iterations

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

Repetition Structures

Lecture 12: Barrier Synchronization

Condition-Controlled Loop. Condition-Controlled Loop. If Statement. Various Forms. Conditional-Controlled Loop. Loop Caution.

Lecture Notes for Chapter 2: Getting Started

Last Class. While loops Infinite loops Loop counters Iterations

COMP 202. Programming With Iterations. CONTENT: The WHILE, DO and FOR Statements. COMP Loops 1

Programming in OOP/C++

Loops / Repetition Statements

Topic 6 loops, figures, constants

Repetition CSC 121 Fall 2014 Howard Rosenthal

Final Exam. COMP Summer I June 26, points

SCHOOL OF COMPUTING, ENGINEERING AND MATHEMATICS SEMESTER 1 EXAMINATIONS 2015/2016 CI101 / CI177. Programming

Drawing Geometrical Objects. Graphic courtesy of Eric Roberts

Primitive data, expressions, and variables

Repetition and Loop Statements Chapter 5

Introduction. C provides two styles of flow control:

Using Microsoft Word. Tables

Nested Loops ***** ***** ***** ***** ***** We know we can print out one line of this square as follows: System.out.

CSE P 501 Exam 8/5/04

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

Lecture 3: Loops. While Loops. While Loops: Powers of Two. While Loops: Newton-Raphson Method

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

`Three sides of a 500 square foot rectangle are fenced. Express the fence s length f as a function of height x.

1.00 Lecture 5. Floating Point Anomalies

Outline for Today CSE 142. Programming a Teller Machine. CSE142 Wi03 I-1. ATM Algorithm for Dispensing Money

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

CS111: PROGRAMMING LANGUAGE II

Course Content. Objectives of Lecture 20 Arrays. Outline of Lecture 20. CMPUT 102: Arrays Dr. Osmar R. Zaïane. University of Alberta 4

Computers Programming Course 7. Iulian Năstac

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Java Coding 3. Over & over again!

TOPIC 2 INTRODUCTION TO JAVA AND DR JAVA

Flow Control. CSC215 Lecture

Honors Computer Science C++ Mr. Clausen Program 4A, 4B, 4C, 4D, 4G

Conditionals and Loops

Building Java Programs

Building Java Programs


All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Topic 6 Nested for Loops

Outline. CIS 110: Introduction to Computer Programming. Exam announcements. Programming assertions revisited. An extended example: mystery

CSCI-1200 Data Structures Spring 2014 Lecture 5 Pointers, Arrays, Pointer Arithmetic

COEN244: Class & function templates

Learning outcomes. COMPSCI 101 Principles of Programming. Drawing 2D shapes using Characters. Printing a Row of characters

ECE 122. Engineering Problem Solving with Java

COMP Summer 2015 (A01) Jim (James) Young jimyoung.ca

*Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* COMPUTER PROGRAMMING LECTURE 05 LOOPS IMRAN IHSAN

Darrell Bethea May 25, 2011

6.189 Project 1. Readings. What to hand in. Project 1: The Game of Hangman. Get caught up on all the readings from this week!

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Goal. Generic Programming and Inner classes. Minor rewrite of linear search. Obvious linear search code. Intuitive idea of generic linear search

Scope of this lecture. Repetition For loops While loops

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

1007 Imperative Programming Part II

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

Chapter 3. Iteration

Honors Computer Science C++ Mr. Clausen Program 4A, 4B, 4C (4D, 4G)

CSCI 1103: Introduction

Topics. Introduction to Repetition Structures Often have to write code that performs the same task multiple times. Controlled Loop

Java Programming. Computer Science 112

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

CS 1110: Introduction to Computing Using Python Loop Invariants

CS 132 Exam #1 - Study Suggestions

For loops, nested loops and scopes. Jordi Cortadella Department of Computer Science

1.3 Conditionals and Loops

C++ for Java Programmers

Transcription:

Outline CIS 110: Introduction to Computer Programming 1. For-loops! 2. Algorithm Design and Pseudocode Lecture 5 The Loop-the-Loop ( 2.3-2.4) 1 2 Announcements Date of the final is tentatively: MONDAY, DECEMBER 19 th, 6-8 PM If you have a conflict, please let me know ASAP. Need more practice? Try Practice-it! Web-based tool where you can work on practice problems that are automatically checked online. Linked off of the course webpage. For Loops 3 4 Redundancy in Patterns Problem: write a program that prints out successive cubes. Output: 0^3 = 0 1^3 = 1 2^3 = 8 3^3 = 27 4^3 = 64 A Solution with Our Current Tools System.out.println( 0^3 = + 0 * 0 * 0); System.out.println( 1^3 = + 1 * 1 * 1); System.out.println( 2^3 = + 2 * 2 * 2); System.out.println( 3^3 = + 3 * 3 * 3); System.out.println( 4^3 = + 4 * 4 * 4); Pretty obvious repetition, but we have no way of dealing with it 5 6 CIS 110 (11fa) - University of Pennsylvania 1

Our First For-loop The for loop construct allows us to express repetitive patterns in a structured way. The For-loop The For-loop is a control statement. Doesn t do anything on its own, but instead controls the execution of other statements. Initialization Continuation Test Update Body 7 8 For-loop Semantics Loop iteration Test is true Initialization Test Body Update Next int i = 0 i < 5 println i++ Our Example, Step-by-step (1) Test is false 9 10 Our Example, Step-by-step (2) Our Example, Step-by-step (3) 11 12 CIS 110 (11fa) - University of Pennsylvania 2

Our Example, Step-by-step (4) Our Example, Step-by-step (5) 4 3 3 < 5 is true 3^3 = 27 13 14 Our Example, Step-by-step (6) 4 3 3 < 5 is true 3^3 = 27 5 4 4 < 5 is true 4^3 = 64 15 Our Example, Step-by-step (7) 4 3 3 < 5 is true 3^3 = 27 5 4 4 < 5 is true 4^3 = 64 6 5 5 < 5 is false 16 For-loop Bounds Different sorts of bounds are possible! for (int i = 1; i <= 5; i++) { for (int i = 5; i > 0; i--) { for (int i = 256; i > 0; i /= 2) { for (int i = -3; i < 10; i += 2) { Output: 1 2 3 4 5 Output: 5 4 3 2 1 Output: 256 128 64 32 16 8 4 2 1 Output: -3-1 1 3 5 7 9 < versus <= Bounds i = 0; i < 5 versus i = 1; i <= 5 : Both give 5 loop iterations. i = 0, 1, 2, 3, 4 versus i = 1, 2, 3, 4, 5. i = 0; i < 5 is less intuitive, more canonical Most computations are naturally zero-based. For homework 2, either is fine. Try to get used to the zero-based style. 17 18 CIS 110 (11fa) - University of Pennsylvania 3

Yo Dawg, I Heard You Liked Loops Problem: draw a rectangle of stars. Solution: Loopception public class Rectangle { // The outer for-loop controls the #/lines // The inner for-loop controls the // contents of a single line. for (int j = 0; j < 5; j++) { 19 20 Careful with Your Nested Loop Bounds! for (int j = 0; j < 5; j++) { for (int j = 0; j < i; j++) { for (int j = 0; j < 5; i++) { * ** *** **** Infinite loop! 21 Algorithm Design and Pseudocode 22 Remember Compilation? English is Still Useful! You (the programmer) The compiler The computer (your new best friend) Sometimes it is difficult to write an algorithm/computer program directly. PUT THE JELLY ON THE BREAD AND THEN PUT IT ALL IN YOUR MOUTH Ilagay ang halaya SA tinapay AT pagkatapos a y ilagay ito LAHAT SA INYONG bibig Problem: Draw 5 rows each containing 5 stars /*??? */ The computer program Machine code Pseudocode! 23 24 CIS 110 (11fa) - University of Pennsylvania 4

Pseudocode Helps Organize Your Thoughts Stuck and don t know how to make progress? Write an English description of your solution. Transform that English description into code. Draw 5 rows each containing 5 stars for (Each of 5 rows) { Draw 5 stars. for (Each of 5 rows) { for (int j = 0; j < 5; j++) { 25 By Example: A Cone Problem: draw the following cone shape. /\ /--\ /----\ /------\ /--------\ Draw 5 rows each containing a section of the cone. Draw a section of the cone 26 Cone Sections From Tables to Code Each line has the following form: <spaces> / <dashes> \ Let s find the pattern for each part! Iteration/row (i) Spaces / Dashes \ 0 4 1 0 1 1 3 1 2 1 2 2 1 4 1 3 1 1 6 1 4 0 1 8 1 Formula for spaces: 4 - i Formula for dashes: i * 2 /\ /--\ /----\ /------\ /--------\ Draw a section of the cone Draw spaces Draw / Draw dashes Draw \ for (int j = 0; j < 4 i; j++) { System.out.print( ); System.out.print( / ); for (int j = 0; j < i * 2; j++) { System.out.print( - ); System.out.print( \\ ); 27 28 Stop Being So Constant What value controls the height of the cone? for (int j = 0; j < 4 i; j++) { System.out.print( ); System.out.print( / ); for (int j = 0; j < i * 2; j++) { System.out.print( - ); System.out.print( \\ ); The height of the cone but not immediately obvious! An example of a magic number. Surprise! An indirect use of the height of the cone. If we change the height, then this number needs to change as well! (Bad) Example: Quake III Arena float Q_rsqrt( float number ) { long i; float x2, y; const float threehalfs = 1.5F; x2 = number * 0.5F; y = number; i = * ( long * ) &y; i = 0x5f3759df - ( i >> 1 ); // evil floating point bit level hacking // what the fuck? y = * ( float * ) &i; y = y * ( threehalfs - ( x2 * y * y ) ); // 1st iteration // y = y * ( threehalfs - ( x2 * y * y ) ); // 2nd iteration, this can be removed return y; Note: code is written in C but should be (somewhat) understandable! This is the exact source from the game! The constant was a source of much debate! See Fast Inverse Square Root @ Wikipedia for more details. 29 30 CIS 110 (11fa) - University of Pennsylvania 5

Solution to Magic Numbers: Class Constants Class constants let us document magic numbers by naming them and giving us a central place to change their values if necessary. 9/21/2011 public class Cone { public static final int HEIGHT = 5; for (int i = 0; i < HEIGHT; i++) { for (int j = 0; j < (HEIGHT 1) i; j++) { System.out.print( ); System.out.print( / ); for (int j = 0; j < i * 2; j++) { System.out.print( - ); System.out.print( \\ ); CIS 110 (11fa) - University of Pennsylvania 31 Syntax of Class Constants public class Cone { public static final int HEIGHT = 5; for (int i = 0; i < HEIGHT; i++) { /* Snip! */ Constants are declared outside of methods but inside the class. public static final <type> <name> = <value>; Constants are variables that cannot be reassigned. Convention is to NAME_THEM_LIKE_THIS. 32 CIS 110 (11fa) - University of Pennsylvania 6