Iteration Preview of Coming Attractions The Need to Get Your Act Together A Process for Developing While Statements

Similar documents
Unit 4: Client View of a Component Methods

6.001 Notes: Section 4.1

CS103 Handout 29 Winter 2018 February 9, 2018 Inductive Proofwriting Checklist

Program Syntax; Operational Semantics

Divisibility Rules and Their Explanations

COMP 161 Lecture Notes 16 Analyzing Search and Sort

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

Proofwriting Checklist

n! = 1 * 2 * 3 * 4 * * (n-1) * n

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

6.001 Notes: Section 6.1

Repetition Through Recursion

introduction to Programming in C Department of Computer Science and Engineering Lecture No. #40 Recursion Linear Recursion

CS 161 Computer Security

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

Limits. f(x) and lim. g(x) g(x)

1.7 Limit of a Function

Table of Laplace Transforms

XP: Backup Your Important Files for Safety

Week - 04 Lecture - 01 Merge Sort. (Refer Slide Time: 00:02)

Slide 1 Side Effects Duration: 00:00:53 Advance mode: Auto

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

LOOPS. Repetition using the while statement

Chapter 4 Loops. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

CSCI 141 Computer Programming I. Filip Jagodzinski

Chapter 3. Set Theory. 3.1 What is a Set?

(Refer Slide Time: 0:19)

Some Extra Information on Graph Search

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING

Iteration. CSE / ENGR 142 Programming I. Chapter 5. Motivating Loops. What s Wrong with Fahrenheit/Celsius Program? One More Type of Control Flow

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

Chapter Goals. Contents LOOPS

Lecture Transcript While and Do While Statements in C++

7. Arrays, More Java Looping

Design and Analysis of Algorithms Prof. Madhavan Mukund Chennai Mathematical Institute. Week 02 Module 06 Lecture - 14 Merge Sort: Analysis

Animations involving numbers

Foundations, Reasoning About Algorithms, and Design By Contract CMPSC 122

What is Iteration? CMPT-101. Recursion. Understanding Recursion. The Function Header and Documentation. Recursively Adding Numbers

Lesson 6A Loops. By John B. Owen All rights reserved 2011, revised 2014

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

Text Input and Conditionals

6.001 Notes: Section 15.1

How to approach a computational problem

(Refer Slide Time: 1:27)

Note: This is a miniassignment and the grading is automated. If you do not submit it correctly, you will receive at most half credit.

Computer Science 210 Data Structures Siena College Fall Topic Notes: Searching and Sorting

Clean & Speed Up Windows with AWO

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

1 Getting used to Python

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

(Refer Slide Time: 00:26)

CSE 142/143 Unofficial Commenting Guide Eric Arendt, Alyssa Harding, Melissa Winstanley

CS103 Spring 2018 Mathematical Vocabulary

Basics of Computational Geometry

(Python) Chapter 3: Repetition

CS2304: Python for Java Programmers. CS2304: Sequences and Collections

Hardware versus software

This loop prints out the numbers from 1 through 10 on separate lines. How does it work? Output:

Iteration. # a and b are now equal # a and b are no longer equal Multiple assignment

6.00 Introduction to Computer Science and Programming Fall 2008

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Shortest Paths Date: 10/13/15

Repetition. Chapter 6

Topic C. Communicating the Precision of Measured Numbers

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

Assignment 2.4: Loops

Array Based Lists. Collections

CS125 : Introduction to Computer Science. Lecture Notes #38 and #39 Quicksort. c 2005, 2003, 2002, 2000 Jason Zych

Repetition. Chapter 6

Recursively Enumerable Languages, Turing Machines, and Decidability

CSC148, Lab #4. General rules. Overview. Tracing recursion. Greatest Common Denominator GCD

Greedy algorithms is another useful way for solving optimization problems.

Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Recursive Data and Recursive Functions

Lecture 3: Recursion; Structural Induction

SOFTWARE ENGINEERING DESIGN I

DRAFT. Dependent types. Chapter The power of and

Here are some of the more basic curves that we ll need to know how to do as well as limits on the parameter if they are required.

Imelda C. Go, South Carolina Department of Education, Columbia, SC

XNA Tutorials Utah State University Association for Computing Machinery XNA Special Interest Group RB Whitaker 21 December 2007

Problem 1: Building and testing your own linked indexed list

Module 10A Lecture - 20 What is a function? Why use functions Example: power (base, n)

This lecture presents ordered lists. An ordered list is one which is maintained in some predefined order, such as alphabetical or numerical order.

If you don t, it will return the same thing as == But this may not be what you want... Several different kinds of equality to consider:

The Beauty and Joy of Computing 1 Lab Exercise 9: Problem self-similarity and recursion - Python version

1 Lecture 5: Advanced Data Structures

8 Understanding Subtyping

CIS220 In Class/Lab 1: Due Sunday night at midnight. Submit all files through Canvas (25 pts)

Technical Section. Lab 4 while loops and for loops. A. while Loops or for loops

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

Handout 9: Imperative Programs and State

Advanced Operations Research Prof. G. Srinivasan Department of Management Studies Indian Institute of Technology, Madras

Principles of Computer Science I

Chapter 1. Math review. 1.1 Some sets

Big-O-ology 1 CIS 675: Algorithms January 14, 2019

CSE 2123 Recursion. Jeremy Morris

Hot X: Algebra Exposed

Transcription:

Iteration Preview of Coming Attractions In this unit be sure to look for a five-step process for developing while statements tracing while statements The Need to Get Your Act Together As you probably know already, developing a while statement can require a great deal of care. Because the body of a while is executed repeatedly, a single mistake can come back to haunt us not just once, not just twice, but an untold number of times! That's serious. In order to help keep you out of trouble, we ll explain a somewhat general process for developing while statements. The process is informal, intuitive, sensible, straightforward, and helpful. If you find yourself facing a particularly difficult while statement, fall back on the process and let it help you out. A Process for Developing While Statements 1. Make sure you understand what is given to the while statement and what its goal is. (You should think of the goal as what the while statement is supposed to accomplish.) In particular, make sure you understand what must be true of variable values 1.1 as execution first reaches the while statement 1.2 upon successful completion of the while statement. (You might think of 1.1 as a precondition or requires for the while and think of 1.2 a postcondition or ensures for the while.) 2. Think of an action that, if repeated a sufficient number times, will accomplish the goal of the while statement. The action will eventually morph into the body of the while statement and may consist of several steps. 3. Think of an appropriate continuation condition to go with the action from step 2. Unit 10-1

4. Write a skeleton of the while statement using an informal statement of the continuation condition from step 3 and an informal expression of the action, from step 2, for the body of the while. 5. Gradually refine the skeleton into the final while statement. A First Example Let s see how the process works on a real example. Informally, the problem is, given variable int n and variable double approx, assign to approx the summation of the first n terms in (1/1) (1 / 3) (1/ 5) (1/ 7). To get started, notice that iteration is necessary here because the number of terms to be added is not fixed, but rather depends on the value of n. Step 1 Understand the Given and the Goal Variables n and approx are given. n determines the number of terms to be added together. Let s assume that at least one term should appear in the summation; otherwise, there really isn t much to do. So we assume that n >= 1 when execution reaches the while statement. For now, we really don t care what value approx has when execution reaches the while statement, because the while statement will produce a value for approx. When the while statement is finished, let s ensure that n has the same value as before the while and ensure that approx has, for its value, the appropriate summation. Step 2 Think of an Appropriate Repeatable Action We need to compute the sum of n terms. So, one obvious piece of the action will be to add another term to approx. But, the terms aren t there for the picking they must be computed. Hence, another piece of the action will be to compute another term. Let s compute and add the terms in the order (1/1), then (1/ 3), then (1/ 5), and so on. It looks like our action sequence is: compute the next term and add it to approx Unit 10-2

Step 3 Think of a Continuation Condition The action from step 2 can be repeated as long as there are additional terms to be computed and added to approx. So, the continuation condition will be something like there are more terms to process Step 4 Write a Skeleton Given the action sequence and continuation condition from steps 2 and 3, here s a simple skeleton: while (there are more terms to process) { compute the next term and add it to approx; Step 5 Refine the Skeleton Into a While Statement The skeleton does a nice job of getting us organized. Now we just need to add some flesh to the bones by growing some good code. Adding the terms, one at a time, to approx should be easy. The more interesting challenge is computing the next term. For this problem, a little notation defining the terms goes a long way: t 1 (1/1) defines the first term; t 2 (1/ 3) defines the second term; t 3 (1 / 5) defines the third term, and so on. Can you see the pattern? What defines the i-th term? It looks like for all i 1, t i (1/(2i 1)). To take advantage of this insight, let s introduce a new variable for the while loop of type int and call it termnum. termnum will be used to keep track of the number of the current term being processed. To be very precise, we ll make sure that the while statement has the following key property: Unit 10-3

The Key Property EVERY TIME execution reaches the continuation condition of the while statement, the value of termnum will equal the number of the last term processed; that is, the number of the last term computed and added to approx. In other words, every time execution reaches the continuation condition, approx will be the summation. The body of the while statement will need to make sure that the value of termnum is properly updated. And, the continuation condition now can be stated formally as termnum < n. Putting these ideas together, here is the first version of the while statement: while (termnum < n) { // adjust the value of termnum termnum = termnum + 1; // compute the next term and add it to approx approx = approx + (1.0 / (2 * termnum 1)); We re almost there. The last detail concerns possibly setting the values of the variables just prior to the while statement. Remember the key idea: EVERY TIME execution reaches the continuation condition of the while statement the value of termnum must be the number of the last term computed and added to approx and approx must be the corresponding summation. This includes the very first time execution reaches the continuation condition, even before the body of the while statement has been executed even once. So, which term should be processed just before the very first time execution reaches the continuation condition? The simplest thing is to process term t 1 (1/1) only just prior to the while statement. This leads to: Unit 10-4

A Final Solution int termnum = 1; // processing term t1 double approx = 1.0; // approx = t1 while (termnum < n) { // adjust the value of term_num termnum = termnum + 1; // compute the next term and add it to approx approx = approx + (1.0 / (2 * termnum 1)); 10-1. Explain why the value of approx should be set to 1.0 just prior to the while statement. Consider the key property. Still Awake? 10-2. Would it be okay to use termnum <= n for the continuation condition? Explain your answer. 10-3. Could the final solution start with approx = 0.0 just before the while statement? If so, explain what other changes would have to be made to the final solution and why. 10-4. The final solution computes and sums the terms in the order t 1,t 2,t 3,. Develop another while statement that solves the same problem by computing and summing the terms in the order,t 3,t 2,t 1. 10-5. In the sequence of terms (1/1),(1/ 3),(1/ 5),(1/ 7), the denominators are 1, 3, 5, 7,. Suppose that we had an additional variable of type int named denominator and that we ensured that, every time execution reached the continuation condition, the value of denominator was equal to the denominator of the last term computed and added to approx. Develop another while statement that solves the same problem using the denominator variable. Unit 10-5

A Second Example The second example uses the SimpleReader and String components. Informally, for this problem, we are given a SimpleReader variable named input connected to a file that contains a sequence of number, string pairs, and the task is to count how many of these pairs have the property that the number is equal to the length of the string. Iteration is necessary here because the number of pairs to be considered is not fixed, but rather depends on the value of file to which input is connected. Step 1 Understand the Given and the Goal Variable input is given and has been connected to a file that contains the number, string pairs. Let s assume that the format of this file is a sequence of lines organized as: n1 string1 n2 string2 n3 string3 nk stringk for some k >= 0. (If k=0, then the file is empty; that is, the file contains no pairs.) Let s also assume that we have a variable of type int named count that we will use to store the number of pairs to be counted. When the while statement is finished, let s ensure that all pairs have been read from input so that input is empty and ensure that count has, for its value, the number of (n, string) pairs where n = length of string. Step 2 Think of an Appropriate Repeatable Action The repeatable action for this problem is fairly obvious get the next number and string from input increment count if the number matches the length of the string Unit 10-6

Step 3 Think of a Continuation Condition The action from step 2 can be repeated as long as there are additional pairs to read from input; that is, as long as input is not empty. So, the continuation condition will be something like input is not empty Step 4 Write a Skeleton Given the action sequence and continuation condition from steps 2 and 3, here s a simple skeleton: while (input is not empty) { get the next number and string from input increment count if number matches the length of string Step 5 Refine the Skeleton Into a While Statement To get a number-string pair from input, we need two new variables, say n of type int and str of type String. Then, the statements n = input.nextinteger(); str = input.nextline(); can be used to get another number and string. The length operation for String variables can be used to check whether the pair satisfies the required property. The key property for our solution will be: The Key Property EVERY TIME execution reaches the continuation condition of the while statement, the value of count will be exactly the number of pairs that have been read from input and that satisfy the required property. Unit 10-7

To state the continuation condition, we use the ateos operation for SimpleReader variables. This operation is specifically designed to test for emptiness. Applied to input, the operation call is input.ateos(), which you can read as input is at the end of stream. Here s the first version of the while statement: while (! input.ateos ()) { // get the next number and string n = input.nextinteger(); str = input.nextline(); // check if n is equal to the length of str // and if it is increment count if (n == str.length()) { count = count + 1; Once again, we need to make sure that the key property holds the very first time execution reaches the continuation condition before the body of the while statement has been executed even once. Just prior to the while, no pairs have been read from input, so count should be 0. A Final Solution int count = 0; // nothing counted yet while (! input.ateos ()) { // get the next number and string int n = input.nextinteger(); String str = input.nextline(); // check if n is equal to the length of str // and if it is increment count if (n == str.length()) { count = count + 1; Unit 10-8

10-6. If the variables n and str were declared before the while loop, what values should they be initialized to? Why? 10-7. Explain how you would modify the problem statement and Still Awake? solution if the input contained 3-tuples (instead of pairs), each 3-tuple had two integers and a string, and you needed to count how many 3-tuples have the property that the length of the string is between the two integers. Tricky Tracing Ahead Tracing while statements is a double-edged sword. Because the details of while statements can be quite intricate and difficult to get right, tracing on sample values would appear to be an attractive idea. However, because the values of variables can change each time the body of the while is executed, tracing while statements can be very tricky. Great care is needed to make sure that unexpected results during tracing are due to a mistake in the while-statement design and not due to erroneous tracing! A systematic, though tedious way to trace while statements is to use the cross em out technique. For each iteration of the body of the while, the values of the variables from the previous iteration are crossed out and new values recorded beside the old. Here s an example, where variables x, y, and m are of type int. (Note: the % operator is the Java remainder operator. For instance, 10 % 3 is 1 because 10 divided by 3 leaves a remainder of 1.) Unit 10-9

Statement while (y!= 0) { m = x % y; x = y; y = m; Variable Values x = 21 y = 15 m = 2917 x = 21 15 6 y = 15 6 3 m = 2917 6 3 x = 21 15 6 y = 15 6 3 m = 6 3 0 x = 15 6 3 y = 15 6 3 m = 6 3 0 x = 15 6 3 y = 6 3 0 m = 6 3 0 x = 3 y = 0 m = 0 Unit 10-10

10-8. Complete the following tracing table. Variables x, y, and q are of type int. Still Awake? Statement while (x >= y) { x = x - y; q++; // q = q + 1 Variable Values x = 103 y = 32 q = 0 x = y = q = x = y = q = x = y = q = x = y = q = Unit 10-11