Solving a problem often involves making a decision between two or more options. In an algorithm this process of making a decision is called selection.
Examples:
Moving through a set of traffic lights.
1) Drive towards traffic lights
2) Determine whether the traffic light is red, yellow or green
3a) If red, got to 4)
3b) If yellow, go to 5)
3c) If green, go to 6)
4) slow to a stop, then go to 2)
5) continue driving slowly
6) continue driving at constant speed
Deciding whether to take an umbrella to school.
1) Check whether it is going to rain
2a) If yes, go to 3)
2b) If No, go to 4)
3) Bring umbrella to school
4) Don't bring umbrella to school
Giving a student an A, B or C on their test.
1) Determine whether student has achieved 90-100%, 70-89% or 50-69%
2a) If 90-100%, go to 3)
2b) If 70-89%, go to 4)
2c) If 50-69%, go to 5)
3) Give student A
4) Give student B
5) Give student C
Often an algorithm will often require you to repeat a task in order to solve a problem; when this is done in an algorithm the structure is called repetition.
Examples:
Adding the correct amount of cordial to water to make a drink.
1) Attempt to pour 1 part cordial into a glass
2) Attempt to pour 3 part water into the glass
3) Stir (optional)
4) Taste beverage
5) Determine whether drink is too sweet, too bland or just right
6a) If too sweet, go to 7)
6b) If too bland, go to 8)
6c) If just right, go to 9)
7) Pour more water, then go to 4)
8) Pour more cordial, then go to 4)
9) Drink it
Filling a bucket with water
1) Put bucket underneath a tap/ put a hose in bucket
2) Turn on tap
3) Check whether bucket is full
4a) If yes, go to 5)
4b) If no, go to 6)
5) Turn off tap
6) Continue filling bucket with water, then go to 3)
Blowing up a balloon
1) Blow into ballooon
2) Determine whether ballooon has enough air
3a) If yes, go to 4)
3b) If no, go to 5)
4) Tie balloon
5) Continue blowing into balloon, then go to 2)
No comments:
Post a Comment