Top Banner
1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm
62

1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

Dec 22, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

1

TEL 104 / MKK Fundamental Programming:

Lecture 2Computer Systems Overview

&Introduction to Algorithm

Page 2: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

2

AlgorithmAlgorithm: A set of instructions describing how to do a task (or process)

eureka!

Program Program

Page 3: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

3

How the program works inside the computer

Page 4: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

4

Transistor

Base

Collector

Emitter

“semi-conductor”

Binary digit or “bit”:0 off1 on

Page 5: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

5

Transistor (cont)

Base

Collector

Emitter

off : 0

Page 6: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

6

Transistor

Base

Collector

Emitter

on : 1

Page 7: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

7

Transistor

Base

Collector

Emitter

Modern-day “chips” (about 3 x 3 mm in size) can contain millions of transistors

Page 8: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

8

Gates • Gate: a group of transistors

• Gates are switches that distinguish between two electrical voltages:– Current is low => 0– Current is high => 1

• Types:

AND Gate OR Gate NOT Gate

Page 9: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

9

A B

0 0

0 1

1 0

1 1

A AND B

A AND B

A

B

A

BA AND B

Example: AND Gate

Page 10: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

10

Gates and Boolean Algebra

A B

0 0

0 1

1 0

1 1

AND Gate

A AND B A OR B

OR Gate

Page 11: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

11

Gates and Boolean Algebra (cont)

A

0

1

NOT A

NOT Gate

Page 12: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

12

Gates and Boolean Algebra (cont)

A =

A sequence of bits at a time:

1 1 0 0 1 1 0 1

B = 0 1 1 0 0 1 1 0

A AND B =

• Most PCs do 32 bits at a time (“32-bit machines”), others as many as 128 bits at a time

Page 13: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

13

• Gates are the basic building blocks of computers

0 or 1

Page 14: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

14

Hardware Components of a Typical Computer

PeripheralDevices

Central ProcessingUnit (CPU)

Memory

• "Buses" allow components to pass data to each other

Page 15: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

15

Hardware Components of a Typical Computer -- CPU

Central Processing Unit (CPU) • performs the basic operations• consists of two parts:

– Arithmetic / Logic Unit (ALU) - data manipulation– Control Unit - coordinate machine’s activities

MemoryPeripheralDevices

Central ProcessingUnit (CPU)

Page 16: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

16

Hardware Components of a Typical Computer -- Memory

Main Memory • holds programs and data• stores bits in fixed-sized chunks: “word” (8, 16, 32, or

64 bits)• each word is stored in a cell, which has a unique address• the cells can be accessed in any order =>

random-access memory or “RAM”

PeripheralDevices

Central ProcessingUnit (CPU)

Memory

Page 17: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

17

Hardware Components of a Typical Computer -- Peripherals

PeripheralDevices

Central ProcessingUnit (CPU)

Memory

Peripheral devices –• communicate with the outside world• store data long term

Page 18: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

18

Hardware Components of a Typical Computer – Peripheral Devices that

Communicate with the Outside World

PeripheralDevices

Central ProcessingUnit (CPU)

Memory

• Input/Output (I/O)– Input: keyboard, mouse, microphone, scanner,

sensors (camera, infra-red), punch-cards– Output: video, printer, audio speakers, etc

• Communication– modem, ethernet card

Page 19: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

19

Modes of communication

• Parallel communication: – all the bits are transferred at the same time – each bit on a separate line

01

• Serial communication: – one bit at a time

10110001

Page 20: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

20

Introduction to Algorithm

Page 21: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

21

7 Steps in Program Development

1. Define the problem• The inputs• The outputs• The processing steps to produce the required outputs

2. Outline the solution• The major processing steps involved• The subtask (if any)• The user interface (if any)• The major control stuctures (e.g. Repetition loops)• The major variables and record structures• The mainline logic

3. Develop the outline into an algorithm

Page 22: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

22

7 Steps in Program Development (cont)

4. Test the algorithm for correctness

5. Code the algorithm into a specific programming language

6. Run the program on the computer

7. Document and maintain the program

Page 23: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

23

How do we solve problems?

• We "just do"

• Guesswork-and-luck

• Trial-and-error

• Experience (possibly someone else's)

• "Scientifically"

Page 24: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

24

0100111010110010101010101001010101010100110010101010101001011010011101010101010010010111010011110101010111110101010001101…

sterilize(saw,alcohol);raise_hammer();lower hammer(fast);start(saw);/* etc. etc. */

Patient has elevated pressure in anterior parietal lobe

The Problem-solving Process

Problem

specification

Algorithm

Program

Executable (solution)

Design

Implementation

Compilation

"Doctor, my head hurts"

Analysis

1. Sterilize cranial saw2. Anaesthetize patient3. Remove top of skull4. Get the big spoon...5. etc., etc.

Page 25: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

25

sterilize(saw,alcohol);raise_hammer();lower hammer(fast);start(saw);/* etc. etc. */

The Problem-solving Process

Problem specification

Algorithm

Program

Executable (solution)

Analysis

Design

Implementation

Compilation

"Doctor, my head hurts"

Patient has elevated pressure in anterior parietal lobe.

1. Sterilize cranial saw2. Anaesthetize patient3. Remove top of skull4. Get the big spoon...5. etc., etc.

010011101011001010101010100101010101010011001010101010100101101001110101010101001001011101001111010101011111010101000110100001101...

Page 26: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

26

The Problem-solving Process

Problem specification

Algorithm

Program

Executable (solution)

Analysis

Design

Implementation

Compilation

Page 27: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

27

• A sequence of instructions specifying the steps required to accomplish some task

• Named after: Muhammad ibn Musa al-Khwarizmi

of Khowarezm (now Khiva in Uzbekistan)

Circa 780-850 C.E. (Common Era)

Algorithm

Page 28: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

28

Algorithm –History

Muhammad ibn Musa Al-Khwarizmihttp://www-groups.dcs.st-andrews.ac.uk/~history/Mathematicians/Al-Khwarizmi.html

• Book on arithmetic:– Hindu numeration, decimal numbers, use of zero,

method for finding square root– Latin translation (c.1120 CE): “Algoritmi de

numero Indorum”

• Book on algebra– Hisab al-jabr w’al-muqabala

Page 29: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

29

• A sequence of instructions describing how to do a task

Algorithm – Working Definition

[As opposed to actually executing

the instructions]

Page 30: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

30

Algorithm -- Examples• A cooking recipe• Assembly instructions for a model• The rules of how to play a game• VCR instructions• Directions for driving from A to B• A knitting pattern• A car repair manual• Recipe for Almond and honey slice

Page 31: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

31

Almond and Honey Slice

1/2 quantity Shortcrust Pastry

185 g unsalted butter

100 g castor sugar

5 tablespoons honey

50 ml cream

50 ml brandy or any other liqueur or spirit

300 g flaked almonds

Preheat oven for 200° C

Line a 30 cm 20 cm baking tray with baking paper, and then with pastry

Bake blind for 20 minutes, then remove weights and foil

Turn oven up to 220° C.

Bring remaining ingredients to a boil, stirring.

Spread evenly over pastry.

Bake until topping is bubbling and has caramelised evenly, about 15 minutes.

Cool before cutting into fingers or squares.

From: Stephanie Alexander, The Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.

Page 32: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

32

Almond and Honey Slice

1/2 quantity Shortcrust Pastry

185 g unsalted butter

100 g castor sugar

5 tablespoons honey

50 ml cream

50 ml brandy or any other liqueur or spirit

300 g flaked almonds

Preheat oven for 200° C

Line a 30 cm 20 cm baking tray with baking paper, and then with pastry

Bake blind for 20 minutes, then remove weights and foil

Turn oven up to 220° C.

Bring remaining ingredients to a boil, stirring.

Spread evenly over pastry.

Bake until topping is bubbling and has caramelised evenly, about 15 minutes.

Cool before cutting into fingers or squares.

From: Stephanie Alexander, The Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.

Instructions are given in the order in which they are performed (“executed”)

Page 33: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

33

Correct Algorithm?Cut chicken into pieces and

brown the pieces on all sides in a casserole dish in hot olive oil.

Remove the chicken and to the juices in the casserole add garlic, onions and green peppers, and sauté until onion is golden.

Add bay leaf, whole tomatoes, and chicken broth.

When the broth boils add salt, saffron and rice.

Arrange chicken on rice, cover casserole and bake in a moderate oven (350°F) for 20 minutes or until the rice is tender.

Add beans and artichokes during last 10 minutes of cooking.

From: “Arroz Con Pollo” in The Margaret Fulton Cookbook, Hamlyn, Sydney, 1968.

Page 34: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

34

Cut chicken into pieces and brown the pieces on all sides in a casserole dish in hot olive oil.

Remove the chicken and to the juices in the casserole add garlic, onions and green peppers, and sauté until onion is golden.

Add bay leaf, whole tomatoes, and chicken broth.

When the broth boils add salt, saffron and rice.

Arrange chicken on rice, cover casserole and bake in a moderate oven (350°F) for 20 minutes or until the rice is tender.

Add beans and artichokes during last 10 minutes of cooking.

From: “Arroz Con Pollo” in The Margaret Fulton Cookbook, Hamlyn, Sydney, 1968.

Correct Algorithm?

Page 35: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

35

Cut chicken into pieces and brown the pieces on all sides in a casserole dish in hot olive oil.

Remove the chicken and to the juices in the casserole add garlic, onions and green peppers, and sauté until onion is golden.

Add bay leaf, whole tomatoes, and chicken broth.

When the broth boils add salt, saffron and rice.

Arrange chicken on rice, cover casserole and bake in a moderate oven (350°F) for 10 minutes.

Add beans and artichokes.

Cover, and bake for another 10 minutes or until rice is tender.

Correct Algorithm?

Page 36: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

36

From Algorithms to Programs

Problem

C, Java ProgramC, Java Program

AlgorithmAlgorithm: A sequence of instructions describing how to do a task (or process)

Page 37: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

37

Components of an Algorithm

• Variables and values

• Instructions

• Sequences

• Procedures

• Selections

• Repetitions

• Documentation

Page 38: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

38

Values

• Represent quantities, amounts or measurements

• May be numerical or alphabetical (or other things)

• Often have a unit related to their purpose

• Example:– Recipe ingredients

Page 39: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

39

Almond and Honey Slice

1/2 quantity Shortcrust Pastry

185 g unsalted butter100 g castor sugar5 tablespoons honey50 ml cream50 ml brandy or any other

liqueur or spirit300 g flaked almonds

Preheat oven for 200° C

Line a 30 cm 20 cm baking tray with baking paper, and then with pastry

Bake blind for 20 minutes, then remove weights and foil

Turn oven up to 220° C.

Bring remaining ingredients to a boil, stirring.

Spread evenly over pastry.

Bake until topping is bubbling and has caramelised evenly, about 15 minutes.

Cool before cutting into fingers or squares.

From: Stephanie Alexander, The Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.

Page 40: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

40

Almond and Honey Slice

1/2 quantity Shotcrust Pastry

185 g unsalted butter

100 g castor sugar

5 tablespoons honey

50 ml cream

50 ml brandy or any other liqueur or spirit

300 g flaked almonds

Preheat oven for 200° C

Line a 30 cm 20 cm baking tray with baking paper, and then with pastry

Bake blind for 20 minutes, then remove weights and foil

Turn oven up to 220° C.

Bring remaining ingredients to a boil, stirring.

Spread evenly over pastry.

Bake until topping is bubbling and has caramelised evenly, about 15 minutes.

Cool before cutting into fingers or squares.

From: Stephanie Alexander, The Cook’s Companion, Viking/Penguin, Ringwood, Victoria, 1996, p. 349.

Page 41: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

41

Variables

This jarcan contain

10 cookies

50 grams of sugar

3 slices of cake

etc.

ValuesVariable

• Are containers for values – places to store values

• Example:

Page 42: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

42

Restrictions on Variables

• Variables may be restricted to contain a specific type of value

Page 43: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

43

Components of an AlgorithmValues and Variables

• Instruction (a.k.a. primitive)

• Sequence (of instructions)

• Procedure (involving instructions)

• Selection (between instructions)

• Repetition (of instructions)

• Documentation (beside instructions)

Page 44: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

44

Instructions (Primitives)

• Some action that is simple...

• ...and unambiguous...

• ...that the system knows about...

• ...and should be able to actually do

Page 45: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

45

Instructions – Examples

• Take off your shoes

• Count to 10

• Cut along dotted line

• Knit 1

• Purl 2

• Sift 10 grams of arsenic

Directions to perform specific actions on values and variables.

Page 46: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

46

Instructions -- Application

• Some instructions can only be applied to a specific type of values or variables

• Examples:

Page 47: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

47

Instructions (Primitives) -- Recommendations

• When writing an algorithm, make each instruction simple and unambiguous

• Example:

Cut chicken into pieces and brown the pieces on all sides in a casserole dish in hot olive oil.

Cut chicken into pieces.

Heat olive oil in a casserole dish.

Brown the chicken pieces in the casserole dish.

Page 48: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

48

Instruction (Primitives)• When writing an algorithm, make the

instructions simple and unambiguous.

• Example:

Cut chicken into pieces and brown the pieces on all sides in a casserole dish in hot olive oil.

Cut chicken into pieces.

Heat olive oil in a casserole dish.

Brown the chicken pieces in the casserole dish.

A “sequence” of simple instructions

Page 49: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

49

Conclusion

• An algorithm must:– Be lucid, precise and unambiguous– Give the correct solution in all cases– Eventually end.

Page 50: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

50

Developing an algorithm

• To help the initial analysis, the problem should be divided into 3 separate components:

1. Input: a list of the source data provided to the problem

2. Output: a list of the outputs required

3. Processing: a list of actions needed to produce the required outputs.

Page 51: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

51

Example 1. Add three numbers

A program is required to read three numbers, add them together and print their total.

Page 52: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

52

Solution:1. Underline the nouns and adjectives used

in the specification establish the input, output component and any object that are required.

A program is required to read three numbers, add them together and print their total.

Page 53: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

53

• Defining diagram

Input Processing Output

Number1Number2Number3

total

Page 54: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

54

2. Underline the verbs and adverbs used in the specification establish the action required.

A program is required to read three numbers, add them together and print their total.

Page 55: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

55

• Defining diagram

Input Processing Output

Number1Number2Number3

Read three numbersAdd numbers togetherPrint total number

total

Page 56: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

56

3. Writing down the processing steps in an algorithm,

Read three numbersAdd numbers togetherPrint total number

Page 57: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

57

Example 2. Find average temperature

• A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.

Page 58: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

58

Step 1

• A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.

Page 59: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

59

• Defining diagram

Input Processing Output

Max_tempMin_temp

Avg_temp

Page 60: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

60

Step 2

• A program is required to prompt the terminal operator for the maximum and minimum temperature readings on a particular day, accept those readings as integers, and calculate and display to the screen the average temperature, calculated by (maximum temperature + minimum temperature)/2.

Page 61: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

61

• Defining diagram

Input Processing Output

Max_tempMin_temp

Prompt for temperaturesGet temperaturesCalculate average temperatureDisplay average temperature

Avg_temp

Page 62: 1 TEL 104 / MKK Fundamental Programming: Lecture 2 Computer Systems Overview & Introduction to Algorithm.

62

Assignment1. Compute mowing time

• A program is required to read from the screen the lenght and widht of a rectangular house block, and the lenght and width of the rectangular house that has been built on the block. The algorithm should then compute and display the mowing time required to cut the grass around the house, at the rate of two square metres per minute.