CSc 110, Autumn 2016 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 Class constants and scope reading: 2.4 self-check: 28 exercises: 11 videos: Ch. 2 #5.
Week 2 ______ \ ^__^ \ (oo)\_______ (__)\ )\/\ ||----w | || ||
Building Java Programs
Derived from Building Java Programs by Stuart Reges & Marty Stepp
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.
1 Building Java Programs Chapter 3: Introduction to Parameters and Objects These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They.
CS 112 Introduction to Programming Variable Scoping; Nested Loops; Parameterized Methods Yang (Richard) Yang Computer Science Department Yale University.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 4: Loop Figures and Constants reading:
Building Java Programs Chapter 2 Primitive Data and Definite Loops.
Nested for loops.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE 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
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
Adapted from slides by Marty Stepp and Stuart Reges
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
CSc 110, Spring 2017 Lecture 6: Parameters (cont.) and Graphics
Building Java Programs Chapter 2
Building Java Programs Chapter 2
CSc 110, Spring 2017 Lecture 5: Constants and Parameters
CSc 110, Spring 2018 Lecture 9: Parameters, Graphics and Random
CSc 110, Autumn 2017 Lecture 9: Graphics and Nested Loops
Topic 7 Nested Loops Case Study
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
CSc 110, Spring 2017 Lecture 4: Nested Loops and Loop Figures
Building Java Programs
CSc 110, Spring 2018 Lecture 7: input and Constants
CSc 110, Spring 2018 Lecture 5: The for Loop and user input
Building Java Programs
Variables variable: A piece of the computer's memory that is given a name and type, and can store a value. Like preset stations on a car stereo, or cell.
Building Java Programs
CSc 110, Spring 2018 Lecture 5: Loop Figures and Constants
Building Java Programs
Topic 6 loops, figures, constants
Building Java Programs
CSE 190D, Winter 2013 Building Java Programs Chapter 2
CSc 110, Spring 2018 Lecture 8: input and Parameters
Building Java Programs
CSc 110, Spring 2018 Lecture 10: More Graphics
Building Java Programs
Chapter 2 Programming Basics.
CSE 142 Lecture Notes Global Constants, Parameters, Return Values
Building Java Programs
Building Java Programs
CSc 110, Autumn 2017 Lecture 8: More Graphics
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
Building Java Programs
Chapter 2 Lecture 2-3: Loop Figures and Constants reading:
Presentation transcript:

CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants 12/3/2018 CSc 110, Autumn 2016 Lecture 5: Loop Figures and Constants Adapted from slides by Marty Stepp and Stuart Reges Can you write this in Python? 1

Nested for loop exercise 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

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

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 #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

Methods 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? #================# | <><> | | <>....<> | | <>........<> | |<>............<>|

Partial solution # Prints the expanding pattern of <> for the top half of the figure. def top_half(): for line in range(1, 5): print("|", end="") for space in range(1, line * -2 + 9): print(" ", end="") print("<>", end="") for dot in range(1, line * 4 - 3): print(".", end="") for space in range(1, line * -2 + 8): print("|")

Class constants and scope 12/3/2018 Class constants and scope 12

Scaling the mirror Let's modify our Mirror program so that it can scale. The current mirror (left) is at size 4; the right is at size 3. We'd like to structure the code so we can scale the figure by changing the code in just one place. #================# | <><> | | <>....<> | | <>........<> | |<>............<>| #============# | <><> | | <>....<> | |<>........<>|

Constants constant: A fixed value visible to the whole program. value should only be set only at declaration; shouldn't be reassigned Syntax: Just like declaring a normal variable: name = value name is usually in ALL_UPPER_CASE Examples: DAYS_IN_WEEK = 7 INTEREST_RATE = 3.5 SSN = 658234569

12/3/2018 Constants and figures Consider the task of drawing the following scalable figure: +/\/\/\/\/\/\/\/\/\/\+ | | | | Multiples of 5 occur many times +/\/\/\/\+ | | | | The same figure at size 2 15

Repetitive figure code 12/3/2018 Repetitive figure code def main(): draw_line() draw_body() def draw_line(): print("+", end="") for i in range(1, 11): print("/\\", end="") print("+") def draw_body(): for line in range(1, 6): print("|", end="") for spaces in range(1, 21): print(" ", end="") print("|") 16

Adding a constant HEIGHT = 5 def main(): draw_line() draw_body() 12/3/2018 Adding a constant HEIGHT = 5 def main(): draw_line() draw_body() def draw_line(): print("+", end="") for i in range(1, HEIGHT * 2 + 1): print("/\\", end="") print("+") def draw_body(): for line in range(1, HEIGHT + 1): print("|", end="") for spaces in range(1, HEIGHT * 4 + 1): print(" ", end="") print("|") 17

Complex figure w/ constant Modify the Mirror code to be resizable using a constant. A mirror of size 4: #================# | <><> | | <>....<> | | <>........<> | |<>............<>| A mirror of size 3: #============# | <><> | | <>....<> | |<>........<>|

Loop tables and constant Let's modify our loop table to use SIZE This can change the amount added in the loop expression #================# #============# | <><> | | <><> | | <>....<> | | <>....<> | | <>........<> | |<>........<>| |<>............<>| |<>........<>| |<>............<>| | <>....<> | | <>........<> | | <><> | | <>....<> | #============# | <><> | #================# SIZE line spaces dots 4 1,2,3,4 6,4,2,0 0,4,8,12 3 1,2,3 4,2,0 0,4,8

Partial solution SIZE = 4; # Prints the expanding pattern of <> for the top half of the figure. def top_half() { for line in range(1, SIZE): print("|", end="") for space in range(1, line * -2 + (2*SIZE) + 1): print(" ", end="") print("<>", end="") for dot in range(1, line * 4 - 3): print(".", end="") print("|")

Observations about constant 12/3/2018 Observations about constant The constant can change the "intercept" in an expression. Usually the "slope" is unchanged. SIZE = 4; for space in range(1, line * -2 + (2 * SIZE)): print(" ", end="") It doesn't replace every occurrence of the original value. for dot in range(1, line * 4 – 4 + 1): print(".", end="")