1 / 19

Introduction to Computer Systems

Introduction to Computer Systems. Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2014 Week 8b: Pseudo Code and Algorithms. Algorithms.

ali-gentry
Download Presentation

Introduction to Computer Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Autumn 2014 Week 8b: Pseudo Code and Algorithms Birkbeck College, U. London

  2. Algorithms • Definition: an algorithm is an ordered set of unambiguous, executable steps that defines a terminating process. • The steps are often called instructions. • Termination is by a special instruction: halt. Brookshear section 5.1

  3. Terminology • Programming language: system for representing algorithms in a human readable form suitable for conversion to machine code. • Program: representation of an algorithm in a programming language. • Pseudo code: system for the informal representation of algorithms. • Hardware: device to carry out the steps or instructions in a program. Brookshear sections 5.1 and 5.2

  4. Representation of an Algorithm The same algorithm can be represented in many different ways: • F = (9/5)C+32 • F <- (9*C)/5+32 • To obtain F multiply C by (9/5) and then add 32. • Lookup table for Fahrenheit and Centigrade Brookshear Section 5.1

  5. Program 1 for XOR Input: binary digits a,b Output: a XOR b • If a==0 and b==0 Return 0 • If a==0 and b==1 Return 1 • If a==1 and b==0 Return 1 • If a==1 and b==1 Return 0 • Halt Birkbeck College, U. London

  6. Program 2 for XOR Input: binary digits a,b Output: a XOR b • If a==b Return 0 • Else Return 1 • Halt Birkbeck College, U. London

  7. Program 3 for XOR Input: binary digits a,b Output: a XOR b • Convert a to the integer ia • Convert b to the integer ib • ic=remainder on dividing ia+ib by 2 • Return ic • Halt Birkbeck College, U. London

  8. Pseudo Code for Algorithm Representation • Very useful informal representation of algorithms. • Preferable to using program code when designing an algorithm. • Basic structures (assignment, loops, arrays …) are similar across many programming languages. Brookshear section 5.2

  9. Pseudo Code: assignment • General form name <- expression • Examples funds <- current + savings x <- 10 Brookshear, Section 5.2

  10. Pseudo Code: conditional selection • General form if(condition)then(activity) else(activity) • Example if (year is leap year) then d <- total divided by 366 else d <- total divided by 365 • Key or reserved words in bold Brookshear section 5.2

  11. Pseudo Code: repeated execution • General form while(condition)do(activity) • Example while (tickets remain) do (sell a ticket) Brookshear section 5.2

  12. Pseudo Code: indentation • if (not raining) then (if (temperature = hot) then (go swimming) else (play golf) ) else (watch television) • if (not raining) then (if (temperature=hot) then (go swimming) else (play golf)) else (watch television) Brookshear section 5.2

  13. Procedure • Definition: set of instructions which can be used as a single unit in different contexts. • A procedure call contains the name of the procedure and any data supplied to the procedure • A result calculated by the procedure can be obtained using a return statement. Brookshear, Section 5.2

  14. Example of a Procedure procedure temp(c) f=(9/5)*c+32; return f; endProcedure f1=temp(34) Definition of a procedure Procedure call Birkbeck College

  15. Pseudo Code: procedures • General type procedurename • Examples procedure helloWorld //no parameters procedure sort (List) // parameter List Brookshear Section 5.2

  16. Pseudo Code: alternatives to brackets • procedurename (activity) endProcedure • while (condition) do (activity) endDo endWhile Brookshear Section 5.2

  17. Exercise 1 • Write an algorithm in pseudo code to carry out the following task: input: a 1-dimensional array A of integers output: the integer -1 if A[i]≥ A[i+1] for 0≤i≤Length[A]-2, otherwise the least integer j such that A[j]<A[j+1]. Birkbeck College

  18. Exercise 2 • Design an algorithm for finding all the factors of a positive integer. For example, in the case of the integer 12, your algorithm should report the values 1,2,3,4,6 and 12. Brookshear Ch 5 review problems 11

  19. Exercise 3 • The following is an addition problem in decimal notation. Each letter represents a different digit. Which letter does each digit represent? XYZ + YWY ==== ZYZW Brookshear Ch 5 review problems 15

More Related