17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.

Slides:



Advertisements
Similar presentations
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Advertisements

15 October 2013Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
What is an algorithm? Informally: An Algorithm is a step by step method for solving a problem. It’s purpose is to break a larger task down so that each.
Program Design and Development
1 Section 2.5 Integers and Algorithms. 2 Euclidean algorithm for finding gcd Where L is a larger number, and S is a smaller number, to find gcd(L,S):
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
© by Kenneth H. Rosen, Discrete Mathematics & its Applications, Sixth Edition, Mc Graw-Hill, 2007 Chapter 3 (Part 3): The Fundamentals: Algorithms, the.
Introduction to Computer Systems
Copyright © 2015 Pearson Education, Inc. Chapter 5: Algorithms.
25 November 2014Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Goals of Course Introduction to the programming language C Learn how to program Learn ‘good’ programming practices.
Chapter 3 Section 1 Number Representation Modern cryptographic methods, unlike the classical methods we just learned, are computer based. Representation.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming (in C++) Loops Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC.
6 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Chapter 5 Algorithms Introduction to computer, 2nd semester, 2010/2011 Mr.Nael Aburas Faculty of Information Technology.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.
29 September 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Computer Programming Control Structure
Computer Science: An Overview Eleventh Edition
17 November 2015Birkbeck College1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems
Python Functions : chapter 3
13 October 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
3 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
8 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
22 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Computer Systems
12 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
24 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Application: Algorithms Lecture 19 Section 3.8 Tue, Feb 20, 2007.
26 February 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Quiz1 Question  Add Binary Numbers a) b) c) d) e) none
Low-Level Programming Languages, Pseudocode and Testing Chapter 6.
1 Computing Functions with Turing Machines. 2 A function Domain Result Region has:
11 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Introduction to Computer Systems
Introduction to Programming
Introduction to Programming
Algorithms, Part 1 of 3 The First step in the programming process
Introduction to Programming
Computer Science: An Overview Eleventh Edition
Lecture 7: Repeating a Known Number of Times
FIGURE 4-10 Function Return Statements
CS1001 Programming Fundamentals 3(3-0) Lecture 2
CSE 504 Discrete Mathematics & Foundations of Computer Science
Introduction to Programming
Introduction to Programming
Introduction to Programming
University of Gujrat Department of Computer Science
Repetition and Loop Statements
FIGURE 4-10 Function Return Statements
Algorithm Discovery and Design
Computing Functions with Turing Machines
Introduction to Programming
FIGURE 4-10 Function Return Statements
Introduction to Programming
Introduction to Programming
Introduction to Programming
Application: Algorithms
Application: Algorithms
FIGURE 4-10 Function Return Statements
Introduction to Programming
Introduction to Programming
Presentation transcript:

17 November 2015Birkbeck College, U. London1 Introduction to Computer Systems Lecturer: Steve Maybank Department of Computer Science and Information Systems Autumn 2015 Week 8a: Pseudo Code and Algorithms

17 November 2015Brookshear section 5.12 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.

Device An algorithm requires a device which understands the instructions CPU: machine code CPU+compiler: high level language Mathematica: 17 February 2017Brookshear Section 5.13

17 November 2015Brookshear sections 5.1 and 5.24 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.

17 November 2015Brookshear Section 5.15 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

17 November 2015Birkbeck College, U. London6 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

17 November 2015Birkbeck College, U. London7 Program 2 for XOR Input: binary digits a,b Output: a XOR b 1. If a==b Return 0 2. Else Return 1 3. Halt

17 November 2015Birkbeck College, U. London8 Program 3 for XOR Input: binary digits a,b Output: a XOR b 1. Convert a to the integer ia 2. Convert b to the integer ib 3. ic=remainder on dividing ia+ib by 2 4. Return ic 5. Halt

17 November 2015Brookshear section 5.29 Pseudo Code for Algorithm Representation Very useful informal representation of algorithms. Easier than using program code when designing an algorithm. Basic structures (assignment, loops, arrays …) are similar across many programming languages.

17 November 2015Brookshear, Section Assignment General form name = expression Examples q = 3 funds = Balance + savings

17 November 2015Brookshear section Conditional Selection General form if (condition): activity else: activity  Example if (year is leap year): dailyTotal = total/366 else: dailyTotal = total/365

Nested if Statement if (not raining): if(temperature == hot): go swimming else: play golf else: watch television 17 February 2015Brookshear Section 5.212

17 November 2015Brookshear section Repeated Execution  General form while (condition): activity  Example while (tickets remain): sell a ticket

Function Definition: set of instructions which can be used as a single unit in different contexts. Example def greetings(): count = 3; while (count > 0): print(‘Hello’) count = count-1 17 November 2015Brookshear, Section 5.214

Parts of a Function def greetings(): count = 3; while (count > 0): print(‘Hello’) count = count-1 17 November 2015Brookshear, Section # keyword: def # name: greetings # parameters: none # header: def greetings(): # body: code apart from the header # return value: none (print does not count!)

Example of a Function def temp(c): f = (9/5)*c+32 return f 17 November 2015Birkbeck College16 # keyword: def # name: temp # parameters: c # header: def temp(c) # body: ‘f=(9/5)*c+32’ and ‘return f’ # return value: value of f

Function Call c=0 while (c <= 100) f = temp(c) c = c+1 print(f) 17 November 2015Birkbeck College17 # The variable f in the while loop is different # from the variable f in the definition of the # function temp, even though the two variables # have the same name

Which Numbers are Printed? last = 0 current = 1 while (current < 100): print(current) temp = last last = current current = last+temp 17 November 2015Brookshear Ch 5, review problem 2218

Exercise 1 The Euclidean algorithm finds the greatest common divisor of two positive integers x and y by the following process: As long as x and y are both not zero divide the larger number by the smaller. Replace the larger value with the remainder. When x or y has the value 0 the value of the other variable is the GCD. 17 November 2015Brookshear Section 5.219

Example 2 A year n is a leap year if n is divisible by 400 or if n is divisible by 4 but not by 100. In all other cases n is not a leap year Write a pseudo code version of a function leapYear that takes an integer n as a parameter and returns True if n is a leap year and False otherwise. 17 November 2015Brookshear Ch 5 review problem 1220

Exercise 3 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]. 17 November 2015Birkbeck College21

Exercise 4 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 November 2015Brookshear Ch 5 review problems 1122