A. Introduction
Have you completed all the tasks in 1.3? If not, try to do all of them. Make sure you are comfortable with errors. Now think about it: what else can you program other than basic prints and calculations?
B. Algorithms and Pseudo Code
When dealing with complex problems, you have to think of the steps to solve the problem. When you write down these steps, you create an "algorithm" to solve the problem.
You don't write your algorithms in a programming language, you write them in English using a simplified format such as if... then... else so everyone can understand. The algorithms written in simplified English are called pseudocode. See examples here.
C. Try this:
Consider a robot that is placed in a room. The robot can:
• Move forward by one unit. • Turn left or right. • Sense what is in front of it: a wall, a window, or neither.
Write an algorithm that enables the robot, placed anywhere in the room, to count the number of windows. For example, in the room at right, the robot (marked as R) should find that it has two windows.
let “w” be the # of windows. set w = 0
make robot go to wall
turn until robot can sense wall
move the robot around the perimeter of the room with the sensor still facing the perimeter
if sense wall, move to next unit
if sense window, do (w+1), then continue to move around the perimeter
repeat step 4,5 for every unit until you finish the entire perimeter of the room
Make robot face south. Move forward until impossible and sense the material. Then robot turn right and detect window or wall. If robot reaches a corner. turn right and continue detecting. Set detecting = turn left and determine the material. When material is wall, turn back right and move forward by one unit. When the material is a window, turn back right and move forward by 2 units. If the robot detects a window, print (i) If another window is detected, print (i + 1). Int i = 1, i = i + 1 Detect the room exactly 35 times. The last number the robot prints is the number of windows in the room.
if: window is in front of R add 1 to window count
then: move to the left
then: add another to window count if there is a window in front of R
else: if window is not in front of R move to the next space
then: Continue this system until every window is counted
1. Make bot move to the nearest corner
2. Make bot face start of line
3. Sense
4. Turn bot to the right
5. Move bot forward 1 unit
6. Turn to the left
7. Sense
8. Continue until the bot reaches end of the line
9. Turn bot 90 degrees so it is facing upwards and is on the start of the next line
10. Continue this system until the bot ends up where it started
Walk until R senses a wall or glass in front of it
Turn left
Walk until R senses a wall or glass in front of it
Repeat the following 4 times:
Repeat this step until told to leave:
If block in front is glass:
Turn left
If block in front is not air:
Leave
Walk 1
Turn right
Repeat the following until Else is reached:
If block in front is glass:
Turn left
If block in front is not air:
Skip to Else
Walk 1
Turn right
Else:
Add 1 to WindowCount
Else:
Turn left
If block in front is not air:
Leave
Walk 1
Turn right
Drive the bot to the wall
Turn the bot so that the sensor faces the wall
Gather information.
Move right one space
Gather information
If reach corner turn 90 degrees to the left
Repeat until it reaches the same point.
Divide the number of window panes sensed by 2.