L6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.

Slides:



Advertisements
Similar presentations
Chapter 5: Control Structures II (Repetition)
Advertisements

Insight Through Computing 7. The While-Loop For-Loop Problems Introduce While-Loops.
Clear your desk for your quiz. Unit 2 Day 8 Expected Value Average expectation per game if the game is played many times Can be used to evaluate and.
Monte Carlo Simulation of the Craps Dice Game Sencer Koç.
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
Multiplication Rule. A tree structure is a useful tool for keeping systematic track of all possibilities in situations in which events happen in order.
James Tam Loops In Python In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Functions.
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Insight Through Computing More on Conditionals Nested if’s Multiple Alternatives Top-Down Design.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Designing Algorithms Csci 107 Lecture 3. Designing algorithms Last time –Pseudocode –Algorithm: computing the sum 1+2+…+n –Gauss formula for 1+2+…+n Today.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
James Tam Loops In Pascal In this section of notes you will learn how to rerun parts of your program without having to duplicate the code.
Computer Science 1620 Programming & Problem Solving.
Insight Through Computing 8. More Practice with Iteration and Conditionals Through Graphics For-Loop Problems Introduce While-Loops.
Recursion Alice. Repetition In some situations, we don’t know exactly how many times a block of instructions should be repeated. All we know is that repetition.
Week 11 Recap CSE 115 Spring Want to write a programming language? You’ll need three things: You’ll need three things: –Sequencing –Selection Polymorphism.
Reinforcement Learning Game playing: So far, we have told the agent the value of a given board position. How can agent learn which positions are important?
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
 The Law of Large Numbers – Read the preface to Chapter 7 on page 388 and be prepared to summarize the Law of Large Numbers.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
Introduction to Problem SolvingS1.2.1 Bina © 1998 Liran & Ofir Introduction to Problem Solving Programming in C.
8.1 Sequences and Series Essential Questions: How do we use sequence notation to write the terms of a sequence? How do we use factorial notation? How.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Looping Exercises Deciding Which Loop to Use At this.
Data Structures & AlgorithmsIT 0501 Algorithm Analysis I.
MATLAB® While Loops.
AP Statistics: Section 8.2 Geometric Probability.
Probability (Grade 12) Daljit Dhaliwal. Sticks and Stones game.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
P. STATISTICS LESSON 8.2 ( DAY 1 )
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
1 Programming with MATLAB ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
1 CSCI N201 Programming Concepts and Database 9 – Loops Lingma Acheson Department of Computer and Information Science, IUPUI.
Chapter 5: Control Structures II
7.2 Means and Variances of Random Variables.  Calculate the mean and standard deviation of random variables  Understand the law of large numbers.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
Lesson 9-3 Example Solve. GEOMETRY The perimeter of a trapezoid is the sum of the length of its sides. One side length is 16 inches. One side length.
Lecture 5: Stopping with a Sentinel. Using a Sentinel Problem Develop a class-averaging program that will process an arbitrary number of grades each time.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
A LOTTERY GAME Assignment P2 IST Guideline Use the PHP you learnt in class to make a PHP page Game rules:
Overview Go over parts of quiz? Another iteration structure for loop.
Introduction to Discrete Probability Epp, section 6.x CS 202.
 In this chapter you will learn about:  Introduction to Problem Solving  Software development method (SDM)  Specification of needs  Problem analysis.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
CSCI/CMPE 4341 Topic: Programming in Python Chapter 4: Control Structures (Part 2) Xiang Lian The University of Texas – Pan American Edinburg, TX
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
CS 101 – Oct. 7 Solving simple problems: create algorithm Structure of solution –Sequence of steps (1,2,3….) –Sometimes we need to make a choice –Sometimes.
CMSC 104, Section 301, Fall Lecture 18, 11/11/02 Functions, Part 1 of 3 Topics Using Predefined Functions Programmer-Defined Functions Using Input.
Computer Programming 12 Lesson 6 – Loop structure By: Dan Lunney.
Beginning Programming for Engineers Matlab Conditional Computation.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 3 - Structured Program Development Outline.
26/06/ Iteration Loops For … To … Next. 226/06/2016 Learning Objectives Define a program loop. State when a loop will end. State when the For.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Modelling and Simulating Social Systems with MATLAB
Recursion Alice.
Recursion Alice.
MSIS 655 Advanced Business Applications Programming
Types of Flow of Control
Chapter 3 – Control Structures
3.1 Iteration Loops For … To … Next 18/01/2019.
Class 4: Repetition Pretest Posttest Counting Flowchart these!
Output Variables {true} S {i = j} i := j; or j := i;
EPSII 59:006 Spring 2004.
Nested if’s Multiple Alternatives
Presentation transcript:

L6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions

First = input(‘Enter first year’); Last = input(‘Enter last year’); for y = First:Last fprintf(‘%5d\n’,y) end A Year-Printing Fragment

How It Works for y = First:Last fprintf(‘%5d\n’,y)) end Suppose First is 1999 and Last is First 2001 Last y

How It Works for y = First:Last fprintf(‘%5d\n’,y) end We enter the for-loop and y is initialized 1999 First 2001 Last 1999 y

How It Works for y = First:Last fprintf(‘%5d\n’,y)) end Is the value in y <= than the value in Last? 1999 First 2001 Last 1999 y

How It Works for y = First:Last fprintf(‘%5d\n’,y) end Yes. Execute the loop body and increment y First 2001 Last 2000 y 1999 Output

How It Works for y = First:Last fprintf(‘%5d\n’,y) end Is the value in y <= the value in Last? 1999 First 2001 Last 2000 y 1999 Output

How It Works for y = First:Last fprintf(‘%5d\n’,y) end Yes. Execute the loop body and increment y First 2001 Last 2001 y Output

How It Works for y = First:Last fprintf(‘%5d\n’,y) end Is the value in y <= the value in Last? 1999 First 2001 Last 2001 y Output

How It Works for y = First:Last fprintf(‘%5d\n’,y) end Yes. Execute the loop body and increment y First 2001 Last 2002 y Output

How It Works for y = First:Last fprintf(‘%5d\n’,y) end Is the value in y <= the value in Last? 1999 First 2001 Last 2002 y Output

How It Works for y = First:Last fprintf(‘%5d\n’,y) end No. The loop is finished First 2001 Last 2002 y Output

Problem Solving With the For-Loop The calculation to be repeated. end for count variable = expression for starting value : expression for ending value :

Developing For-Loop Solutions Illustrate the thinking associated with the design of for-loops The methodology of stepwise refinement. An example..

A Game: TriStick Pick three sticks each having a random length between zero and one. You win if you can form a triangle whose sides are the sticks. Otherwise you lose.

Win: Lose:

Problem Estimate the probability of winning a game of TriStick by simulating a million games and counting the number of wins.

Pseudocode Initialize running sum variable. Repeat 1,000,000 times: Play a game of TriStick by picking the three sticks. If you win increment the running sum Estimate the probability of winning

Refine… % Initialize running sum variable. Wins = 0; for n = 1: Play the nth game of TriStick by picking the three sticks. If you win increment the running sum. end % Estimate the prob of winning p = wins/

Refine the Loop Body Play the nth game of TriStick by picking the three sticks. If you win increment the running sum.

Refine the Loop Body % Play the nth game of TriStick % by picking the three sticks. a = rand; b = rand; c = rand; if (a<b+c) && (b<a+c) && (c<a+b) % No stick is longer than the % sum of the other two. wins = wins+1; end

Key Problem-Solving Strategy Progress from pseudocode to Matlab through a sequence of refinements. Comments have an essential role during the transitions. They “stay on” all the way to the finished fragment.