|
|
|
Programming is the art of creating sequences of instructions that tell a computer to carry out a task. A sequence of instructions is known as a program. In developing a computer program, the programmer mustOnce the programmer has defined the problem they begin to design the specific steps that will be used in the program. There are two commonly used tools that help the programmer design the solution: flowcharts and pseudocode.
- Define the problem
- Design a solution
- Write the program
- Compile, debug and test the program
A flowchart consists of special geometric symbols connected by arrows. Within each symbol is a phrase presenting the activity at that step. For instance, the parallelogram denotes input or output. The arrows connecting the symbols, called flowlines, show the progression in which the steps take place.Here are examples of several different kinds of flow chart symbols:
The main advantage of using a flowchart to plan a task is that it provides a pictorial representation of the task, which makes the logic easier to follow. We can clearly see each step and how each step is connected to the next. The major disadvantage to using flowcharts is that when a program is very large, the flowcharts may continue for many pages, making them hard to follow.
Let's see how it all fits together:
A computer program makes use of algorithms. An algorithm is a complete step-by-step procedure for solving a problem or accomplishing a task. To understand how an algorithm works, consider the following problem:
The Acme Widget Company gives each of its salespeople $1,000 at the beginning of each month to cover travel, lodging, and food expenses. At the end of the month, a salesperson must total his or her expense receipts. If the amount is less than $1,000 then the difference must be returned to the company.Here is an algorithm for figuring how much money, if any, must be returned:
- Total the expense receipts for the month
- Subtract the amount of the expense receipts from 1,000
- If the remainder is more than 0, return that amount to the company
To visualize how an algorithm works, a programmer may use a flowchart in which each step of the algorithm is represented by a symbol and the flow of the program is represented by arrows. This is a flowchart for the algorithm described above:
Click here to see the pseudocode for this algorithm
All computer instructions are based on four basic processing patterns: simple sequence, selection, loop and branch.
- Simple Sequence logic involves executing instructions one statement after another-in the order presented by the program. This is the simplest and most-used pattern. The computer assumes that all instructions are to be executed in this order unless the program presents other instructions.
- Selection Pattern requires that the computer make a choice among two or more items. Each choice is based on one of three comparisons a computer can make: equal to, less than or greater than.
- Loop Pattern causes an interuption in the normal sequence of processing and directs that computer to loop back to a previous statement in the program, repeating the same sequence over again, usually with new data. By looping the programmer avoids having to repeat the same set of instructions over and over.
- Branch Pattern is often used in combination with selection or looping. The branch pattern allows the computer to skip statements in a program. The branch enoucrages undisciplined jumping around among program statements, a characteristic that is frowned on by most programming experts. Branching is difficult to follow and is an inefficient use of computer power.
Pseudocode uses everyday language to prepare a brief set of instructions in the order in which they will appear in the finished program. It is an abbreviated version of actual computer code (that's why it is called pseudocode). Pseudocode looks more like a real program than the flowchart does. It allows the programmer to focus on the steps required to solve a program rather than on how to use the computer language. An advantage of pseudocode is that it can be used to describe a program to a nontechnical user and can still provide guidelines for the writing of program code.In the Acme Widget Problem the following algorithm is developed:
- Total the expense receipts for the month
- Subtract the amount of the expense receipts from 1,000
- If the remainder is more than 0, return that amount to the company
The Pseudocode for this algorithm would look like this:
Click here to see the flowchart for this algorithm
- Begin
- Get the Expenses
- If the Expenses < 1000 then
- Subtract Expenses from 1000 (expenses-1000=remainder)
- Print "You Owe" and Remainder
- Else Print "You Owe Nothing"
- End
Return to the Beginning
written by Andrea Polli
edited by Schuy Jewell
Columbia College Chicago
Programming for Interactive Mulitmedia I Home