Unit 4 Algorithms Lesson 4 Daily warm up

  • Slides: 23
Download presentation
Unit 4 Algorithms Lesson 4

Unit 4 Algorithms Lesson 4

Daily warm up: What is pseudo-code? Give an example.

Daily warm up: What is pseudo-code? Give an example.

Pseudo-code Add two numbers together: Enter two numbers (A, B) Add the numbers (A+B=C)

Pseudo-code Add two numbers together: Enter two numbers (A, B) Add the numbers (A+B=C) Display answer (C) ● ● Is easily understood Describes the complete process Is written clearly Sequenced

Writing in Pseudo-code How can we write the following: - The sum of two

Writing in Pseudo-code How can we write the following: - The sum of two numbers The product of three numbers Counting by 5’s Check the algorithm The sum of two numbers: Enter two numbers (A + B) Add the two numbers (A + B = C) Display Answer (C) The sum of two numbers: Enter two numbers (12 + 5) Add the two numbers (12 + 5 = 17) Display Answer (17)

Writing in Pseudo-code How can we write the following: - The sum of two

Writing in Pseudo-code How can we write the following: - The sum of two numbers The product of three numbers Counting by 5’s The product of three numbers: Enter three numbers (A * B * C) Multiply the numbers (A * B * C = D) Display Answer (D) The product of three numbers: Check the algorithm Enter three numbers (5 * 1 * 2) Multiply the numbers (5 * 1 * 2 = 10) Display Answer (10)

Writing in Pseudo-code How can we write the following: - The sum of two

Writing in Pseudo-code How can we write the following: - The sum of two numbers The product of three numbers Counting by 5’s: Enter start and stop numbers (A, B) Add 5 to the start number (A + 5 = C) Add to stored(C)answer Store 5 answer (D +B? 5 = E) Is stored answer Store (E) No - answer continue Is. Yesstored answer B? answers display stored No 5 - continue Add to stored answer Yes- display stored (C + 5 answers = D) (C, D, E) (D) Store answer Is stored answer B? No - continue Yes- display stored answers

Writing in Pseudo-code How can we write the following: - The sum of two

Writing in Pseudo-code How can we write the following: - The sum of two numbers The product of three numbers Counting by 5’s Check the algorithm Counting by 5’s: Enter start and stop numbers (0, 15) Add 5 to the start number (0 + 5 = 5) Store Add 5 answer to stored(5)answer Is stored answer (1015? + 5 = 15) No - answer continue Store (10) display stored Is. Yesstored answer 15? answers Add to stored answer No 5 - continue (5 + 5 answers = 10) Yes- display stored Store answer (10) (5, 10, 15) Is stored answer 15? No - continue Yes- display stored answers

Your turn Write the following calculations in pseudo-code: a. Subtract two numbers b. Multiply

Your turn Write the following calculations in pseudo-code: a. Subtract two numbers b. Multiply two numbers then subtract 5 c. Count by 2’s from 0 to 10

Algorithms Take an input, perform a calculation, and return an output. A variable stores

Algorithms Take an input, perform a calculation, and return an output. A variable stores a value. We could have used a variable to store the output in our pseudo-code for counting by 5’s. Here are some examples of variables. Area = length x width Square = side x 4 Answer 1 = a + b

Selection Statements Sometimes we need options that meet certain conditions. When counting by 5’s

Selection Statements Sometimes we need options that meet certain conditions. When counting by 5’s to get to 15, we had to check to see if we got to 15. Programmers use if/else statements to accomplish this. We use them in everyday life too. If you are 13 years old stand up; everyone else sit.

Selection statements Let’s build a rectangle and check to see if it is a

Selection statements Let’s build a rectangle and check to see if it is a square. Enter length and width Set area equal to length*width Set perimeter equal to length*2 + width*2 If length is equal to width then print “Your rectangle is a square!” Else print “Your rectangle is not a square. ”

Repetition Sometimes we need to do something over and over before getting to the

Repetition Sometimes we need to do something over and over before getting to the end of the algorithm. This repetition can be shortened using Iteration. This will loop the steps until a condition is met. The term while is used to do this.

Counting by 5’s Our counting by 5’s algorithm was pretty long for such an

Counting by 5’s Our counting by 5’s algorithm was pretty long for such an easy task. Let’s shorten it using a loop. Counting by 5’s: Enter start and stop numbers (A, B) Add 5 to the start number (A + 5 = C) answer = (C) While answer is < 15 Add 5 to answer (answer + 5 = answers) Print C and answers Remember there is more than one way to write an algorithm just like there is more than one way to get to 4 (2+2, 1+3, 2*2).

Data types Basic data: contains single values Examples: an integer, a real number, a

Data types Basic data: contains single values Examples: an integer, a real number, a letter Abstract data: a list of values; a list of basic data Weekdays = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) Print weekdays number 2 (Tuesday)

Penny match Chris and Pat are flipping coins at the same time. If both

Penny match Chris and Pat are flipping coins at the same time. If both coins match (HH or TT), Chris gets both coins, otherwise Pat gets both coins. The game starts with both Chris and Pat having 10 coins. Your algorithm should play the game 10 times and count the number of times Chris wins and the number of times Pat wins.

Warm up for Part 2 1. If you had 1 million books, how would

Warm up for Part 2 1. If you had 1 million books, how would you organize them to find any book quickly? 2. How many books would you need to look at in the worst case scenario to find a title before you have organized the books? 3. How many books would you need to look at in the worst case scenario to find a title after you have organized the books?

Writing Pseudocode for Iteration with Conditions Remember our bus fare example from a previous

Writing Pseudocode for Iteration with Conditions Remember our bus fare example from a previous lesson? ● ask how old you are ● IF you are under 16, THEN pay half fare ● ELSE pay full fare If you try this algorithm using 15 as your age, the answer to the question at step 2 is true, so the algorithm tells you to pay half fare. If you try the algorithm using 16 as your age, the answer to the question at step 2 is false, so the algorithm tells us to pay full fare.

The Else If Instruction The ELSE IF instruction allows there to be more than

The Else If Instruction The ELSE IF instruction allows there to be more than two paths through an algorithm. Any number of ELSE IF instructions can be added to an algorithm. It is used along with other instructions: IF represents a question THEN points to what to do if the answer to the question is true ELSE IF represents another question THEN points to what to do if the answer to that question is true ELSE points to what to do if the answer to the question is false

The Else If Instruction Using this method, the bus fare algorithm could be improved

The Else If Instruction Using this method, the bus fare algorithm could be improved like this: 1. 2. 3. 4. 5. ask how old you are IF you are under 5, THEN pay no fare ELSE IF you are under 16, THEN pay half fare ELSE IF you are an OAP, THEN pay no fare ELSE pay full fare

Activity 1

Activity 1

Card Sort Design an algorithm to arrange a row of playing cards in order

Card Sort Design an algorithm to arrange a row of playing cards in order from lowest to highest value.

Card Sort Rules: ● ● ● ● If a card is on the table,

Card Sort Rules: ● ● ● ● If a card is on the table, it must be face down. You can only see the value of a card by picking it up and looking at its face. You can only hold and look at two cards at a time (1 in each hand). You can compare the values of the cards you are holding in your hands and determine if one is greater, less than, or equal to the other card. When you put a card down, try to be clear about where it should be put back down. Cards should be put face down. You cannot use your memory of face-down cards to make decisions about them. You should behave as though you have no recollection of cards that you aren't currently holding. You will have eight cards to practice, but the procedure you follow should be general enough to work for any number of cards.

Exit Ticket What makes a “good” algorithm? How do you think a sorting algorithm

Exit Ticket What makes a “good” algorithm? How do you think a sorting algorithm should be "measured" to determine if it is the "best"? Ask for ideas and discuss this during the group activity.