How Computers Solve Problems Computers also use Algorithms to solve problems, and change data into information Computers can only perform one simple step at a time Complex “Human” Algorithms must be broken down into simple step-by-step instructions BEFORE they can be translated into computer code
Algorithm Definition: An algorithm is a procedure consisting of a finite set of unambiguous rules (instructions) which specify a finite sequence of operations that provides the solution to a problem, or to a specific class of problems for any allowable set of input quantities (if there are inputs). In other word, an algorithm is a step-by-step procedure to solve a given problem
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '08 3 Computer Program A computer program may also be called: – Project – Application – Solution
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '08 4 Program Development Cycle Software refers to a collection of instructions for the computer The computer only knows how to do what the programmer tells it to do Therefore, the programmer has to know how to solve problems
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '08 5 Performing a Task on the Computer Determine Output Identify Input Determine process necessary to turn given Input into desired Output INPUTPROCESS OUTPUT
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '08 6 Problem-solving: Approach like algebra class How fast is a car traveling if it goes 50 miles in 2 hours? Input: the distance and time the car has traveled Process: speed = distance / time Output: a number giving the speed in miles per hour
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '08 7 Pictorial representation of the problem solving process
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '08 8 Program Planning A recipe is a good example of a plan Ingredients and amounts are determined by what you want to bake Ingredients are input The way you combine them is the processing What is baked is the output
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '08 9 Program Planning Tips Always have a plan before trying to write a program The more complicated the problem, the more complex the plan must be Planning and testing before coding saves time coding “Program in Haste - debug forever.”
Programming Task A typical programming task can be divided into two phases: Problem solving phase – produce an ordered sequence of steps that describe solution of problem i.e. an algorithm Implementation phase – implement the program in some programming language
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '08 11 Program Development Cycle 1. Analyze: Define the problem. 2. Design: Plan the solution to the problem. 3. Choose the interface: Select the objects (text boxes, buttons, etc.). 4. Code: Translate the algorithm into a programming language. 5. Test and debug: Locate and remove errors. 6. Complete the documentation: Organize all the materials that describe the program.
Expressing the Algorithms A “Standard” way of describing an algorithm must exist if we expect our solution to be understood by others easily There are three main standards in programming: – PSEUDOCODE – FLOWCHARTS – PROGRAMMING LANGUAGE
Pseudocode Pseudocode is a generic way of describing an algorithm without use of any specific programming language syntax. “Pseudo” means “pretend” or “false” Pseudo Code is pretend or false computer code; generic English-like terms that are somewhat like computer code Pseudo Code is not as standardized as flowcharts, and does not facilitate the breaking down of problems as well as a flowchart does
Pseudocode Example Problem Statement: Obtain a number from the user, then display twice this number back to the user. 1.display prompt to user 2.read number 3.result = number * 2 4.display result
Pseudocode Advantages – Almost as easy to learn as expressing the Algorithm in English – Easier to translate into a programming language than stating the solution in English – The pseudo-code statements closely resemble most procedural programming languages Disadvantages – This technique relies on your understanding of the procedural programming languages. For someone learning their first programming language, this can be frustrating.
Flowcharts A Flowchart is a Visual Representation of an algorithm A Flowchart uses easy-to-understand symbols to represent actions on data and the flow of data Flowcharts aid in breaking down a problem into simple steps
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '08 17 Flowchart symbols
Chapter 2 - VB 2005 by Schneider- modified by S. Jane '08 18 Flowchart Symbols (continued)
Example Problem Statement: Obtain a number from the user, then display square of this number back to the user.