48
Lesson 5 Essence of Programming 2007/09/28

Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

Embed Size (px)

Citation preview

Page 1: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

Lesson 5

Essence of Programming

2007/09/28

Page 2: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

2

index

Reviewing computer hardware Introduction of programming language Algorithms Structured programming The software development progress

Page 3: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

3

Computer Hardware

Computers are constructed from physical components referred to as hardware

Hardware facilitates the storage and processing of data under the direction of a file system

Computer hardware does not store data using the same symbols that humans do

Page 4: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

4

Bits and Bytes The smallest and most basic data item in a computer is a

bit Open or closed switch 0 or 1

The grouping of 8 bits to form a larger unit is referred to as a byte Can represent any one of 256 distinct patterns

The collections of patterns consisting of 0s and 1s used to represent letters, single digits, and other single characters are called character codes

computer hardware

Page 5: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

5

Components

computer hardware

Page 6: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

6

Main Memory Unit (RAM) Stores data and instructions as sequence of bytes A program must reside in main memory if it is to operate

on the computer Combines 1 or more bytes into a single unit, referred to

as a word Constructed as random access memory, or RAM

Every section of memory can be accessed randomly as quickly as any other section

Volatile: data is lost when power is turned off Size is usually specified in bytes (MB or GB)

computer hardware

Page 7: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

7

Central Processing Unit (CPU) Control unit: directs and monitors the overall operation of the

computer Keeps track of where the next instruction resides Issues the signals needed to both read data from and write

data to other units in the system Executes all instructions

Arithmetic and Logic Unit (ALU): performs all of the computations, such as addition, subtraction, comparisons, and so on,

CPUs are constructed as a single microchip, which is referred to as a microprocessor

computer hardware

Page 8: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

8

Input/Output Unit

The input/output (I/O) unit provides access to the computer, allowing it to input and output data

It is the interface to which peripheral (外围的 )

devices, such as keyboards, console screens, and printers, are attached

computer hardware

Page 9: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

9

Secondary Storage

Used as permanent storage for programs and data Magnetic tape, magnetic disks, and CD-ROMs

Direct access storage device (DASD): allows a computer to read or write any one file or program independent of its position on the storage medium Magnetic hard disk consists of rigid platters that

spin together on a common spindle Initially, the most common magnetic disk storage

device was the removable floppy disk

computer hardware

Page 10: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

10

Magnetic Hard Disk

扇区

柱面

computer hardware

Page 11: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

11

Programming Languages

Computer program: data and instructions used to operate a computer and produce a specific result A program or set of programs is called software

Programming: writing instructions in a language that the computer can respond to and that other programmers can understand

Programming language: set of instructions that can be used to construct a program

Page 12: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

12

Machine Language Executable program: program that can operate a computer Executable programs are written with binary numbers, which

is a computer’s internal language (machine language) An example of a simple machine language program

containing two instructions is:

11000000000000000001000000000010

11110000000000000010000000000011 Opcode is short for operation code; tells the computer the

operation to be performed

Programming Languages

Page 13: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

13

Assembly Language

Assembly language: uses the substitution of word-like symbols for the opcodes, and decimal numbers and labels for memory addresses

LOAD first

ADD second

MUL factor

STORE answer

load R1, [R0]

load R2, new_number

ADD R2, R1

jmp address

Programming Languages

Page 14: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

14

Assembly Language (continued)

Programming Languages

Page 15: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

15

The level of languages

11000000,00000000, 00010000,00000010

opcode

Machine language

Assembly language

ADD op1 op2Language specification

rate = rate +1;

High level language

compiler

use instructions that are directly tied to one type of computer.

Source code

Object code

Programming Languages

Page 16: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

16

High - to - Low Level Languages (compile & link)

Programming Languages

Page 17: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

17

Data pipe line

1+ rate dep * R_step1

depositrateoutput

rateR_step1 deposit output

R_step1

registersControl

unit

What is a program (physically)

A set of instructions that can make the computer run chronologically.

rate = rate +1;result = rate * deposit

a program

Page 18: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

18

Program is a white box for data processing (logically)

a program

Page 19: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

19

Coding an algorithm: construct a white box step by step

Algorithms

Page 20: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

20

Algorithms (arithmetic progression)Algorithms

Page 21: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

21

Algorithms Algorithm: specific steps required to produce a

desired resultSet n equal to 100Set a equal to 1Set b equal to 100Calculate sum = n(a+ b)/2Display the sum

When English phrases are used to describe an algorithm, the description is called pseudocodeInput the three numbers into the computerCalculate the average by adding the numbers and dividing the

sum by threeDisplay the average

Algorithms

Page 22: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

22

Algorithms and flowchart

A formula or set of steps for solving a particular

problem. To be an algorithm, the set of rules

must be unambiguous and have a clear stopping

point.

Algorithms can be expressed in any language,

from natural languages like English or Chinese.

Algorithms

Page 23: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

23

flow chart

A flow chart is an organized combination of shapes, lines, and text that graphically illustrates a process or structure.

Algorithms

Page 24: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

24

Flowchart Symbols

Algorithms

Page 25: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

25

Flowchart for calculating the average of three numbers

Algorithms

Page 26: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

26

structured programming

A technique for organizing and coding computer programs:a hierarchy of modules is used,each having a single entry and a single exit p

oint, control is passed downward through the struct

ure without unconditional branches to higher levels of the structure.

structured programming

Page 27: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

27

A module must accept data, process the data, and produce a result

structured programming

Page 28: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

28

Structured programming

A well-designed program is built using modules

structured programming

Page 29: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

29

First-level structure diagram

structured programming

Page 30: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

30

Second-level refinement structure diagram

structured programming

Page 31: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

31

The Software Development ProcessPhase I: Specify the program’s requirements

Phase II: Design and development

Step 1: Analyze the problem

Step 2: Select an overall solution algorithm

Step 3: Write the program

Step 4: Test and correct the program

Phase III: Documentation

Phase IV: Maintenance

Page 32: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

32

Phase I: Specify the Program’s Requirements It is impossible to construct a successful progra

m for a problem that is not fully understood.

Software Development process

Page 33: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

33

Phase II: Design and Development

Step 1: Analyze the problem. You must understand:

The outputs that must be produced

The input data required to create the desired

outputs

The formulas relating the inputs to the outputs

Step 2: Select an overall solution algorithm

Software Development process

Page 34: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

34

Phase II: Design and Development (continued)

Software Development process

Page 35: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

35

Phase II: Design and Development (continued) For larger programs you will have to refine the initial

algorithm and organize it into smaller algorithms, with specifications for how these smaller algorithms will interface with each other First-level structure diagram for an algorithm is the

first attempt at a structure for a solution algorithm Top-down algorithm development starts at the

topmost level and proceeds to develop more and more detailed algorithms as it proceeds to the final set of algorithms

Software Development process

Page 36: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

36

Phase II: Design and Development (continued)

Software Development process

Page 37: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

37

Phase II: Design and Development (continued)

Software Development process

Page 38: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

38

Phase II: Design and Development (continued) The sooner you start programming an

application, the longer it usually takes to debug and complete.

Software Development process

Page 39: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

39

Phase II: Design and Development (continued) Step 3: Write the program (or code the algorithm)

Sequence structure defines the order in which instructions are executed by the program

Selection structure provides the capability to make a choice between different instructions, depending on the result of some condition

Repetition structure, also called looping or iteration, provides the ability for the same operation to be repeated based on the value of a condition

Invocation

Software Development process

Page 40: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

40

Phase II: Design and Development (continued) Step 4: Test and correct the program

A program error is called a bug Testing attempts to ensure that a program works

correctly and produces meaningful results If you find an error, initiate debugging: locating,

correcting, and verifying the correction Develop a set of test data that determines whether the

program gives correct answers The tests should examine every possible situation

under which a program will be used

Software Development process

Page 41: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

41

Page 42: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

42

Phase III: Documentation Six documents for every problem solution:

1. The requirements statement2. A description of the algorithms that were coded3. Comments within the code itself4. A description of modification and changes made over

time5. Sample test runs, which include the inputs used for

each run and the output obtained from the run6. A user’s manual, which is a detailed explanation of

how to use the program

Software Development process

Page 43: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

43

Case Study: circumference of circle

Void main() Statements; {compound statements; } //comments

Software Development process

Page 44: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

44

Phase IV: Maintenance

How easily a program can

be maintained (corrected,

modified, or enhanced) is

related to the ease with

which the program can be

read and understood

Software Development process

Page 45: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

45

Backup Making and keeping backup copies of your work when

writing a program is critical Not part of the formal software development process

Backup is unimportant if you don’t mind starting all over again

Many organizations keep at least one backup on site where it can be easily retrieved, and another backup copy either in a fireproof safe or at a remote location

Software Development process

Page 46: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

46

What is a Programming Language for (conclusion) Programming is an explanatory activity

 To yourself, now and in the future. To whoever has to read your code. To the compiler, which has to make it run.

Explanations require language To state clearly what is going on. To communicate ideas over space and time.

Page 47: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

47

How to make a nice program

Analysis the problem Correct algorithm Nice structure Coding with Comments Debugging documentation

Page 48: Lesson 5 Essence of Programming 2007/09/28. 2 index Reviewing computer hardware Introduction of programming language Algorithms Structured programming

48

The assignment:

Finish and hand in personal web page (10.8)

Read the material recommended in Chapter one. Include the exercises.