Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 7: Computer Tools for Problem Solving and Critical Thinking

Similar presentations


Presentation on theme: "Week 7: Computer Tools for Problem Solving and Critical Thinking"— Presentation transcript:

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

2 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

3 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

4 Click through for instructions
Iteration Flow Chart Start Let’s go back to our example: For a list of numbers, x=[ ] 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=[ ] 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

5 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:

6 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;

7 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);

8 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

9 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


Download ppt "Week 7: Computer Tools for Problem Solving and Critical Thinking"

Similar presentations


Ads by Google