Week 7: Computer Tools for Problem Solving and Critical Thinking

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

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
Repeating Actions While and For Loops
ITC 240: Web Application Programming
For Loops 2 ENGR 1181 MATLAB 9. For Loops and Looped Programming in Real Life As first introduced last lecture, looping within programs has long been.
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.
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Coding Design Tools Rachel Gauci. Task: Counting On Create a program that will print out a sequence of numbers from "1" to a "number entered”. Decision’s.
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.
For Code Next For Code Next A loop is a segment of a code that repeats (changing slightly each time)
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
Chapter 7 Problem Solving with Loops
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
Lesson #5 Repetition and Loops.
MATLAB – More Script Files
REPETITION CONTROL STRUCTURE
Repetition Structures Chapter 9
Matlab Training Session 4: Control, Flow and Functions
Topics Introduction to Repetition Structures
Lesson #5 Repetition and Loops.
Scratch: iteration / repetition / loops
CS1371 Introduction to Computing for Engineers
Think What will be the output?
( Iteration / Repetition / Looping )
Topics Introduction to Repetition Structures
Iterations Programming Condition Controlled Loops (WHILE Loop)
Logical Operators and While Loops
LESSON 11 – WHILE LOOPS UNIT 5 – 1/10/17.
Learning to Program in Python
Control Structure Senior Lecturer
Learning to Program in Python
CPS120: Introduction to Computer Science
Lesson #5 Repetition and Loops.
Introduction to pseudocode
MATLAB – Basic For Loops
For -G7 programing language Teacher / Shamsa Hassan Alhassouni.
Global Challenge Fitness Friend Lesson 2.
Global Challenge Night Sensor Lesson 2.
Loops CIS 40 – Introduction to Programming in Python
Types of Flow of Control
Coding Concepts (Basics)
Introduction to Programming
Global Challenge Night Sensor Lesson 2.
Module 4 Loops.
Global Challenge Night Sensor Lesson 2.
Global Challenge Fitness Friend Lesson 2.
Global Challenge Night Sensor Lesson 2.
Let’s all Repeat Together
Global Challenge Night Sensor Lesson 2.
Matlab tutorial course
Global Challenge Fitness Friend Lesson 2.
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.
Global Challenge Night Sensor Lesson 2.
Global Challenge Fitness Friend Lesson 2.
Global Challenge Night Sensor Lesson 2.
Global Challenge Fitness Friend Lesson 2.
A LESSON IN LOOPING What is a loop?
CHAPTER 6: Control Flow Tools (for and while loops)
Global Challenge Night Sensor Lesson 2.
CPS125 Week
Java Programming Loops
Global Challenge Fitness Friend Lesson 2.
Chapter 4: Repetition Structures: Looping
Lesson #5 Repetition and Loops.
Global Challenge Night Sensor Lesson 2.
Global Challenge Night Sensor Lesson 2.
Introduction to Programming
Global Challenge Fitness Friend Lesson 2.
Presentation transcript:

Week 7: Computer Tools for Problem Solving and Critical Thinking For Loops Week 7: Computer Tools for Problem Solving and Critical Thinking

Iterations Sometimes we want to repeat a task over and over until some condition is met. For example- For every student on my class list, display their student number For a list of numbers, I want to extract all the even numbers from the list For a list of numbers I need to display the square of each number These are what are called iteration tasks- or something repeated over and over a certain number of times

Iteration Tasks Iteration tasks have five basic components- A counter A start value for the counter A step size for the counter A finishing/stopping value for the counter Instructions to follow each iteration (or value of) of the counter

Click through for instructions Iteration Flow Chart Start Let’s go back to our example: For a list of numbers, x=[2 5 7 8] we want to display the square of each number. What do we need? Our list of numbers- a vector, x, of values Our counter variable, i- if we want to extract values from vector x one value at a time, starting at the first value, we need to start with a value of i=1. Now let’s think what we want the program to do. We want to go through each element of the vector x and display x2 one element at a time. So our step size is 1 (one element at a time) Once we get past the last element, we want the program to stop. For example for vector x=[2 5 7 8] once our counter gets to 5, x(5) exceeds vector dimensions. Our stopping value is the length of the vector, in this case 4 Now that we have all 5 of our iteration components we can think about how MATLAB is going to execute the instructions of disp(x(i)^2) within the range of vector x length one element at a time. This is a looping structure. Because we know the start and stop values of our counter (i.e. we know how many times we want to loop) we can use a For loop Input vector x set counter start=1 No is i<= length(x)? Yes Stop LOOP disp(x(i)^2) set i=i+1

For Loops in MATLAB BASIC SYNTAX with our FIVE components: for counter = start_value:step_size:stop_value instructions end *Read through the Mathwors documentation on For Loops: http://www.mathworks.com/help/matlab/ref/for.html

Click through for instructions For Loops in MATLAB Click through for instructions for counter = start_value:step_size:stop_value instructions end How will we code this? What was first on our flow chart? x= input (‘Please insert a vector:’) Then we have to set our counter, i, value to the start value 1: start_value= 1; On our flow chart we now have to ask if our counter, i, is bigger than the length of x. We should create a variable with this value. This value is also our stop value. stop_value= length(x); Then we can also create a variable for our step size: step_size= 1;

Click through for instructions For Loops in MATLAB Click through for instructions Now we can start to code our looping structure: for i = start_value:step_size:stop_value instructions end What about the instructions? We want to display the square of each element of x. The line of code we need inside the for loop is thus: disp((x(i))^2);

For Loops in MATLAB What will this all look like together? Create an .m file ForLoopsDemo.m and try the code below: %This program takes a vector input and displays the squares of %each element x= input (‘Please insert a vector:’) start_value=1; stop_value= length(x); step_size=1; for i = start_value:step_size:stop_value disp((x(i))^2); end

Exercises Starting from the code above, edit the script to display only the even numbered elements. Make a vector that has at least 10 entries. Hint-you won’t start at 1 and you won’t step by 1. Starting from the code above, edit the script to now store all the even numbered elements in a vector, y, and display y at the end. Hint- your instructions in the loop will be to create y, your disp command will be used outside the looping structure. Display a count down from 10 to 0. Hint-your step size doesn’t have to be positive. Compute the sum of the numbers 1 to 10 without using any built-in MATLAB commands for sum. For simplicity you can do this first without creating a vector of the values, but for next exercise you will need to. Compute the sum of all the elements in a vector without using any built-in MATLAB commands for sum . Challenge-create a vector that should sum to 0. Using the code above in 4, compute the average of your vector without using any built-in MATLAB commands for average. Write a program that will display the cube of a number up to a given integer. Hint- you will need to refresh on fprintf. The display should look something like this: The number is : 1 and cube of 1 is :1 The number is : 2 and cube of 2 is :8 The number is : 3 and cube of 3 is :27 The number is : 4 and cube of 4 is :64