Skip to main content

Pseudocode

For getting the turtle to draw simple things, like a box, you can just enter Logo instructions in TG's CommandCenter and most of the time your program just works.  It does what you wanted it to, what you thought it would do.  However, when you write any non-trivial program, this will rarely be the case.  The more instructions you include in your programs, the more you need to keep in your head about what you're doing and the more typing you need to do.  This means more chances for making mistakes.
In this lesson you will learn how to write computer programs in steps.
The first step will be to just think about what you need to do.
In the next step you will write what you want the program to do in pseudocode.
    pseudo adj. 1: being apparently rather than actually
           as stated.  ( Webster's New Collegiate Dictionary )

    Pseudocode (derived from pseudo and code) is a description
    of a computer programming algorithm that uses the structural
    conventions of programming languages, but omits detailed
    subroutines or language-specific syntax.  ( WikipediA )
   
A third step is the conversion of the pseudocode into properly formed instructions that are available in the programming language you are using, Logo in our case.

How To Write Non-Trivial Programs
That Do What You Want

When you did the exercises at the end of the last lesson, my guess is that you didn't type in programs that worked perfectly the first time.  If you did, congratulations... you are great at visualization and orderly thought.  The good news is that anyone can learn to write correct programs - an experienced programmer would have no problem writing "correct" programs for these exercises. 
Why?
An experienced programmer would think about how to solve the problem before she would start typing in instructions.  An experienced programmer would break the problem into pieces that each are simple to do by themselves.  Then, put all the pieces together to solve the problem.  In computer science this is a process that's called "stepwise refinement."  You break the problem down into steps and then refine what each step does.
Let's play "experienced programmer" with the first exercise.

Understanding the Problem

What you should do first is think about what you need to do.  Get out some paper and a pencil to write down your thoughts.  Look at the diagram carefully.  How can you break it up into multiple, simpler-to-do parts.

You need to get the turtle to:

  • draw 4 rectangles
    • one is tall and thin
    • three are short and form a stack of rectangles next to the first
    - OR -
  • draw 7 lines (4 horizontal and 3 vertical)
    • 4 lines form the perimeter of a big rectangle
    • 1 line splits this rectangle in half vertically
    • 2 lines split the right half of the rectangle into 3, equally sized rectangles
    - OR -
  • draw 13 line segments (6 horizontal and 7 vertical).  This is hard to describe; so, I'm choosing not to go any further with it.  But, it is a way of looking at the problem.
Can you think of any other ways to break down and describe what the turtle needs to be instructed to do?
Ok. This completes the "Understanding the Problem" phase, now on to planning.

Devising a Plan


      The most important mental tool for coping with complexity
      is abstraction.  Therefore, a complex problem should not be
      regarded immediately in terms of computer instructions...
    
                 (On the Composition of Well-Structured Programs,
                  Nicklaus Wirth, Computing Surveys, Dec., 1974)
   
So you now have some sketches and notes on a piece of paper that describe ways that lead to a solution.  The next step is to decide on an approach and put together a process, a procedure which gets the turtle to draw the figure.
Let's follow our experienced programmer's thoughts through the rest of a solution.  She has chosen to draw a square (a special kind of rectangle) and subdivide it.
The first step: figure out how many turtle steps each side of the square should be.  Since it will be split in half and into thirds, she wants a length that is a multiple of 2 and 3.  She likes nice round numbers so she makes each side of the square 120 turtle steps (120/2=60; 120/3=40).  As you can see, it's nice to know a little bit of math when you're programming.
Most beginning programmers would not have thought about the size as a first step.  So if this caught you by surprise, no big deal.  You would have found out that it is important when you were further along.  But it is important for you to know the level of detail that an experienced programmer considers, that you will eventually think about when you write big programs.
With this decided, the experienced programmer first writes her program down on paper in pseudocode.
   1. draw a square
   2. draw the vertical line that splits the square in half
   3. draw the top horizontal line spliting the right half
   4. draw the bottom horizontal line spliting the right half
   5. make the turtle invisible
   
Pseudocode is a term for describing something in your native language.  Her pseudocode is English descriptions of what she wants her program to do.  Once this is complete and she is convinced that her plan should produce what she wants, it's time for the next phase: converting the pseudocode into Logo instructions.

Comments

Popular posts from this blog

Pointers in C

Pointers are an extremely powerful programming tool. They can make some things much easier, help improve your program's efficiency, and even allow you to handle unlimited amounts of data. For example, using pointers is one way to have a function modify a variable passed to it. It is also possible to use pointers to dynamically allocate memory, which means that you can write programs that can handle nearly unlimited amounts of data on the fly--you don't need to know, when you write the program, how much memory you need. Wow, that's kind of cool. Actually, it's very cool, as we'll see in some of the next tutorials. For now, let's just get a basic handle on what pointers are and how you use them. What are pointers? Why should you care? Pointers are aptly name: they "point" to locations in memory. Think of a row of safety deposit boxes of various sizes at a local bank. Each safety deposit box will have a number associated with it so that you ca...

Types of Computer Networks(LAN,MAN,WAN)

Types of Computer Networks Computer networks are categorized according to: How they are organized physically. The way they are used. The distance over which they operate. Three main types of computer networks are as follows: LAN   (Local Area Network) WAN (Wide Area Network) MAN ( Metropolitan-Area Network) LAN (Local Area Network) LAN is the most common type of network . LAN stands for Local area Network . It covers a small area. Most LANs are used to connect computers in a single building or group of buildings. Hundreds or thousands of computer maybe connected through LAN. Typical LANs can be found in industrial plants, office buildings, and college or university campuses. LANs are capable of transmitting data at very fast rate. Data transmission speeds of LAN are 1 to 100 megabits per second. It is much faster than data transmission over a telephone line. LAN can transmit data in a limited distance. There is also a limit on the number of...

Ring topologies

Ring topologies are similar to bus topologies, except they transmit in one direction only from station to station. Typically, a ring architecture will use separate physical ports and wires for transmit and receive. Token Ring is one example of a network technology that uses a ring topology.