Friday, October 28, 2011

Algorithms & Pseudocode

"It's the question that drives us, Neo. It's the question that brought you here."
_Trinity (The Matrix)

What is an Algorithm?
An algorithm is an ordered sequence of steps guaranteed to solve a specific problem. If that sounds like the definition for a computer program...it is! Programmers develop algorithms when they develop computer applications. It's a crucial problem-solving skill that demands critical thinking. It takes time, and many programs, to become truly proficient but there are procedures that can help with creating an algorithm: 
 How to Write Algorithms
1. Understand the problem.
  • read and re-read the problem.
  • analyze the problem and express it in your own words.
  • break it down into smaller steps (in order). 
  • begin thinking about the data inputs required and the variable names you will use.
  • if the program performs any math, work out a few examples with pencil and paper.
  • finalize what is needed to solve the problem (called the requirements).
  • Note: the key skill needed here is attention to detail. Be meticulous.
2. Create a written list of steps for the algorithm. This is called pseudocode.
  • use a pencil and ordinary English words to describe each step.
  • choose suitable variable names for data and use them in your pseudocode.
  • make sure your steps are in the correct sequence. We did say to use a pencil!
3. Code the steps in RAPTOR.
  • add the RAPTOR flowchart symbols suggested by your pseudocode, in the same order.
  • delay configuring any symbol until all symbols have been added.
  • starting at the top, double-click each RAPTOR symbol to configure it.
  • configure using the same variable names you chose for your pseudocode.
  • add comments liberally to explain what the symbols are doing in the program.
4. Test and evaluate the algorithm.
  • run the progam. If the program performs arithmetic, use small numbers for data.
  • use a calculator if necessary to check the arithmetic and outputs.
  • ask someone else to run the program to test assumptions made when configuring.
  • ensure that the program meets the problem requirements by running it repeatedly.
There are many entire books and college courses about algorithms and there are many fine resources on the Internet, too. Advanced programmers need some specific algorithmic techniques, but you get good at algorithms the same way you get to Carnegie Hall. Practice, practice, practice.