Wk2.2 Prior Knowledge Lesson 2 DiT_Y07-08Band_U1_SS_Algorithms.pdf
Document Details
Uploaded by AstonishedEcoArt
Ebenezer Christian College
Tags
Related
- Fundamentals of Algorithms and Flowcharts PDF 2020
- Lecture 1: Subject Introduction and Overview on Structured Programming Approach PDF
- Week 2 - Algorithm & Flowchart PDF
- Fundamentals of Programming - Algorithms and Flowcharts PDF
- Algorithms and Flowcharts Lecture Notes PDF
- Lecture 2: Flowcharts and Computer System PDF
Full Transcript
Algorithms There’s An App For That Flowchart Basics We Theusediagram standard of symbols an algorithm to make is it easy called for others a to flowchart. understand. Basic flowchart symbols...
Algorithms There’s An App For That Flowchart Basics We Theusediagram standard of symbols an algorithm to make is it easy called for others a to flowchart. understand. Basic flowchart symbols I recognise this! This is a flowchart to Start order a milkshake. Get flavour data from user “chocolate or strawberry? What flavour did Chocolate Get chocolate flavour the user choose? Strawberry Get strawberry flavour Put flavour in shaker Do you recall what the coloured flowchart Put milk in shaker symbols mean? Shake well Pour in glass Stop Terminator (start/stop) An oval or rounded rectangle is used to show the beginning and end. The terminator indicates the beginning and end of the algorithm. Draw an oval and write START or STOP inside it. START STOP Ready to move on? Decision Add the question to the diamond shape Use the decision symbol to and the answers to ask questions or branch to the arrows pointing different options. out of the diamond. For example, checking if the user selected option 1 or option 2. Did the user yes select option1? Ready to no move on? Input/Output Input can include user input or input from a file or Use the input/output program. symbol to show user Output can be displayed on interactions in the the screen or stored in the computer. algorithm. For example, when you ask the user for input or show an answer on the screen. Ready to move on? Process The process blocks show a logical sequence of steps Use the process symbol to in the algorithm. describe the simple steps. This could include calculations, actions or any other type of instruction. Ready to move on? Arrows Arrows help to communicate the flow of Arrows connect the the algorithm. different symbols and show the flow from beginning to end. The flowchart is generally drawn from top to bottom but arrows help to show steps that point back or jump ahead. Ready to move on? Flowchart rules The flow chart starts at the top left with Here are some simple rules START to follow when creating basic flow charts. and ends at the bottom right with STOP Decision symbols Process symbols are include a question connected with arrows. inside the diamond One or more arrows can point with two arrows into the process but only one pointing out of the arrow will point out of the diamond. process. Next steps Complete an What do you want to activity? do next? Write a simple flowchart for an everyday activity. Contents GO BACK: Basic flowcharts NEXT: Advanced flowcharts Learn about basic flow chart Learn about flowchart symbols for symbols. connectors and functions. Go back and Move on to the next review? section? Advanced Flowcharts That’s Complex When ayour great YouYescan idea. sort algorithms use of. If I use cana I’m algorithms getting much function In flowcharts functions, Like also Whatthey bedosymbol joined do I youwe might with become more betteraatgroup to use maps be replace symbols across able mean? in an to pages fitcalled atlas? my complex flowcharts you can now. of connectors algorithm steps. to usingon one join use abstraction to parts of flowcharts connectors. page.easier make them together. to understand. Functions Functions can be used to group a set of complex instructions. Function name The function symbol is like the process symbol. A function could appear once or several times in the same It groups a set of related algorithm. process steps together and gives them a name. Functions Start Start Get flavour data from user Get flavour data from user “chocolate or strawberry? “chocolate or strawberry? What Chocolate What Chocolate flavour did Get chocolate flavour flavour did the user Get chocolate flavour the user choose? choose? Strawberry Strawberry Get strawberry flavour Put flavour in shaker Make Get strawberry flavour milkshake Put milk in shaker Stop Shake well Pour in glass Stop The steps in the circled part of this algorithm can be grouped and made into a function. I named this function ‘Make milkshake’. Functions Start Get flavour data from user Start “chocolate or strawberry? flavour Make milkshake What Chocolate Put flavour in shaker flavour did the user Get chocolate flavour choose? Put milk in shaker Strawberry Make Get strawberry flavour milkshake Shake well Stop Pour in glass Stop Make milkshake Thisfunction This functionseparates can be reused fordetail out the a variety of or steps different flavours. for making a milkshake. This hiding of detail is called abstraction. Connectors Connectors can be used to join algorithms across pages. A circle connector is used to show how parts A of an algorithm join on Same page the same page. connector There are two An off-page connector 2 types of is used to connect connectors. algorithms on different Off page pages. connector Connectors A 2 Start Clear totalArea. No Add shapeArea to totalArea. Is shape a B Display: “This program calculates rectangle? the area of a composite shape” Display: “What is the next shape? Yes Please enter triangle, rectangle, Display: “What is the first shape? Please circle or none:” Calculate enter triangle, rectangle, circle or none:” RectangleArea Get shape 2 No Is shape 1 none? 1 Clear shapeArea. B Yes No No Display: “The area of the Is shape a composite shape is”, totalArea Is shape a A 2 circle? triangle? Yes Yes Stop Calculate Calculate TriangleArea CircleArea 2 2 Page 1 Page 2 Connectors help to organise large flow charts across multiple pages. Next steps Complete an What do you want to activity? do next? Simplify a complex algorithm. Contents GO BACK: Advanced NEXT: Variables flowcharts Review connectors and Learn about variables and how to use functions. them in a flowchart. Go back and Move on to the review? next section? Variables WeThat’s use right, but variables That Variables makes can besense, used as Avariables variable in maths is in when a especially a label forwhen text, you think colours, algorithms What place-holder wehowdoevenis acan algebra.for about pictures, computers sound! information represent variable? more that storeWeanduse a letter display lots of can than change. just to represent different types ofthe data. numbers. variable. In Digital Technologies, a variable is a place-holder for information that can change. Variables in maths Formula for a triangle: The formula to calculate the area of a triangle is a type of A = ½b x h algorithm with variables. The same formula can be used for lots of different triangles. Variables in algorithms Variables in algorithms can be used to store data The that might change, Start itemCount such as a score in a variable game or a count of tracks how items collected. Collect item many items have been collected. Add 1 to itemCount Add 10 to score The score When you use variables Hide item variable in a flowchart, highlight keeps track of the variable name by Stop the score. using a different format. Variables in decisions Variables can also be used to hold the Get flavour data from user user choice in a “chocolate or strawberry? decision step. flavour = Chocolate What flavour did Get chocolate the user flavour choose? flavour = Strawberry When you use variables more than Get strawberry once in a flowchart, flavour use exactly the same spelling each time. Variables in loops By using variables in Start a repeating section (also called a loop) Get numStars data from user we can control the number of repeats. The first time Set starCount to 0 you use a variable give Draw a star at random it a starting x,y coordinates value. Add 1 to starCount Change the Is variable that starCount Variables controls the No numStars: The total number equal to repeats inside the numStars of stars selected by the user. loop to make sure ? starCount: The number of it will finish. Yes stars drawn so far. Stop Variables in functions Variables can also be used to pass values to and from flavour Start functions. Make milkshake Put flavour in shaker Use the input/output symbol to pass Put milk in shaker flavour data to the MakeMilkshake function Shake well Flavour data could be stored in The the flavour variable. Pour in glass MakeMilkshake function needs flavour data. Variables Stop Make milkshake flavour: The flavour selected by the user e.g. chocolate or strawberry. Benefits of variables Abstraction helps us create a model for our solution. A=½ bxh Variables in a computer program can be any name you like. Variables can change value during the program to keep track of data properties and values. Variables are a form of abstraction and can make a Variables fill in the simple algorithm work with lots of different values. details of the model. Draw a ____ shape of size ____ and colour ____. This could be This could be This could be the the shapeType the shapeSize shapeColour variable. variable. variable. Variables tips A variable is a place-holder for information that can change Here are some simple rules to follow when When you use variables in a using variables in flowchart, highlight the algorithms. variable name by using a different format. When you use variables When you use variables to control a more than once in a repeating section: flowchart, use exactly the 1. Give the control variable a starting same spelling each time. value before the loop. Keep a list of variables you 2. Change the control variable inside have used and don’t use the the loop to make sure it can same name twice. eventually finish. Next steps Complete an What do you want to activity? do next? Modify an algorithm to use variables. Contents GO BACK: Variables NEXT: Pseudo-code Review how to use variables in your algorithms. Learn how to write your algorithms using structured English. Go back and Move on to the review? next section? Pseudo-code You Another guessedway of it! Flowcharts are Mmm, Why Pseudo-code So,writing would ait’s personal an I use Ineasier scientific to Pseudo… pseudo-code algorithms looks algorithm choice… likedoesn’t code that is if byI terms, understand it means but looks or already but that in using like it algorithm mean can know a piece be pseudo-code to closely pseudo-code translated ofpretend? terms… computer about into or takes resemble. up less flowcharts? any astructured decision. code? coding room. language. English. Pseudo-code statements Pseudo-code uses sentence fragments Start to list the steps in Indentation is used the algorithm. to show a group of statements. Collect item Flowchart Add 1 to itemCount Pseudo-code START Collect item Add 1 to itemCount Add 10 to score Add 10 to score Hide item STOP Hide item To convert a process block to pseudo-code KEYWORDS are just remove the shape. Stop used instead of symbols. Decision statements Start Get flavour data from user “chocolate or strawberry? To convert a decision Flowchart block to pseudo-code What Chocolate remove the shape and flavour did the user Get chocolate flavour change the question to choose? IF the Strawberry an expression. Make expression expression isis Get strawberry flavour milkshake true, true, THEN THEN do the first statement Stop and jump We use IF, THEN, ahead to ELSE keywords for a START ENDIF decision step. Ask user “chocolate or strawberry?” Pseudo-code Get flavour data from user IF flavour = “chocolate” THEN Get chocolate flavour ELSE, the ELSE expression is false, Get strawberry flavour so do the second ENDIF statement and then Make milkshake continue after the ENDIF. STOP Repeating statements Flowchart Pseudo-code To convert repeating statements from a Option 1: REPEAT flowchart to Set starCount to 0 starCount = 0 pseudo-code, use REPEAT indentation and Draw a star at random Draw a random star appropriate keywords x,y coordinates Add 1 to starCount at the start and end of UNTIL starCount = numStars the repeating section. Add 1 to starCount Option 2: WHILE We use REPEAT, WHILE WHILE starCount < numCount Is or FOR to group starCount Draw a random star repeating statements. equal to Add 1 to starCount numStars? END WHILE Option 3: FOR FOR i from 1 to numStars Draw a random star Add 1 to i Pseudo-code To convert a process block to pseudo-code just remove Here are some simple the shape. rules to follow when Use indentation to structure writing with statements into groups. pseudo-code or structured English. Use uppercase to show special keywords for decisions and loops. To convert a decision block to To convert repeating statements from a pseudo-code remove the shape flowchart to pseudo-code, use indentation and change the question to an and appropriate keywords at the start and expression. end of the repeating section. Statements for the true branch Keywords to use for repeating sections are: go between THEN and ELSE REPEAT … UNTIL expr Statements for the false branch WHILE expr … END WHILE go between ELSE and ENDIF. FOR i from minValue to maxValue … Next steps Complete an What do you want to activity? do next? Re-write a flowchart in pseudo-code. Contents GO BACK: Pseudo-code NEXT: Activities Review how to write algorithms in pseudo-code. Select an activity from the activity list. Go back and Move on to the review? next section? ACTIVITIES Which activity Advanced Flowchart do you want flowcharts basics to practise? Variables Pseudo-code Contents Write a simple flowchart for an everyday activity. Write a simple flowchart for making a hot beverage. Include options for milk and sugar. Test out the Modify the flowchart to serve multiple beverages. algorithm at home to see if you missed any steps. Simplify a complex algorithm Simply this algorithm for a paper, scissors rock game using connectors & functions. If not paper or Start scissors then it must be rock. Display “Paper, scissors, rock game begins” Is No Is No Is No Is No userChoice userChoice compChoice compChoice paper? scissors? paper? scissors? Set userScore to 0 Yes Yes Yes Set compScore to 0 Yes Is Is No Display: “What is your name? ” No compChoice compChoice paper? paper? Get userName Is No Yes Yes compChoice Display: “Choose paper, scissors or rock ” scissors? Is No compChoice Yes scissors? Get userChoice Yes Display: userName, Set compChoice to a random choice (paper, scissors or rock) “wins” Display: “computer wins” Display “no one wins” Add 1 to compScore Add 1 to userScore Stop Modify an algorithm to use variables This algorithm counts up to 10. Modify the algorithm so it will count up to a number entered by the user. START Keep a list of Set count variable to 0 variables and use REPEAT the correct spelling Add 1 to count each time. Say count for 1 second UNTIL count = 10 STOP Rewrite a flowchart in pseudo-code Rewrite this composite area algorithm in pseudo-code. A 2 Start Clear totalArea. No Add shapeArea to totalArea. Is shape a B Display: “This program calculates the area of a composite shape” rectangle? Use uppercase for keywords and Display: “What is the next shape? Yes Please enter triangle, rectangle, circle or none:” Display: “What is the first shape? Please enter triangle, rectangle, circle or none:” Calculate indentation for RectangleArea groups of Get shape 2 Is shape No statements. 1 none? 1 B Clear shapeArea. Yes No No Display: “The area of the composite Is shape a shape is”, totalArea Is shape a A 2 circle? triangle? Yes Stop Yes Calculate Calculate TriangleArea CircleArea 2 2 Page 1 Page 2