EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.

Slides:



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

Nested Loops and the Break Statement. What are Nested Loops? Nested loops are: Loops which run inside another loop When would you use nested loops? Performing.
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.
Example – calculating interest until the amount doubles using a for loop: will calculate up to 1000 years, if necessary if condition decides when to terminate.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
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.
CS150 Introduction to Computer Science 1
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Week 7 - Programming I Relational Operators A > B Logical Operators A | B For Loops for n = 1:10 –commands end.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Week 7 - Programming II Today – more features: – Loop control – Extending if/else – Nesting of loops Debugging tools Textbook chapter 7, pages
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.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
COMPE 111 Introduction to Computer Engineering Programming in Python Atılım University
* What kind of loop would I use to complete the following: A. Output all of the prime numbers that are less than 100,000 B. Output the Fibonacci sequence.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
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.
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.
Statements That Repeat. For...Next Loop Structure For counter = start To end Step increment statements Next counter Where Counter is tested to see if.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1. Example 1 – Averages 2. Example 2 – Rolling Dice 3. Example 3 – Number Analysis 4. Example 4 - Divisibility ( while ) Additional Examples on for Loops.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
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.
CS1109 L AB 3 July 3rd. R OAD M AP Homework submission Review How to use function and scripts While-end Finish last exercise Lab 2 Challenge questions.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Matlab Programming for Engineers
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers.
EGR 115 Introduction to Computing for Engineers Designing The Battleship Game in MATLAB Monday 22 Sept 2014 EGR 115 Introduction to Computing for Engineers.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
EGR 115 Introduction to Computing for Engineers MATLAB Basics 6: Debugging in MATLAB Monday 15 Sept 2014 EGR 115 Introduction to Computing for Engineers.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Flowchart. a diagram of the sequence of movements or actions of people or things involved in a complex system or activity. a graphical representation.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
1 for – while – switch ผศ. ดร. อนันต์ ผลเพิ่ม Anan Phonphoem
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 3 Friday 17 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
REPETITION CONTROL STRUCTURE
Computer Application in Engineering Design
Matlab Training Session 4: Control, Flow and Functions
for Repetition Structures
Week 8 - Programming II Today – more features: Loop control
TOPIC 4: REPETITION CONTROL STRUCTURE
Outline Altering flow of control Boolean expressions
MATLAB (Lecture 2) BY:MAHA ALMOUSA.
Conditional Construct
Types of Flow of Control
Repetition Control Structure
Let’s all Repeat Together
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.
REPETITION Why Repetition?
Electrical and Computer Engineering Department SUNY – New Paltz
ME 123 Computer Applications I Lecture 5: Input and Output 3/17/03
Programming Fundamental
Presentation transcript:

EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers

Lecture Outline Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Loops and Vectorization Slide 2 of 15

Loops and Vectorization Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Computers are very good at performing repetitive tasks efficiently  For example – Computer Animation o Calculate the RGB values of every pixel in a 1080p image every 1/30 of a second (i.e., HDTV)  width*height*frame rate*colors*bits/pixel  (16/9*1080)*1080*30*3*24 = 4,478,976,000 bits/sec = 4.5 billion bits / sec  MATLAB offers two constructs for looping o The for loop, and o The while loop Slide 3 of 15

Loops and Vectorization For Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers For Loops: for k = start:step:stop some block of code end  … start = initial value step = incremental value stop = terminating value start = initial value step = incremental value stop = terminating value Slide 4 of 15

Loops and Vectorization For Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example:  Code:  Output: my_sum = 0; for k=0:2:11 my_sum = my_sum + k; fprintf(' k = %2.0f and sum = %2.0f \n', k, my_sum); end k = 0 and sum = 0 k = 2 and sum = 2 k = 4 and sum = 6 k = 6 and sum = 12 k = 8 and sum = 20 k = 10 and sum = 30 start = 0 step = 2 stop = 11 start = 0 step = 2 stop = 11 Further examples for index variable:  Step k with increments of 1 (indexing): for k = 1 : 10 (default step is 1)  Step k with increments of -0.1 (indexing):: for k = 1.0 : -0.1 : 0.0  Step k with values 3, 2, and 7 (direct access): for k = [3, 2, 7] Further examples for index variable:  Step k with increments of 1 (indexing): for k = 1 : 10 (default step is 1)  Step k with increments of -0.1 (indexing):: for k = 1.0 : -0.1 : 0.0  Step k with values 3, 2, and 7 (direct access): for k = [3, 2, 7] Slide 5 of 15

Loops and Vectorization For Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Another Example:  Will the loop terminate early? for k = 1:5 disp(k); if k == 3 k = 6; end disp(k); end CAVEAT: this command DO NOT change the next value of k in the loop 1111 output Slide 6 of 15

Loops and Vectorization For Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example:  Develop a program to compute N! = 1 * 2 * 3 * … *(N-1)*N o Use a for loop  Pseudo Code: o Prompt user for a positive integer N o Initialize N_Factorial o Loop from 1 to N and update N_Factorial o Display the result of N!  Compare your result with MATLAB function “factorial” >> factorial(N) Slide 7 of 15

Loops and Vectorization For Loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Solution: N = 7; % or N = input(' Input N: ') N_Factorial = 1; for i = 1:N N_Factorial = N_Factorial * i; end disp([num2str(N),'! = ', num2str(N_Factorial)]); >> 7! = 5040 Slide 8 of 15

Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers While Loops: while (logical expression) some block of code end … The Logical Expression MUST eventually go false in order to exit the loop!!! Slide 9 of 15

Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example:  Code:  Output: my_sum = 0; k = 0; while k <= 10 my_sum = my_sum + k; fprintf(' k = %2.0f and sum = %2.0f \n', k, my_sum); k = k + 2; end k = 0 and sum = 0 k = 2 and sum = 2 k = 4 and sum = 6 k = 6 and sum = 12 k = 8 and sum = 20 k = 10 and sum = 30 Slide 10 of 15

Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Another Example:  Will the loop terminate early? k=1; while k <= 5 disp(k); if k == 3 k = 6; end disp(k); k = k +1; end This command DOES change the next value of k in the loop 1111 output Slide 11 of 15

Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers An Example:  Develop a program to compute N! = 1 * 2 * 3 * … *(N-1)*N o Use a while loop  Pseudo Code: o Prompt user for a positive integer N o Initialize N_Factorial o Loop from 1 to N and update N_Factorial o Display the result of N!  Compare your result with MATLAB function “factorial” >> factorial(N) Slide 12 of 15

Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Solution: N = 7; N_Factorial = 1; i = 1; while i <= N N_Factorial = N_Factorial * i; i = i + 1; end disp([num2str(N),'! = ', num2str(N_Factorial)]); >> 7! = 5040 Slide 13 of 15

Loops and Vectorization while loops Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Revisit the guessing game num = rand; % Generate a random number betw 0 &1 num = ceil(10*num); % Convert to an integer betw 1 & 10 % Prompt the user to guess the number guess = input('Guess a Integer from 1 to 10: '); while guess ~= num % Test the guess and issue a response if guess < num disp('Your Guess is Too Low!! Guess again'); else % guess > num disp('Your Guess is Too High!! Guess again'); end guess = input('Guess a Integer from 1 to 10: '); end disp('Your Guess is Correct!!'); Looping_Examples.m Slide 14 of 15

Next Lecture Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers Vectorization Slide 15 of 15