Last Time: Rolling a Weighted Die

Size: px
Start display at page:

Download "Last Time: Rolling a Weighted Die"

Transcription

1 Last Time: Rolling a Weighted Die import math/rand func DieRoll() int { return rand.intn(6) + 1

2 Multiple Rolls When we run this program 100 times, we get the same outcome! func main() int { fmt.println(dieroll())

3 Seeding in Go Go has a method of generating an infinite list of pseudorandom numbers. Calling rand.seed(n) for some integer n tells Go how to start when generating this list. import ( math/rand ) func main() int { rand.seed(200) fmt.println(dieroll())

4 Seeding in Go Go has a method of generating an infinite list of pseudorandom numbers. Calling rand.seed(n) for some integer n tells Go how to start when generating this list. import ( math/rand ) func main() int { rand.seed(200) fmt.println(dieroll()) // prints same every time!

5 Seeding in Go Better: Seed based on when the function is called (gives the appearance of randomness). import ( math/rand ) func main() int { rand.seed(time.now().utc().unixnano()) fmt.println(dieroll())

6 Warmup Exercise: Simulating Craps CrapsWinExpectation(trials) count ß 0 for i ß 1 to trials roll ß sum of two dice if roll = 2, 3, or 12 count-- (player loses) else if roll = 7 or 11 count++ (player wins) else // 4, 5, 6, 8, 9, or 10 rollagain ß true while rollagain roll2 ß sum of two dice if roll2 = roll count++ (player wins) rollagain = false else if roll2 = 7 count-- (player loses) rollagain = false return count/trials

7 Warmup Exercise: Simulating Craps func CrapsWinExpectation(trials int) float64 { count := 0 for i:=1; i<=trials; i++ { diceroll := DiceRoll() if diceroll == 2 diceroll == 3 diceroll == 12 { count-- else if diceroll == 7 diceroll == 11 { count++ //winner winner else { // rollagain := true for rollagain { diceroll2 := DiceRoll() if diceroll2 == diceroll { count++ rollagain = false else if diceroll2 == 7 { count-- rollagain = false return float64(count)/float64(trials)

8 Random Walks

9 Random Walks

10 Random Walk Problem Input: Integers n and s. Output: Each step of a random walk with s steps in an n x n chessboard.

11 Planning Random Walk Code English Pseudocode Go

12 English 1. Start at (x, y) = (n/2, n/2). 2. Make n random steps of the form below, making sure at each step that we don t jump out of the grid. 3. Print the coordinates at each step.

13 Pseudocode Helps Us Program Top-Down! RandomWalk(n, steps) x, y ß n/2, n/2 print (x, y) for i ß 0 to steps 1 x, y ß RandomStep(x, y, n) print (x, y)

14 Pseudocode Helps Us Program Top-Down! RandomWalk(n, steps) x, y ß n/2, n/2 print (x, y) for i ß 0 to steps 1 x, y ß RandomStep(x, y, n) print (x, y) RandomStep(): Input: coordinates (x, y) as well as an integer n. Output: coordinates of random step within n x n grid. Tricky: dealing with steps at edge cases that fall off grid.

15 Pseudocode Helps Us Program Top-Down! RandomWalk(n, steps) x, y ß n/2, n/2 print (x, y) for i ß 0 to steps 1 x, y ß RandomStep(x, y, n) print (x, y) RandomStep(x, y, n) a ß x b ß y while (a, b) = (x, y) or (a, b) not in n x n field a ß x + RandomDelta() b ß y + RandomDelta() return a, b

16 Pseudocode Helps Us Program Top-Down! RandomWalk(n, steps) x, y ß n/2, n/2 print (x, y) for i ß 0 to steps 1 x, y ß RandomStep(x, y, n) print (x, y) RandomStep(x, y, n) a ß x b ß y while (a, b) = (x, y) or (a, b) not in n x n field a ß x + RandomDelta() b ß y + RandomDelta() return a, b

17 Moving from Pseudocode to Go func RandomWalk(n, steps int) { x, y := n/2, n/2 fmt.println(x, y) for i := 0; i <= steps 1; i++ { x, y = RandomStep(x, y, n) fmt.println(x, y) RandomStep(x, y, n) a ß x b ß y while (a, b) = (x, y) or (a, b) not in n x n field a ß x + RandomDelta() //-1, 0, or 1 b ß y + RandomDelta() //-1, 0, or 1 return a, b

18 Moving from Pseudocode to Go func RandomWalk(n, steps int) { x, y := n/2, n/2 fmt.println(x, y) for i := 0; i <= steps 1; i++ { x, y = RandomStep(x, y, n) fmt.println(x, y) func RandomStep(x,y,n int) (a int, b int) { a, b = x, y for (a == x && b == y)!infield(a,b,n) { a = x + RandomDelta() b = y + RandomDelta() return a, b

19 Small Functions are Now Easy //Tests if point (x,y) is in n x n grid func InField(x, y, n int) bool { if x < 0 x > n y < 0 y > n { return false else { return true

20 Small Functions are Now Easy //Tests if point (x,y) is in n x n grid func InField(x, y, n int) bool { if x < 0 x > n y < 0 y > n { return false else { return true //Generates random integer from {-1, 0, 1 func RandomDelta() int { return (rand.intn(3))-1

21 func randdelta() int { return (rand.int() % 3) - 1 func infield(coord, n int) bool { return coord >= 0 && coord < n func randstep(x,y,n int) (nx int, ny int) { nx, ny = x, y for (nx == x && ny == y)!infield(nx,n)!infield(ny,n) { nx = x+randdelta() ny = y+randdelta() return func randomwalk(n, steps int) { var x, y = n/2, n/2 fmt.println(x,y) for i := 0; i < steps; i++ { x,y = randstep(x,y,n) fmt.println(x,y) Putting It All Together

22 Notes on Style

23 Comments on Commenting Use comments to document what a function does. // ReverseInteger(n) will return a new integer // formed by the decimal digits of n reversed. func ReverseInteger(n int) int { out := 0 for n!= 0 { out = 10*out + n % 10 n = n / 10 // note: integer division! return out

24 Comments on Commenting Code between /* and */ is also a comment (useful for commenting multiple lines). /* ReverseInteger(n) will return a new integer formed by the decimal digits of n reversed.*/ func ReverseInteger(n int) int { out := 0 for n!= 0 { out = 10*out + n % 10 n = n / 10 // note: integer division! return out

25 Code Formatting func Gauss(n int) int { var sum int = 0 for i:=1; i<=n; i++; { sum = sum + i return sum func Gauss(n int) int { var sum int = 0 for i:=1; i<=n; i++; { sum = sum + i return sum go fmt gauss.go will reformat your Go program using the preferred Go style, with all the correct indentations, etc. Reason #1000 to compile frequently: your program must be a correct Go program for this to work (it won t format code with syntax errors)

More Examples /

More Examples / More Examples 02-201 / 02-601 Debugging How to Debug Debugging is solving a murder mystery: you see the evidence of some failure & you try to figure out what part of the code caused it. It s based on backward

More information

Conditionals & Loops /

Conditionals & Loops / Conditionals & Loops 02-201 / 02-601 Conditionals If Statement if statements let you execute statements conditionally. true "then" part condition a > b false "else" part func max(a int, b int) int { var

More information

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous Assignment 3 Methods Review CSC 123 Fall 2018 Notes: All homework must be submitted via e-mail. All parts of assignment must be submitted in a single e-mail with multiple attachments when required. Notes:

More information

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations

Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations Tutorial 12 Craps Game Application: Introducing Random Number Generation and Enumerations Outline 12.1 Test-Driving the Craps Game Application 12.2 Random Number Generation 12.3 Using an enum in the Craps

More information

Arrays and Strings

Arrays and Strings Arrays and Strings 02-201 Arrays Recall: Fibonacci() and Arrays 1 1 2 3 5 8 13 21 34 55 a Fibonacci(n) a ß array of length n a[1] ß 1 a[2] ß 1 for i ß 3 to n a[i] ß a[i-1] + a[i-2] return a Declaring Arrays

More information

GO SHORT INTERVIEW QUESTIONS EXPLAINED IN COLOR

GO SHORT INTERVIEW QUESTIONS EXPLAINED IN COLOR GO SHORT INTERVIEW QUESTIONS EXPLAINED IN COLOR REVISION 1 HAWTHORNE-PRESS.COM Go Short Interview Questions Explained in Color Published by Hawthorne-Press.com 916 Adele Street Houston, Texas 77009, USA

More information

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++ Comp Sci 1570 Introduction to C++ Outline 1 Outline 1 Outline 1 switch ( e x p r e s s i o n ) { case c o n s t a n t 1 : group of statements 1; break ; case c o n s t a n t 2 : group of statements 2;

More information

Functions & Variables !

Functions & Variables ! Functions & Variables 02-201! What Is Programming? Programming is clearly, correctly telling a computer what to do. Programming Executable Program Algorithm: (English) instructions to the computer Programming

More information

C Functions. 5.2 Program Modules in C

C Functions. 5.2 Program Modules in C 1 5 C Functions 5.2 Program Modules in C 2 Functions Modules in C Programs combine user-defined functions with library functions - C standard library has a wide variety of functions Function calls Invoking

More information

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single

Functions in C++ Problem-Solving Procedure With Modular Design C ++ Function Definition: a single Functions in C++ Problem-Solving Procedure With Modular Design: Program development steps: Analyze the problem Develop a solution Code the solution Test/Debug the program C ++ Function Definition: A module

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

COMP 110 Programming Exercise: Simulation of the Game of Craps

COMP 110 Programming Exercise: Simulation of the Game of Craps COMP 110 Programming Exercise: Simulation of the Game of Craps Craps is a game of chance played by rolling two dice for a series of rolls and placing bets on the outcomes. The background on probability,

More information

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module.

6.S189 Homework 2. What to turn in. Exercise 3.1 Defining A Function. Exercise 3.2 Math Module. 6.S189 Homework 2 http://web.mit.edu/6.s189/www/materials.html What to turn in Checkoffs 3, 4 and 5 are due by 5 PM on Monday, January 15th. Checkoff 3 is over Exercises 3.1-3.2, Checkoff 4 is over Exercises

More information

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved. Functions In general, functions are blocks of code that perform a number of pre-defined commands to accomplish something productive. You can either use the built-in library functions or you can create

More information

Lecture 9: Lists. Lists store lists of variables. Declaring variables that hold lists. Carl Kingsford, , Fall 2015

Lecture 9: Lists. Lists store lists of variables. Declaring variables that hold lists. Carl Kingsford, , Fall 2015 Carl Kingsford, 0-0, Fall 0 Lecture : Lists Terminology: Go uses a non-standard term slice to refer to what we are calling lists. Others use the term array for the same concept. Unfortunately, Go uses

More information

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015

Classwork 7: Craps. N. Duong & R. Rodriguez, Java Crash Course January 6, 2015 Classwork 7: Craps N. Duong & R. Rodriguez, Java Crash Course January 6, 2015 For this classwork, you will be writing code for the game Craps. For those of you who do not know, Craps is a dice-rolling

More information

LAB: WHILE LOOPS IN C++

LAB: WHILE LOOPS IN C++ LAB: WHILE LOOPS IN C++ MODULE 2 JEFFREY A. STONE and TRICIA K. CLARK COPYRIGHT 2014 VERSION 4.0 PALMS MODULE 2 LAB: WHILE LOOPS IN C++ 2 Introduction This lab will provide students with an introduction

More information

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Functions. Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan. Functions Angela Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2009 Fall Outline 5.1 Introduction 5.3 Math Library Functions 5.4 Functions 5.5

More information

CPSC 427: Object-Oriented Programming

CPSC 427: Object-Oriented Programming CPSC 427: Object-Oriented Programming Michael J. Fischer Lecture 18 November 7, 2016 CPSC 427, Lecture 18 1/19 Demo: Craps Game Polymorphic Derivation (continued) Name Visibility CPSC 427, Lecture 18 2/19

More information

Functions and Recursion

Functions and Recursion Functions and Recursion 1 Outline Introduction Program Components in C++ Math Library Functions Functions Function Definitions Function Prototypes Header Files Random Number Generation Example: A Game

More information

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution

Learning Recursion. Recursion [ Why is it important?] ~7 easy marks in Exam Paper. Step 1. Understand Code. Step 2. Understand Execution Recursion [ Why is it important?] ~7 easy marks in Exam Paper Seemingly Different Coding Approach In Fact: Strengthen Top-down Thinking Get Mature in - Setting parameters - Function calls - return + work

More information

Text Input and Conditionals

Text Input and Conditionals Text Input and Conditionals Text Input Many programs allow the user to enter information, like a username and password. Python makes taking input from the user seamless with a single line of code: input()

More information

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson)

Dr M Kasim A Jalil. Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Lecture 9 Functions Dr M Kasim A Jalil Faculty of Mechanical Engineering UTM (source: Deitel Associates & Pearson) Objectives In this chapter, you will learn: To understand how to construct programs modularly

More information

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times

12/22/11. } Rolling a Six-Sided Die. } Fig 6.7: Rolling a Six-Sided Die 6,000,000 Times } Rolling a Six-Sided Die face = 1 + randomnumbers.nextint( 6 ); The argument 6 called the scaling factor represents the number of unique values that nextint should produce (0 5) This is called scaling

More information

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18 Assignment Lecture 9 Logical Operations Formatted Print Printf Increment and decrement Read through 3.9, 3.10 Read 4.1. 4.2, 4.3 Go through checkpoint exercise 4.1 Logical Operations - Motivation Logical

More information

Chapter Four: Loops II

Chapter Four: Loops II Chapter Four: Loops II Slides by Evan Gallagher & Nikolay Kirov Chapter Goals To understand nested loops To implement programs that read and process data sets To use a computer for simulations Processing

More information

Conditionals !

Conditionals ! Conditionals 02-201! Computing GCD GCD Problem: Compute the greatest common divisor of two integers. Input: Two integers a and b. Output: The greatest common divisor of a and b. Exercise: Design an algorithm

More information

Lecture 04 FUNCTIONS AND ARRAYS

Lecture 04 FUNCTIONS AND ARRAYS Lecture 04 FUNCTIONS AND ARRAYS 1 Motivations Divide hug tasks to blocks: divide programs up into sets of cooperating functions. Define new functions with function calls and parameter passing. Use functions

More information

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132)

BoredGames Language Reference Manual A Language for Board Games. Brandon Kessler (bpk2107) and Kristen Wise (kew2132) BoredGames Language Reference Manual A Language for Board Games Brandon Kessler (bpk2107) and Kristen Wise (kew2132) 1 Table of Contents 1. Introduction... 4 2. Lexical Conventions... 4 2.A Comments...

More information

WELCOME! (download slides and.py files and follow along!) LECTURE 1

WELCOME! (download slides and.py files and follow along!) LECTURE 1 WELCOME! (download slides and.py files and follow along!) 6.0001 LECTURE 1 6.0001 LECTURE 1 1 TODAY course info what is computation python basics mathematical operations python variables and types NOTE:

More information

Function Call Stack and Activation Records

Function Call Stack and Activation Records 71 Function Call Stack and Activation Records To understand how C performs function calls, we first need to consider a data structure (i.e., collection of related data items) known as a stack. Students

More information

cs1114 REVIEW of details test closed laptop period

cs1114 REVIEW of details test closed laptop period python details DOES NOT COVER FUNCTIONS!!! This is a sample of some of the things that you are responsible for do not believe that if you know only the things on this test that they will get an A on any

More information

From Last Time... Given a bacterial genome (~3 Mbp), where is ori?

From Last Time... Given a bacterial genome (~3 Mbp), where is ori? From Last Time... Given a bacterial genome (~3 Mbp), where is ori? Given ori (~500 bp), what is the hidden message saying that replication should start here? From Last Time... Frequent Words Problem. Find

More information

BOREDGAMES Language for Board Games

BOREDGAMES Language for Board Games BOREDGAMES Language for Board Games I. Team: Rujuta Karkhanis (rnk2112) Brandon Kessler (bpk2107) Kristen Wise (kew2132) II. Language Description: BoredGames is a language designed for easy implementation

More information

Programming Assignment #4 Arrays and Pointers

Programming Assignment #4 Arrays and Pointers CS-2301, System Programming for Non-majors, B-term 2013 Project 4 (30 points) Assigned: Tuesday, November 19, 2013 Due: Tuesday, November 26, Noon Abstract Programming Assignment #4 Arrays and Pointers

More information

Introduction to Computer Science Unit 4B. Programs: Classes and Objects

Introduction to Computer Science Unit 4B. Programs: Classes and Objects Introduction to Computer Science Unit 4B. Programs: Classes and Objects This section must be updated to work with repl.it 1. Copy the Box class and compile it. But you won t be able to run it because it

More information

Random Numbers. Chapter 14

Random Numbers. Chapter 14 B B Chapter 14 Random Numbers Many events in our lives are random. When you enter a full parking lot, for example, you drive around until someone pulls out of a parking space that you can claim. The event

More information

Random Walks and Defining Functions FEB 9 AND 11, 2015

Random Walks and Defining Functions FEB 9 AND 11, 2015 Random Walks and Defining Functions FEB 9 AND 11, 2015 If we take a random walk, will we go places? Problem: Simulate a random walk in which a person starts of at point 0 and at each step randomly picks

More information

More on Strings & Arrays

More on Strings & Arrays More on Strings & Arrays 02-201 Indexing Strings Strings work like arrays in some ways: Strings have fixed length. You can find the length of string s with len(s). You can access elements of string s with

More information

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17 List of Programs xxv List of Figures xxix List of Tables xxxiii Preface to second version xxxv PART 1 Structured Programming 1 1 Getting started 3 1.1 Programming 3 1.2 Editing source code 5 Source code

More information

CSci 1113 Lab Exercise 5 (Week 6): Reference Parameters and Basic File I/O

CSci 1113 Lab Exercise 5 (Week 6): Reference Parameters and Basic File I/O CSci 1113 Lab Exercise 5 (Week 6): Reference Parameters and Basic File I/O So far, we've been getting our program input from the console keyboard using cin and displaying results on the terminal display

More information

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

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved Chapter Four: Loops Slides by Evan Gallagher The Three Loops in C++ C++ has these three looping statements: while for do The while Loop while (condition) { statements } The condition is some kind of test

More information

CSE123. Program Design and Modular Programming Functions 1-1

CSE123. Program Design and Modular Programming Functions 1-1 CSE123 Program Design and Modular Programming Functions 1-1 5.1 Introduction A function in C is a small sub-program performs a particular task, supports the concept of modular programming design techniques.

More information

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section: 7 Arrays Now go, write it before them in a table, and note it in a book. Isaiah 30:8 To go beyond is as wrong as to fall short. Confucius Begin at the beginning, and go on till you come to the end: then

More information

CSI32 Object-Oriented Programming

CSI32 Object-Oriented Programming Outline Department of Mathematics and Computer Science Bronx Community College February 2, 2015 Outline Outline 1 Chapter 1 Cornerstones of Computing Textbook Object-Oriented Programming in Python Goldwasser

More information

UNIT 9A Randomness in Computation: Random Number Generators

UNIT 9A Randomness in Computation: Random Number Generators UNIT 9A Randomness in Computation: Random Number Generators 1 Last Unit Computer organization: what s under the hood 3 This Unit Random number generation Using pseudorandom numbers 4 Overview The concept

More information

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017 Control Structures Lecture 4 COP 3014 Fall 2017 September 18, 2017 Control Flow Control flow refers to the specification of the order in which the individual statements, instructions or function calls

More information

Basics of Programming with Python

Basics of Programming with Python Basics of Programming with Python A gentle guide to writing simple programs Robert Montante 1 Topics Part 3 Obtaining Python Interactive use Variables Programs in files Data types Decision-making Functions

More information

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 4 Certificate in IT SOFTWARE DEVELOPMENT

BCS THE CHARTERED INSTITUTE FOR IT. BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 4 Certificate in IT SOFTWARE DEVELOPMENT BCS THE CHARTERED INSTITUTE FOR IT BCS HIGHER EDUCATION QUALIFICATIONS BCS Level 4 Certificate in IT SOFTWARE DEVELOPMENT Wednesday 25th March 2015 - Afternoon Time: TWO hours Section A and Section B each

More information

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6 1. Python is a calculator. A variable is a container Introduction to Python and Programming BBM 101 - Introduction to Programming I Hacettepe University Fall 016 Fuat Akal, Aykut Erdem, Erkut Erdem 3.

More information

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++

Chapter 3 - Functions. Chapter 3 - Functions. 3.1 Introduction. 3.2 Program Components in C++ Chapter 3 - Functions 1 Chapter 3 - Functions 2 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3. Functions 3.5 Function Definitions 3.6 Function Prototypes 3. Header Files 3.8

More information

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode STUDENT OUTLINE Lesson 8: Structured Programming, Control Structures, if- Statements, Pseudocode INTRODUCTION: This lesson is the first of four covering the standard control structures of a high-level

More information

CIS 194: Homework 11. Due Thursday, April 5. Risk. The Rand StdGen monad

CIS 194: Homework 11. Due Thursday, April 5. Risk. The Rand StdGen monad CIS 194: Homework 11 Due Thursday, April 5 Files you should submit: Risk.hs. You should take the version we have provided and add your solutions to it. Risk The game of Risk involves two or more players,

More information

Arrays/Slices Store Lists of Variables

Arrays/Slices Store Lists of Variables Maps 02-201 Arrays/Slices Store Lists of Variables H i T h e r e! 0 1 2 3 4 5 6 7 8 1 1 2 3 5 8 13 21 34 55 89 0 1 2 3 4 5 6 7 8 9 10 ACG TTA GAG CCT TAA GGG CAT 0 1 2 3 4 5 6 What if Indices Aren t Integers?

More information

EAS230: Programming for Engineers Lab 1 Fall 2004

EAS230: Programming for Engineers Lab 1 Fall 2004 Lab1: Introduction Visual C++ Objective The objective of this lab is to teach students: To work with the Microsoft Visual C++ 6.0 environment (referred to as VC++). C++ program structure and basic input

More information

More about Loops and Decisions

More about Loops and Decisions More about Loops and Decisions 5 In this chapter, we continue to explore the topic of repetition structures. We will discuss how loops are used in conjunction with the other control structures sequence

More information

Protocols and Delegates. Dr. Sarah Abraham

Protocols and Delegates. Dr. Sarah Abraham Protocols and Delegates Dr. Sarah Abraham University of Texas at Austin CS329e Fall 2016 Protocols Group of related properties and methods that can be implemented by any class Independent of any class

More information

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. 1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol. B. Outputs to the console a floating point number f1 in scientific format

More information

Java Outline (Upto Exam 2)

Java Outline (Upto Exam 2) Java Outline (Upto Exam 2) Part 4 IF s (Branches) and Loops Chapter 12/13 (The if Statement) Hand in Program Assignment#1 (12 marks): Create a program called Ifs that will do the following: 1. Ask the

More information

Python Games. Session 1 By Declan Fox

Python Games. Session 1 By Declan Fox Python Games Session 1 By Declan Fox Rules General Information Wi-Fi Name: CoderDojo Password: coderdojowireless Website: http://cdathenry.wordpress.com/ Plans for this year Command line interface at first

More information

IT 374 C# and Applications/ IT695 C# Data Structures

IT 374 C# and Applications/ IT695 C# Data Structures IT 374 C# and Applications/ IT695 C# Data Structures Module 2.5: Methods A Deeper Look Xianrong (Shawn) Zheng Spring 2017 1 Outline static Methods, static Variables, and Class Math Methods with Multiple

More information

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

Using the Command Line

Using the Command Line 1 Unit 15 Debugging COMPILATION 2 3 Using the Command Line While it has a GUI interface like your Mac or Windows PC much of its power lies in its rich set of utilities that are most easily run at the command

More information

Introduction to Java Programs for Packet #4: Classes and Objects

Introduction to Java Programs for Packet #4: Classes and Objects Introduction to Java Programs for Packet #4: Classes and Objects Note. All of these programs involve writing and using more than one class file. 1. Copy the Box class and compile it. But you won t be able

More information

Add Subtract Multiply Divide

Add Subtract Multiply Divide ARITHMETIC OPERATORS if AND if/else AND while LOOP Order of Operation (Precedence Part 1) Copyright 2014 Dan McElroy Add Subtract Multiply Divide + Add - Subtract * Multiply / Divide = gives the quotient

More information

BRANCHING if-else statements

BRANCHING if-else statements BRANCHING if-else statements Conditional Statements A conditional statement lets us choose which statement t t will be executed next Therefore they are sometimes called selection statements Conditional

More information

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017

Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017 Lecture 3 (02/06, 02/08): Condition Statements Decision, Operations & Information Technologies Robert H. Smith School of Business Spring, 2017 K. Zhang BMGT 404 The modulus operator It works on integers

More information

Part III Appendices 165

Part III Appendices 165 Part III Appendices 165 Appendix A Technical Instructions Learning Outcomes This material will help you learn how to use the software you need to do your work in this course. You won t be tested on it.

More information

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands Introduction Operators are the symbols which operates on value or a variable. It tells the compiler to perform certain mathematical or logical manipulations. Can be of following categories: Unary requires

More information

Week 4 Lecture 1. Expressions and Functions

Week 4 Lecture 1. Expressions and Functions Lecture 1 Expressions and Functions Expressions A representation of a value Expressions have a type Expressions have a value Examples 1 + 2: type int; value 3 1.2 + 3: type float; value 4.2 2 More expression

More information

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2 Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2 Ziad Matni Dept. of Computer Science, UCSB Administrative This class is currently FULL and

More information

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators Objectives Chapter 4: Control Structures I (Selection) In this chapter, you will: Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

More information

15-110: Principles of Computing, Spring 2018

15-110: Principles of Computing, Spring 2018 5-: Principles of Computing, Spring 28 Problem Set 8 (PS8) Due: Friday, March 3 by 2:3PM via Gradescope Hand-in HANDIN INSTRUCTIONS Download a copy of this PDF file. You have two ways to fill in your answers:.

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 Outline 3.1 Introduction 3.2 Progra m Components in C++ 3.3 Ma th Libra ry Func tions 3.4 Func tions 3.5 Func tion De finitions 3.6 Func tion Prototypes 3.7 He a de r File s 3.8

More information

COMP 202 Java in one week

COMP 202 Java in one week COMP 202 Java in one week... Continued CONTENTS: Return to material from previous lecture At-home programming exercises Please Do Ask Questions It's perfectly normal not to understand everything Most of

More information

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use?

Algorithm Discovery and Design. Why are Algorithms Important? Representing Algorithms. Chapter 2 Topics: What language to use? Algorithm Discovery and Design Chapter 2 Topics: Representing Algorithms Algorithmic Problem Solving CMPUT101 Introduction to Computing (c) Yngvi Bjornsson & Jia You 1 Why are Algorithms Important? If

More information

Introduction to. Copyright HKTA Tang Hin Memorial Secondary School 2016

Introduction to. Copyright HKTA Tang Hin Memorial Secondary School 2016 Introduction to 2 VISUAL BASIC 2016 edition Copyright HKTA Tang Hin Memorial Secondary School 2016 Table of Contents. Chapter....... 1.... More..... on.. Operators........................................

More information

Chapter 3 - Functions

Chapter 3 - Functions Chapter 3 - Functions 1 3.1 Introduction 3.2 Program Components in C++ 3.3 Math Library Functions 3.4 Functions 3.5 Function Definitions 3.6 Function Prototypes 3.7 Header Files 3.8 Random Number Generation

More information

Introduction to Computer Science Unit 3. Programs

Introduction to Computer Science Unit 3. Programs Introduction to Computer Science Unit 3. Programs This section must be updated to work with repl.it Programs 1 to 4 require you to use the mod, %, operator. 1. Let the user enter an integer. Your program

More information

KAREL UNIT 5 STUDENT JOURNAL REVISED APRIL 19, 2017 NAME SCHOOL, CLASS, PERIOD

KAREL UNIT 5 STUDENT JOURNAL REVISED APRIL 19, 2017 NAME SCHOOL, CLASS, PERIOD KAREL UNIT 5 STUDENT JOURNAL REVISED APRIL 19, 2017 NAME DATE STARTED DATE COMPLETED SCHOOL, CLASS, PERIOD Copyright 2016, 2017 NCLab Inc. 2 3 TABLE OF CONTENTS: WELCOME TO YOUR JOURNAL 4 SECTION 21: PROBABILITY

More information

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d. Chapter 4: Control Structures I (Selection) In this chapter, you will: Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate logical (Boolean)

More information

CS 251 Intermediate Programming Java Basics

CS 251 Intermediate Programming Java Basics CS 251 Intermediate Programming Java Basics Brooke Chenoweth University of New Mexico Spring 2018 Prerequisites These are the topics that I assume that you have already seen: Variables Boolean expressions

More information

Functions & Variables /

Functions & Variables / Functions & Variables 02-201 / 02-601 Functions Functions in calculus give a rule for mapping input values to an output: f : R R May take multiple inputs: g : R R R 11 10 9 8 7 6 5-1.0-0.5 0.5 1.0 x f(x)

More information

Creating a C++ Program

Creating a C++ Program Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer. 1 Creating a C++ Program created using an

More information

3 The L oop Control Structure

3 The L oop Control Structure 3 The L oop Control Structure Loops The while Loop Tips and Traps More Operators The for Loop Nesting of Loops Multiple Initialisations in the for Loop The Odd Loop The break Statement The continue Statement

More information

5. What is a block statement? A block statement is a segment of code between {}.

5. What is a block statement? A block statement is a segment of code between {}. COSC 117 Exam 1 Key Fall 2012 Part 1: Definitions & Short Answer (3 Points Each) 1. What does CPU stand for? Central Processing Unit 2. Explain the difference between high-level languages and machine language.

More information

Fundamentals of Programming Session 2

Fundamentals of Programming Session 2 Fundamentals of Programming Session 2 Instructor: Reza Entezari-Maleki Email: entezari@ce.sharif.edu 1 Fall 2013 Sharif University of Technology Outlines Programming Language Binary numbers Addition Subtraction

More information

Flow Control. CSC215 Lecture

Flow Control. CSC215 Lecture Flow Control CSC215 Lecture Outline Blocks and compound statements Conditional statements if - statement if-else - statement switch - statement? : opertator Nested conditional statements Repetitive statements

More information

(Python) Chapter 3: Repetition

(Python) Chapter 3: Repetition (Python) Chapter 3: Repetition 3.1 while loop Motivation Using our current set of tools, repeating a simple statement many times is tedious. The only item we can currently repeat easily is printing the

More information

Control Structures: The IF statement!

Control Structures: The IF statement! Control Structures: The IF statement! 1E3! Topic 5! 5 IF 1 Objectives! n To learn when and how to use an IF statement.! n To be able to form Boolean (logical) expressions using relational operators! n

More information

Issue with Implementing PrimeSieve() in Go

Issue with Implementing PrimeSieve() in Go Slices 02-201 Issue with Implementing PrimeSieve() in Go func PrimeSieve(n int) [n+1]bool { var iscomposite [n+1]bool //ERROR! biggestprime := 2 for biggestprime < n for i:=2; i

More information

Common Loop Algorithms 9/21/16 42

Common Loop Algorithms 9/21/16 42 Common Loop Algorithms 9/21/16 42 Common Loop Algorithms 1. Sum and Average Value 2. Coun4ng Matches 3. Promp4ng un4l a Match Is Found 4. Maximum and Minimum 5. Comparing Adjacent Values 9/21/16 43 Sum

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

ProgrammingAssignment #5: Let s Play Craps!

ProgrammingAssignment #5: Let s Play Craps! ProgrammingAssignment #5: Let s Play Craps! Due Date: April 6, 1998 1 The Problem Craps is a game played with a pair of dice. In the game of craps, the shooter (the player with the dice) rolls a pair of

More information

5. Control Statements

5. Control Statements 5. Control Statements This section of the course will introduce you to the major control statements in C++. These control statements are used to specify the branching in an algorithm/recipe. Control statements

More information

Conditionals and Recursion. Python Part 4

Conditionals and Recursion. Python Part 4 Conditionals and Recursion Python Part 4 Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print (remainder) 1 Boolean expressions An expression that

More information

Chapter 2. C++ Basics

Chapter 2. C++ Basics Chapter 2 C++ Basics Overview 2.1 Variables and Assignments 2.2 Input and Output 2.3 Data Types and Expressions 2.4 Simple Flow of Control 2.5 Program Style Slide 2-2 2.1 Variables and Assignments Variables

More information

4. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

4. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++ 162 Our Goal 163 4. Logical Values Boolean Functions; the Type bool; logical and relational operators; shortcut evaluation int a; std::cin >> a; if (a % 2 == 0) std::cout

More information

CSC 1351: Quiz 6: Sort and Search

CSC 1351: Quiz 6: Sort and Search CSC 1351: Quiz 6: Sort and Search Name: 0.1 You want to implement combat within a role playing game on a computer. Specifically, the game rules for damage inflicted by a hit are: In order to figure out

More information

Haskell An Introduction

Haskell An Introduction Haskell An Introduction What is Haskell? General purpose Purely functional No function can have side-effects IO is done using special types Lazy Strongly typed Polymorphic types Concise and elegant A First

More information