Matlab tutorial course Exercises 4:. Exercises – for loops Download the scripts loops_example.m and if_else_example.m, run the code Download the function,

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

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.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
This game is loosely based on the Whack-A- Mole arcade game.  Each game starts with 45 seconds of play.  Moles randomly pop out of holes on the landscape.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
6. More on the For-Loop Using the Count Variable Developing For-Loop Solutions.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 12 – Craps Game Application: Introducing Random.
1 Lab Session-6 CSIT-121 Spring 2005 Structured Choice The do~While Loop Lab Exercises.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
Matlab tutorial course Exercises 1:. Exercises Go back to my webpage, download the file ‘startup.m’ and save it in your MATLAB home folder – Must be directly.
Matlab tutorial course Exercises 2:. Exercises Copy the script ‘face_points.m’ from my webpage into your ‘scripts’ folder Create a new folder in your.
THE BIG PICTURE. How does JavaScript interact with the browser?
MOM! Phineas and Ferb are … Aims:
Bill's Amazing Content Rotator jQuery Content Rotator.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
 Make sure you are subscribed to announcements on Moodle.  Activity 4 will be due 48hrs after your lab ends.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 18 - WMLScript: Control Structures II Outline 18.1 Introduction 18.2 Essentials of Counter-Controlled.
How to start Visual Studio 2008 or 2010 (command-line program)
Understanding Randomness
MIPS coding. slt, slti slt $t3, $t1, $t2 – set $t3 to be 1 if $t1 < $t2 ; else clear $t3 to be 0. – “Set Less Than.” slti $t3, $t1, 100 – set $t3 to be.
Synthesis ENGR 1181 MATLAB 11. Topics  No new material  Covers topics that will be on the Midterm 2 Exam MATLAB 01 – Program Design MATLAB 02 – Introduction.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.
Piñata Game: Keeping Score in Alice By Maggie Bashford Professor Susan Rodger Duke University July
 There are times when you will want blocks to repeat. Instead of duplicating blocks and ending up with a long script that might be confusing, there.
read and learn from example loop programs develop modular program
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Chapter 6 Methods: A Deeper Look. Objectives In this chapter you will learn: How static methods and fields are associated with an entire class rather.
Exercise 1: IF/ELSE Step 1: Open NotePad++ and create “number.php” in your webspace Step 2: Write codes to do the following 1.Generate a random number.
1 A Balanced Introduction to Computer Science, 2/E David Reed, Creighton University ©2008 Pearson Prentice Hall ISBN Chapter 13 Conditional.
Matlab tutorial course Lesson 4: Writing your own functions: programming constructs
C++ LANGUAGE TUTORIAL LESSON 1 –WRITING YOUR FIRST PROGRAM.
Boat Racing Game Challenge #2 By Chris Brown Under the direction of Professor Susan Rodger Duke University, January 2013 Based off of the Boat Racing Game.
Matlab tutorial course Exercises 5: Loading and writing images
Cumulative algorithms. 2 Adding many numbers How would you find the sum of all integers from ? // This may require a lot of typing int sum = 1 +
 2000 Prentice Hall, Inc. All rights reserved. 5.2Program Modules in C Functions –Modules in C –Programs combine user-defined functions with library functions.
A Balanced Introduction to Computer Science, 3/E David Reed, Creighton University ©2011 Pearson Prentice Hall ISBN Chapter 13 Conditional.
The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development Lecture 2 Debugging,
Computer Science I: Understand how to evaluate expressions with DIV and MOD Random Numbers Reading random code Writing random code Odds/evens/…
int [] scores = new int [10];
More on Logic Today we look at the for loop and then put all of this together to look at some more complex forms of logic that a program will need The.
IST 210: PHP LOGIC IST 210: Organization of Data IST210 1.
AP Java Java’s version of Repeat until.
1 BUILDING JAVA PROGRAMS CHAPTER 5 PROGRAM LOGIC AND INDEFINITE LOOPS.
Objective of the lesson Use Blockly to make a dice for Snakes and Ladders All of you will: – Make an image which displays when you press a button Most.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
This is a while loop. The code is done while the condition is true. The code that is done is enclosed in { }. Note that this program has three parts: Housekeeping.
Section 5.5 Application: The Card Game of War. 5.5 Application: The Card Game of War A deck of cards is shuffled and dealt to two players The players.
Modelling and Simulating Social Systems with MATLAB
Modelling and Simulating Social Systems with MATLAB
Diamond Hunt Mock Programming Project.
Introducing Do While & Do Until Loops & Repetition Statements
The while Looping Structure
Matlab tutorial course
Matlab tutorial course
DICES ROLLING Sample This is an example text. Go ahead an replace it with your own text. This is an example text.
Truth tables: Ways to organize results of Boolean expressions.
Computer Science I: Get out your notes.
How to allow the program to know when to stop a loop.
GCSE Computing.
Matlab tutorial course
Presentation transcript:

Matlab tutorial course Exercises 4:

Exercises – for loops Download the scripts loops_example.m and if_else_example.m, run the code Download the function, roll_a_dice.m, this simulates rolling a dice to return an integer between 1 and 6 Write function to show the result of rolling a dice 5 times. When your functions runs it should produce a result like: Roll 1, you scored 3 Roll 2, you scored 1 etc… Change the function so it has an input that specifies the number of rolls Hint: use the examples in the scripts to help you. Bart may also provide some assistance!

Exercises – while loops Now write a function using a while loop, to simulate rolling a dice until you score a 6. The output of your function should look something like: Roll 1, you scored 5 Roll 2, you scored 6 Modify the function so it counts the number of rolls it took to win – Add code to tell the player they have won Congratulations, you won after 2 rolls!! – Return the number of rolls as output to the function

Exercises – while loops and if/else Now write a new function, that specifies a maximum number of rolls as input. If the players scores a 6 they win as before. However, if after the n-th roll they still haven’t rolled a 6, then they lose The final display of your output should either be Congratulations, you won after 2 rolls!! – Or I'm sorry, you used up 5 rolls without scoring a 6. You lose!! Hint you will need to use if/else statements When you have finished, download all the rolling dice functions, these provide my sample answers and will be useful for you to keep