CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants

Slides:



Advertisements
Similar presentations
Nested for loops Cont’d. 2 Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? –Real graphics require a.
Advertisements

Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Week 2 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Building Java Programs
Copyright 2009 by Pearson Education Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos: Ch.
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-3: Loop Figures and Constants reading: self-checks: 27 exercises:
1 BUILDING JAVA PROGRAMS CHAPTER 2 Pseudocode and Scope.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-2: The for Loop reading: 2.3 self-check: exercises: 2-14 videos:
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
Building Java Programs Chapter 2 Primitive Data and Definite Loops Copyright (c) Pearson All rights reserved.
Nested for loops.
Topic 6 loops, figures, constants Based on slides bu Marty Stepp and Stuart Reges from "Complexity has and will maintain.
Copyright 2008 by Pearson Education 1 Nested loops reading: 2.3 self-check: exercises: videos: Ch. 2 #4.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 2: Primitive Data and Definite Loops.
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2016 Lecture 9: input ; if/else Adapted from slides by Marty Stepp and Stuart Reges.
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 17: while Loops and Sentinel Loops
Building Java Programs Chapter 2
Building Java Programs
CSCI 161 – Introduction to Programming I William Killian
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
Building Java Programs Chapter 2
CSc 110, Spring 2017 Lecture 8: input; if/else
CSc 110, Autumn 2016 Lecture 12: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
CSc 110, Spring 2017 Lecture 11: while Loops, Fencepost Loops, and Sentinel Loops Adapted from slides by Marty Stepp and Stuart Reges.
Building Java Programs Chapter 2
CSc 110, Spring 2017 Lecture 9: Advanced if/else; Cumulative sum
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
CSc 110, Spring 2018 Lecture 16: Fencepost Loops and while loops
CSc 110, Autumn 2016 Lecture 9: input; if/else
CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Building Java Programs
CSc 110, Autumn 2016 Lecture 10: Advanced if/else; Cumulative sum
CSc 110, Spring 2018 Lecture 7: input and Constants
CSc 110, Spring 2018 Lecture 5: The for Loop and user input
Building Java Programs
Building Java Programs
Building Java Programs
Topic 6 loops, figures, constants
Building Java Programs
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
CSc 110, Spring 2018 Lecture 8: input and Parameters
Building Java Programs
The for loop suggested reading:
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Suggested self-checks:
Building Java Programs
Drawing complex figures
Building Java Programs
Building Java Programs
Building Java Programs
Chapter 2 Lecture 2-2: The for Loop reading: 2.3
Building Java Programs
Building Java Programs
Optional Topic: User Input with Scanner
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants 2/18/2019 CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants Adapted from slides by Marty Stepp and Stuart Reges Can you write this in Python? 1

Exercise Write a function that produces the following output: T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! The end.

Changing step size Add a third number to the end of range, this is the step size A negative number will count down instead of up print("T-minus ") for i in range(10, 0, -1): print(str(i) + ", ", end="") print("blastoff!") print("The end.") Output: T-minus 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, blastoff! The end.

Loop tables Make a table to represent any patterns on each line. ....1 ...2 ..3 .4 5 To print a character multiple times, use a for loop. for j in range(1, 5): print(".") # 4 dots line # of dots 1 4 2 3 5 -1 * line -1 -2 -3 -4 -5 -1 * line + 5 4 3 2 1

Solution Answer: Output: for line in range(1, 6): print("." * (-1 * line + 5) , end='') print(line) Output: ....1 ...2 ..3 .4 5

2/18/2019 Constants and figures Consider the task of drawing the following scalable figure: +/\/\/\/\/\/\/\/\/\/\+ | | | | Multiples of 5 occur many times +/\/\/\/\+ | | | | The same figure at size 2 6

Constant tables What equation would cause the code to print: SIZE = ... What equation would cause the code to print: 2 7 12 17 22 To see patterns, make a table of SIZE and the numbers. Each time SIZE goes up by 1, the number should go up by 5. But SIZE * 5 is too great by 3, so we subtract 3. SIZE number to print 5 * SIZE 1 2 5 7 10 3 12 15 4 17 20 22 25 5 * SIZE - 3 2 7 12 17 22

Constant tables question What equation would cause the code to print: 17 13 9 5 1 Let's create the constant table together. Each time SIZE goes up 1, the number printed should ... But this multiple is off by a margin of ... SIZE number to print 1 17 2 13 3 9 4 5 -4 * SIZE -4 -8 -12 -16 -20 -4 * SIZE -4 * SIZE+ 21 -4 17 -8 13 -12 9 -16 5 -20 1

Interactive programs interactive program: Reads input from the console. While the program runs, it asks the user to type input. The input typed by the user is stored in variables in the code. Can be tricky; users are unpredictable and misbehave. But interactive programs have more interesting behavior.

input input: An function that can read input from the user. Using an input object to read console input: name = input(prompt) Example: name = input("type your name: ") The variable name will store the value the user typed in

input example Console (user input underlined): age 29 29 def main(): age = input("How old are you? ") years = 65 - age print(years, " years until retirement!") Console (user input underlined): How old are you? age 29 It's also useful to write a program that prompts for multiple values, both on the same line or each on its own line. 29 Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> print(65 - age) TypeError: unsupported operand type(s) for -: 'int' and 'str'

input example Console (user input underlined): age 29 years 36 29 def main(): age = int(input("How old are you? ")) years = 65 - age print(years, "years until retirement!") Console (user input underlined): How old are you? 36 years until retirement! age 29 years 36 It's also useful to write a program that prompts for multiple values, both on the same line or each on its own line. 29

Drawing complex figures Use nested for loops to produce the following output. Why draw ASCII art? Real graphics require a lot of finesse ASCII art has complex patterns Can focus on the algorithms #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

Development strategy Recommendations for managing complexity: 1. Design the program (think about steps or methods needed). write an English description of steps required use this description to decide the functions 2. Create a table of patterns of characters use table to write your for loops #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

1. Pseudo-code pseudo-code: An English description of an algorithm. Example: Drawing a 12 wide by 7 tall box of stars print 12 stars. for (each of 5 lines) : print a star. print 10 spaces. ************ * *

Pseudo-code algorithm 1. Line # , 16 =, # 2. Top half | spaces (decreasing) <> dots (increasing) spaces (same as above) 3. Bottom half (top half upside-down) 4. Line #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

Functions from pseudocode def main(): line() top_half() bottom_half() def top_half(): for line in range(1, 5): # contents of each line def bottom_half() { def line(): # ...

2. Tables A table for the top half: Compute spaces and dots expressions from line number line spaces line * -2 + 8 dots 4 * line - 4 1 6 2 4 3 8 12 line spaces dots 1 6 2 4 3 8 12 #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

3. Writing the code Useful questions about the top half: Number of (nested) loops per line? #================# | <><> | | <>....<> | | <>........<> | |<>............<>|