Loop Applications and Review CS 103 February 27, 2004.

Slides:



Advertisements
Similar presentations
Etter/Ingber Arrays and Matrices. Etter/Ingber One-Dimensional Arrays 4 An array is an indexed data structure 4 All variables stored in an array are of.
Advertisements

Introduction to C Programming
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Recursion CS 367 – Introduction to Data Structures.
Chapter Chapter 4. Think back to any very difficult quantitative problem that you had to solve in some science class How long did it take? How many times.
CMPS 1371 Introduction to Computing for Engineers
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Lists Introduction to Computing Science and Programming I.
ITC 240: Web Application Programming
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS1061: C Programming Lecture 23: Review; Examination Details A. O’Riordan, 2005.
Chapter 2: Algorithm Discovery and Design
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Loops – While, Do, For Repetition Statements Introduction to Arrays
To remind us We finished the last day by introducing If statements Their structure was:::::::::
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
General Computer Science for Engineers CISC 106 Lecture 09 James Atlas Computer and Information Sciences 9/25/2009.
An Object-Oriented Approach to Programming Logic and Design Chapter 7 Arrays.
Chapter 2: Algorithm Discovery and Design
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
Lists in Python.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science, C++ Version, Third Edition.
Invitation to Computer Science, Java Version, Second Edition.
PH24010 Data Handling and Statistics Photon Scattering program written in MathCAD.
COMP 116: Introduction to Scientific Programming Lecture 28: Midterm #2 Review.
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Array Cs212: DataStructures Lab 2. Array Group of contiguous memory locations Each memory location has same name Each memory location has same type a.
Loops CS 103 February 19, 2007 Presented by Nate.
1 7.Algorithm Efficiency What to measure? Space utilization: amount of memory required  Time efficiency: amount of time required to process the data.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
ITC Research Computing Support Using Matlab Effectively By: Ed Hall Research Computing Support Center Phone: Φ Fax:
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
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.
Programming with Loops. When to Use a Loop  Whenever you have a repeated set of actions, you should consider using a loop.  For example, if you have.
Loops CS 103 February 13, 2009 Author: Nate Hamm.
ITEC 109 Lecture 11 While loops. while loops Review Choices –1 st –2 nd to ?th –Last What happens if you only use ifs? Can you have just an else by itself?
Susie’s lecture notes are in the presenter’s notes, below the slides Disclaimer: Susie may have made errors in transcription or understanding. If there.
Counting Loops.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
Loops CS 103 February 23, Review FOR is a control construct that provides a loop for ii = 1:10 fprintf(‘%d \n’, ii) end FOR is a control construct.
Arrays. Topics to be Covered... Arrays ◦ Declaration ◦ Assigning values ◦ Array manipulation using loops Multi-dimensional arrays ◦ 2D arrays ◦ Declaration.
CSD 340 (Blum)1 For Loops See Beginning JavaScript (Paul Wilton) p. 87.
Chapter 2: Algorithm Discovery and Design Invitation to Computer Science.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Learning Javascript From Mr Saem
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
Flow control. Conditionals if condition do this stuff end if condition do this stuff else do this stuff end if condition do this stuff elseif condition.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
Discussion 4 eecs 183 Hannah Westra.
Loops BIS1523 – Lecture 10.
Repetition Structures Chapter 9
CS1371 Introduction to Computing for Engineers
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Recursion Data Structures.
Presentation transcript:

Loop Applications and Review CS 103 February 27, 2004

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Before the loop, the index, ii, doesn’t exist

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end First time through, ii = 1

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end First time through, ii = 1 Now we print Count: 1

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end First time through, ii = 1 Now we print Count: 1 The loop ends, so we’ll return to the top of the loop

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Second time through, so ii is incremented ii = 2

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Second time through, ii = 2 Now we print Count: 2

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Second time through, ii = 2 Now we print Count: 2 The loop ends, so we’ll return to the top of the loop

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Third time through, ii = 3

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Third time through, ii = 3 Now we print Count: 3 This will continue for awhile…

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Imagine we’re now on the 9 th iteration Ninth time through, ii = 9 Now we print Count: 9 The loop ends, so we’ll return to the top of the loop

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Tenth time through, ii = 10 This will be the last time we go through the loop!

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Tenth time through, ii = 10 Now we print Count: 10

Example: Count to 10 for ii = 1:10 fprintf(‘\n Count: %d’, ii) end Tenth time through, ii = 10 Now we print Count: 10 We’ve run the loop 10 times….because that’s as far as we have been asked to go, the loop ends and we’ll go on to whatever code exists after the end statement.

Using The Index One of the most common uses of the index is to run through a series of numbers in an array One of the most common uses of the index is to run through a series of numbers in an array x = [ ] x = [ ] We can reference each element in the array using the index…… x(1), x(2), etc. as x(ii)

Polling Question 1 Given an array x, write a FOR loop that prints out the negative numbers in x. Hint: this program can be written in 5 lines Given an array x, write a FOR loop that prints out the negative numbers in x. Hint: this program can be written in 5 lines

Solution Given an array x, write a FOR loop that prints out the negative numbers in x. Given an array x, write a FOR loop that prints out the negative numbers in x. for ii = 1:length(x) if x(ii) < 0 fprintf(‘%f \n’, x(ii)); endend

While Loops There are a couple key differences between FOR loops and WHILE loops There are a couple key differences between FOR loops and WHILE loops The syntax is different (obviously) The syntax is different (obviously) For loops run for a set number of iterations, but while loops run until a logical condition is no longer true For loops run for a set number of iterations, but while loops run until a logical condition is no longer true For loops have a built-in indexing system, while loops do not For loops have a built-in indexing system, while loops do not

Indexing With WHILE Loops If you’re going to do the type of array manipulation we just practiced using a WHILE loop, you have to set up your own index. If you’re going to do the type of array manipulation we just practiced using a WHILE loop, you have to set up your own index. This means: This means: Creating your index variable Creating your index variable Incrementing the index each time through the loop Incrementing the index each time through the loop

Comparing WHILE & FOR Loops FOR Loop FOR Loop for ii = 1:length(x) if x(ii) < 0 fprintf(‘%f \n’, x(ii)); endend WHILE Loop WHILE Loop n = 1; while n <= length(x) if x(n) < 0 fprintf(‘%f \n’, x(n)); end n = n + 1; end

Comparing WHILE & FOR Loops FOR Loop FOR Loop for ii = 1:length(x) if x(ii) < 0 fprintf(‘%f \n’, x(ii)); endend WHILE Loop WHILE Loop n = 1; while n <= length(x) if x(n) < 0 fprintf(‘%f \n’, x(n)); end n = n + 1; end Create Index

Comparing WHILE & FOR Loops FOR Loop FOR Loop for ii = 1:length(x) if x(ii) < 0 fprintf(‘%f \n’, x(ii)); endend WHILE Loop WHILE Loop n = 1; while n <= length(x) if x(n) < 0 fprintf(‘%f \n’, x(n)); end n = n + 1; end Set a logical condition

Comparing WHILE & FOR Loops FOR Loop FOR Loop for ii = 1:length(x) if x(ii) < 0 fprintf(‘%f \n’, x(ii)); endend WHILE Loop WHILE Loop n = 1; while n <= length(x) if x(n) < 0 fprintf(‘%f \n’, x(n)); end n = n + 1; end Increment the index

Polling Question 2 Given an array x, write a WHILE loop that will print all the numbers in x that are greater than 5. Hint: this can be written in 7 lines Given an array x, write a WHILE loop that will print all the numbers in x that are greater than 5. Hint: this can be written in 7 lines

Solution Given an array x, write a WHILE loop that will print all the numbers in x that are greater than 5. Given an array x, write a WHILE loop that will print all the numbers in x that are greater than 5. n = 1; while n <= length(x) if x(n) > 5 fprintf(‘%f \n’, x(n)); end n = n + 1; end

Replacing Values Given an array x, y = zeros(1, length(x)) for ii = 1:length(x) if x(ii) > 0 y(ii) = x(ii); endend

Polling Question 3 Given an array x, y = zeros(1, length(x)) for ii = 1:length(x) if x(ii) > 0 y(ii) = x(ii); endend Now, write the code that will do the same thing using a WHILE loop

Solution y = zeros(1, length(x)); n = 1; while n < length(x) if x(n) > 0 y(n) = x(n) end n = n + 1; end

Preallocation Setting up our array of zeros ahead of time is called pre-allocation. It makes the program more efficient by allocating the needed variable space in memory ahead of time Setting up our array of zeros ahead of time is called pre-allocation. It makes the program more efficient by allocating the needed variable space in memory ahead of time This is really important for large arrays, but for small ones, it’s not that big a deal This is really important for large arrays, but for small ones, it’s not that big a deal

Without Preallocation We can choose not to use preallocation and avoid having all those excess zeros We can choose not to use preallocation and avoid having all those excess zeros y = [ ]; n = 1 while n <= length(x) if x(n) > 0 y = [y, x(n)] end n = n + 1; end

Polling Question 4 Now, do the same thing using a FOR loop Now, do the same thing using a FOR loop

Solution y = [ ]; for ii = 1:length(x) if x(ii) > 0 y = [y, x(ii)]; endend

Watch the Questions Are you answering the right question? Are you answering the right question? Set k = the first element of x that is less than 0 Set k = the first element of x that is less than 0 Set k = the index of the first element of x that is less than 0 Set k = the index of the first element of x that is less than 0

Remember Short Circuiting Given an array x, k = length(x) + 1; for n = 1:length(x) if x(n) < 0 k = n; break;endend

Polling Question 5 Write a FOR loop that sets k = to the index of the first element in x that is larger than the following element in x. Write a FOR loop that sets k = to the index of the first element in x that is larger than the following element in x.

Solution for ii = 1:length(x) – 1 if x(ii) > x(ii+1) k = ii; endend Did you set up your indexing properly? Because we’re looking at the element in front of our active index, we have to be careful that we don’t exceed our array’s dimensions! x = [ ] if x(6) > x(7)  Problem: there is no x(7)

Polling Question 6 Write a WHILE loop to determine whether or not a given vector x is in ascending order. Write a WHILE loop to determine whether or not a given vector x is in ascending order.

Solution n = 1; while n < length(x) if x(n) > x(n+1) fprintf(‘Out of Order’); end n = n + 1; end