Algorithms Recipes for disaster?. Topics Covered Programming vs. problem solving What is an algorithm? Algorithm development common constructs (loops,

Slides:



Advertisements
Similar presentations
The LC-3 – Chapter 6 COMP 2620 Dr. James Money COMP
Advertisements

How Are Algorithms Developed?
COMPUTER PROGRAMMING I Understand Problem Solving Tools to Design Programming Solutions.
Chapter 1 - An Introduction to Computers and Problem Solving
8 Algorithms Foundations of Computer Science ã Cengage Learning.
ITEC113 Algorithms and Programming Techniques
Algorithms and Problem Solving-1 Algorithms and Problem Solving.
ECIV 301 Programming & Graphics Numerical Methods for Engineers Lecture 4 Programming and Software EXCEL and MathCAD.
Algorithms and Problem Solving. Learn about problem solving skills Explore the algorithmic approach for problem solving Learn about algorithm development.
Chapter 2: Algorithm Discovery and Design
Program Design and Development
1. By the end of this lecture you should be able … To describe in general terms how computers function To describe the fetch-execute cycle To explain.
1 Chapter 8 Designing Small Programs. 2 A ‘Procedure’ v A set of instructions which describe the steps to be followed in order to carry out an activity.
Chapter 1 Program Design
Problem Solving Chapter 2. What is an algorithm? n A solution to a problem that is: –Precise –Effective –Terminating.
Chapter 2: Algorithm Discovery and Design
Chapter 3 Planning Your Solution
The Program Design Phases
ALGORITHMS AND FLOW CHARTS 1 Adapted from the slides Prepared by Department of Preparatory year Prepared by: lec. Ghader Kurdi.
Algorithm & Flowchart.
Fundamentals of C programming
DCT 1123 PROBLEM SOLVING & ALGORITHMS INTRODUCTION TO PROGRAMMING.
Introduction to Programming Lecture Number:. What is Programming Programming is to instruct the computer on what it has to do in a language that the computer.
Simple Program Design Third Edition A Step-by-Step Approach
Invitation to Computer Science, Java Version, Second Edition.
Algorithms and their Applications CS2004 ( ) Dr Stephen Swift 1.2 Introduction to Algorithms.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 2: Flowcharts.
CSE 102 Introduction to Computer Engineering What is an Algorithm?
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Problem Solving Techniques. Compiler n Is a computer program whose purpose is to take a description of a desired program coded in a programming language.
C++ Programming Language Lecture 2 Problem Analysis and Solution Representation By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
BACS 287 Programming Logic 1. BACS 287 Programming Basics There are 3 general approaches to writing programs – Unstructured – Structured – Object-oriented.
ALGORITHM List of instructions for carrying out some process step by step. A sequence of instructions which has a clear meaning and can performed with.
Flowcharts and Algorithms. Review of Terms  A computer is a machine that can represent and manipulate data –Ultimately the data and the instructions.
Algorithms & Flowchart
First Steps in Modularization. Simple Program Design, Fourth Edition Chapter 8 2 Objectives In this chapter you will be able to: Introduce modularization.
PROGRAM DEVELOPMENT CYCLE. Problem Statement: Problem Statement help diagnose the situation so that your focus is on the problem, helpful tools at this.
`. Lecture Overview Structure Programming Basic Control of Structure Programming Selection Logical Operations Iteration Flowchart.
The Hashemite University Computer Engineering Department
8.1 8 Algorithms Foundations of Computer Science  Cengage Learning.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Algorithms and Pseudocode
ALGORITHMS AND FLOWCHARTS. Why Algorithm is needed? 2 Computer Program ? Set of instructions to perform some specific task Is Program itself a Software.
STEP 3- DEVELOP AN ALGORITHM At this stage we break down the problem into simple manageable steps so that they can be handled easily.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
How Computers Solve Problems Computers also use Algorithms to solve problems, and change data into information Computers can only perform one simple step.
Introduction to Problem Solving Programming is a problem solving activity. When you write a program, you are actually writing an instruction for the computer.
| MSC 8102:PROGRAMMING CONCEPTS By Vincent Omwenga, PhD. 1.
Victoria Ibarra Mat:  Generally, Computer hardware is divided into four main functional areas. These are:  Input devices Input devices  Output.
Algorithms and Flowcharts
Program design Program Design Process has 2 phases:
Learning outcomes 5 Developing Code – Using Flowcharts
Understand Problem Solving Tools to Design Programming Solutions
INTRODUCTION TO PROBLEM SOLVING
ALGORITHMS AND FLOWCHARTS
Understand Problem Solving Tools to Design Programming Solutions
Algorithms An algorithm is a sequence of steps written in the form of English phrases that specific the tasks that are performed while solving the problem.It.
Lecture 2 Introduction to Programming
Introduction To Flowcharting
ALGORITHM Basic CONCEPTS of Basic Concepts of Algorithm
Introduction to Computer Programming
Programming Fundamentals
Unit# 9: Computer Program Development
Problem Solving Techniques
Lesson 2 Programming constructs – Algorithms – Scratch – Variables Intro.
Introduction to Algorithms and Programming
Algorithms and Problem Solving
Basic Concepts of Algorithm
Presentation transcript:

Algorithms Recipes for disaster?

Topics Covered Programming vs. problem solving What is an algorithm? Algorithm development common constructs (loops, conditionals, etc) Representing algorithms flowcharts pseudocode

Programming vs. Problem Solving Two very distinct tasks! Problem solving: developing a solution to a problem result is an algorithm or a plan for the solution Programming: converting the solution into a computer readable form (C, assembly, VB, Delphi, etc..) Programmers are cheap – problem solvers are not! eg, architect, engineer vs. bricklayers, carpenters, etc..

What is an Algorithm? Formal definition: An algorithm is a step-by-step procedure for accomplishing a stated task A recipe for solving a problem History: derived from 9 th century mathematician Abu Abdullah Muhammad ibn Musa al-Khwarizmi initially referred only to solving arithmetic problems (multiplication, division, etc) known as algorism definition expanded to include all mathematical procedures formalised by Alan Turing in 1936 (Turing Machine) the foundation of computer science

Algorithm Examples Humans solve problems intuitively without usually formalising the steps involved Consider: passing this course making a cup of tea multiplying two numbers determining if a number is prime finding the largest number in a list

Properties of Algorithms Finiteness: must eventually terminate Definiteness: each step is precisely defined, there is no ambiguity Input: information which is required in order to start the algorithm Output: result of the algorithm, with some relation to the input Effectiveness: should be able to be emulated manually (paper and pencil)

Types of Algorithms Iterative or Recursive: recursive algorithms call themselves, iterative algorithms do not Serial or Parallel: one instruction at a time or multiple instructions Deterministic or Stochastic: identical execution for identical input or some randomness involved Exact or Approximate: is the solution exact or merely good enough Complexity: how long the algorithm takes to run (for a given size input)

Algorithmic Constructs There are a number of standard ‘tools’ used when developing algorithms variables statements loops conditional statements modules These are the same actions we use for every activity you just don’t realise it!

Variables Variables are areas of storage used to keep track of data in an algorithm, the size or type of the data in a variable is not necessarily defined, but should be consistent can hold numbers, letters, words, etc.. Each variable requires a distinct name should be related to what the variable is holding, eg. age, iq, student_number, etc The equivalent of writing something down for later when manually running through an algorithm, it is helpful to use labelled boxes for variables Input/Output values are also considered variables Variables are the only data that is available to the algorithm!

Statements Statements are the building blocks of algorithms each statement is an instruction to perform an action set or change a variable’s value perform calculations write data to the screen, read from keyboard call another module Statements should be either: simple, atomic actions that are easily understood module calls which are defined elsewhere ALL relevant information needs to be included in the statement! Read a, b, c from keyboard SET r1 = (-b +  b 2 -4ac) / 2 Print r1 to screen

Conditional Statements Most non-trivial algorithms are not simply a sequence of statements require some decisions at some point! Conditional statements are used to branch the execution of an algorithm logical expressions are used to make such decisions IF ( something is true ) THEN do something ELSE do something else

Loops and Repetition Most algorithms require some actions to be repeated numerous times, either: a fixed number of times, OR until a condition is met (or no longer met) Such constructs are known as loops, and are a powerful tool in algorithm design

Representing Algorithms Algorithms can be represented in numerous ways plain English descriptions graphically (flowcharts, Nasi-Schneiderman diagrams, etc) structured English (pseudocode) Each approach has advantages and disadvantages ease of understanding ambiguity speed of creation ease of conversion to actual source code

Plain English Descriptions As name implies, a plain language description of the algorithm Usually in the form of an English paragraph Advantages: easy for layperson to understand quick to create Disadvantages: often ambiguous does not convert easily to code no well defined structure, so large variations between authors

Graphical Representations Visual representation of algorithm each structure has a set of symbols to represent it Flowcharts are one of the earliest and most popular forms boxes = actions diamonds = decisions arrows = program flow Advantages: some people find visual approach easy to understand unambiguous Disadvantages: can be large and difficult to create direct conversion to code is possible, but not always simple

Structured English Known as pseudocode A cross between English and program language rules, conventions and structures similar to programming languages statements are written in plain English statements Advantages: very easy to convert to program code unambiguous in most cases Disadvantages: not as easy to read as plain language can be ambiguous if written poorly

Algorithm Representation Examples Simple directions to a destination: Plain English: Go straight on to the traffic lights, turn left and take the third turning on the right. If the red gate is open then go through it, otherwise go through the green gate.

Algorithm Representation Examples Flowchart:

Algorithm Representation Examples Pseudocode: Go straight on to the traffic lights Turn left Take the third turning on the right IF the red gate is open THEN go through the red gate ELSE go through the green gate END IF

Examples Reconsider these problems: passing this course making a cup of tea multiplying two numbers determining if a number is prime finding the largest number in a list Now define a solution in each representation…

Breaking Down a Problem Problems are often too complicated to be completely defined in a single algorithm In such cases, some steps are defined only in general terms add milk to tea study determine factors of x Such tasks are not simple cannot be done in one action – one line of code This is OK, provided that they are expanded upon elsewhere This is known as a modular approach, and is how all large software systems are developed each module is designed and built separately, and assumes the other modules will do their job…

Modular Algorithm Examples Traditional program breakdown: get input process input display output For very simple problems all such steps can be done in one algorithm For more complex tasks, each step becomes one or more modules eg, solving quadratic equation: Read a, b, csimple, not a module Calculate r1, r2NOT simple, requires a module Display r1, r2simple, not a module

Defining Modules When modules have been identified, it is necessary to define them What is required? inputs outputs their own algorithm! Note that the calling module doesn’t need to know the algorithm but does need to know the inputs and outputs!!

“Top-Down” Design Systematic method of algorithm development start at a high, conceptual level specify operations in general terms identify sub-modules inputs, outputs repeat for each sub-module Can benefit from re-use of some common modules

Top Down Example Missile guidance system Basic algorithm: inputs: target coordinates (t_location) while ( haven’t hit target yet ) get instrument readings calculate position and direction calculate required trajectory calculate fin/motor adjustments implement adjustments end while Clearly, all statements here require some refinement…

Top Down Example Get_instrument_readings outputs: gps, speed, altitude, gyro activate gps get gps information read speed read altitude from altimeter read spin from gyro RETURN ( gps, speed, altitude, spin ) Calculate_position_and_velocity input: gps, speed, altitude, spin output: position, velocity Algorithm: who knows?!? (somebody else’s job!)

Top Down Example Calculate_trajectory inputs: position, velocity, t_location outputs: req_trajectory Calculate_fin_motor input: position, velocity, req_trajectory output: fin_adjust, motor_adjust Implement_adjustments input: fin_adjust, motor_adjust

Top Down Example Missile Guidance Get_instcalc_poscalc_trajcalc_fin_adjstimp_fin_adjst ………

When to Break Down Problem? In general, an algorithm (module) should be no more than 20 lines long >20, probably should be modularised not a hard and fast rule, but a guideline – many ‘simple’ modules may contain hundreds of lines Any moderately lengthy section of an algorithm which is used a number of times should be made into a module Modules should be logical! well-defined purpose (make sense independently) the module should be cohesive not just to break apart a long module! Modules should be able to be tested independently! must have a good input/output relationship testing is done before integration occurs

Types of Modules Some languages define different module types Procedures: perform a specific task do NOT provide an output can take inputs however Functions: return at least one output in general, their purpose is to calculate those values, and nothing else can of course take input as well C makes no distinction between these types of modules – all are called functions