56
1 COMP 1020: Structured Programming (2) Instructor: Prof. Ken Tsang Room E409-R11 Email: kentsang @uic.edu.hk

COMP 1020: Structured Programming (2)

  • Upload
    anitra

  • View
    34

  • Download
    0

Embed Size (px)

DESCRIPTION

COMP 1020: Structured Programming (2). Instructor: Prof. Ken Tsang Room E409-R11 Email: kentsang @uic.edu.hk. TA information. Mr Fu Song-feng 付嵩峰 Room E409 Tel: 3620630 [email protected]. Web-page for this class. Watch for announcements about this class and - PowerPoint PPT Presentation

Citation preview

Page 1: COMP 1020: Structured Programming (2)

1

COMP 1020:

Structured Programming (2)

Instructor: Prof. Ken Tsang Room E409-R11

Email: [email protected]

Page 2: COMP 1020: Structured Programming (2)

2

TA information

Mr Fu Song-feng付嵩峰Room E409 Tel: 3620630

[email protected]

Page 3: COMP 1020: Structured Programming (2)

3

Web-page for this class

• Watch for announcements about this class and

• download lecture notes from

• http://www.uic.edu.hk/~kentsang/comp1020/comp1020.htm

• Or from this page:

http://www.uic.edu.hk/~kentsang/

Or from the Ispace

Page 4: COMP 1020: Structured Programming (2)

4

Tutorials

• One hour each week

• Time & place to be announced later (we need your input)

• More explanations

• More examples

• More exercises

Page 5: COMP 1020: Structured Programming (2)

5

How is my final grade determined?

• Lab works 15% • Project 20%• Quizzes 15%• Final Examination 50%

Page 6: COMP 1020: Structured Programming (2)

6

UIC Score System

Page 7: COMP 1020: Structured Programming (2)

7

Grade Distribution Guidelines

Page 8: COMP 1020: Structured Programming (2)

Reference

“The C Programming Language”by Brian W. Kernighan & Dennis M. Ritchie

Published by Prentice-Hall in 1988

ISBN 0-13-110362-8 (paperback)

8

Page 9: COMP 1020: Structured Programming (2)

• Practical C Programmingby Steve Oualline, O’Reilly&Associates

• Practical C++ Programmingby Steve Oualline, O’Reilly&Associates

C Programming: A Modern Approach by K.N. King

http://www.cprogramming.com/tutorial/c/lesson15.html

Reference books

9

Page 10: COMP 1020: Structured Programming (2)

Why do we have to learn programming?

• Big Data

• There is a lot of data around us, because– It is cheap to obtain & store

• It is difficult to capture, store, manage, share, analyze and visualize data without using computational tools.

10

Page 11: COMP 1020: Structured Programming (2)

11

The McKinsey Global Institute:

Page 12: COMP 1020: Structured Programming (2)

12

How Companies Learn Your Secrets

By CHARLES DUHIGGPublished: February 16, 2012

Page 13: COMP 1020: Structured Programming (2)

Lecture 1

Introduction: basic knowledge of computer hardware and software

Structured Programming Instructor: Prof. K. T. Tsang

13

Page 14: COMP 1020: Structured Programming (2)

Computer -- a machine for manipulating data according to a list of instructions known as a program.

Supercomputer, Mainframes

Workstations, Servers

Personal computers, LaptopsEmbedded computers: Cell phones, Digital

cameras, Fighter aircrafts, robots

14

Page 15: COMP 1020: Structured Programming (2)

Basic components of a computer

D = dataI = information

Memory(RAM)

CPU(Central Processing Unit)

Input devices(mouse,

keyboard, microphone)

Output devices(monitor,

printer,speaker)

Communication devices(modem,

internet connection)

D I

Storage Devices(hard-disk, DVD)

D D

D

15

Page 16: COMP 1020: Structured Programming (2)

CPU - central processing unitprocessor/central processor

The CPU is the brains of the computer, where most calculations take place and determines the power of the system.

In personal computers and small workstations, the CPU is housed in a single chip called a microprocessor. More powerful computers have more than one processors.

Typical components of a CPU are: • The arithmetic logic unit (ALU), which performs arithmetic and logical

operations. • The control unit (CU), which extracts instructions from memory and

decodes and executes them, calling on the ALU when necessary. • The Registers, temporarily hold instructions and data.

16

Page 17: COMP 1020: Structured Programming (2)

Machine instruction cycle:(also called machine cycle, instruction cycle) The time period during which one instruction is fetched

from memory and executed when computer is given an instruction in machine language.

Four stages of an instruction cycle : • Fetch the instruction from memory. This step brings

the instruction into the instruction register, a circuit that holds the instruction so that it can be decoded and executed.

• Decode the instruction. • Execute the instruction.• Store the result in memory.Steps 1 and 2 are called the fetch cycle and are the same

for each instruction. Steps 3 and 4 are called the execute cycle and will change with each instruction.

17

Page 18: COMP 1020: Structured Programming (2)

Computer memory (or casually memory) --computer components/devices that retain

data/programs for some interval of time.

Computer storage provides one of the core functions of the modern computer, that of information retention. It is one of the fundamental components of all modern computers.

18

Page 19: COMP 1020: Structured Programming (2)

Von Neumann Architecture for computer

CPU

Main memoryI/O modules – move data between computer and its

external environment, including secondary memory devices (e.g. disks) communication equipments and terminals.

System buses – move data between CPU, main memory and I/O devices.

19

Page 20: COMP 1020: Structured Programming (2)

Memory/ Storage

Memory usually refers to a form of solid state storage known as random access memory (RAM) and sometimes other forms of fast but temporary storage.

Storage more commonly refers to mass storage – optical discs, forms of magnetic storage like hard disks, and other types of storage which are slower than RAM, but of a more permanent nature.

20

Page 21: COMP 1020: Structured Programming (2)

RAM: a data storage formats and equipment that allow the storing data to be accessed in any order — that is, at random, not just in sequence.

In contrast, other types of memory devices (such as magnetic tapes, disks, and drums) can access data on the storage medium only in a predetermined order due to constraints in their mechanical design.

21

Page 22: COMP 1020: Structured Programming (2)

RAMRAM in a computer is considered main memory (primary storage). The

arithmetic and logic unit can very quickly transfer information between a processor register and locations in main storage (memory addresses). In modern computers, RAM is directly connected to the CPU via a "memory bus" and a "data bus". In general, a faster memory bus means higher processing speeds and a faster computer.

RAM is also volatile, losing the stored information in an event of power loss, and quite expensive.

This type of RAM is usually in the form of integrated circuits (IC). Most personal computers have slots for adding and replacing memory chips.

A location in memory is identified by its address. The data hold in a memory address can be changed, but the address itself remains the same.

22

Page 23: COMP 1020: Structured Programming (2)

Inboard memory Registers fastest

Cache

Main memory

Outboard storage magnetic disk

CD-ROM

CD-RW

DVD

Off-line storage magnetic tape

Network-attached storage (NAS)

Memory Hierarchy

23

Page 24: COMP 1020: Structured Programming (2)

Three kinds of Primary memoryProcessor registers are internal to the CPU. Registers

contain information that the arithmetic and logic unit needs to carry out the current instruction. They are technically the fastest of all forms of computer storage.

Cache memory is a special type of internal memory used by many CPUs to increase their performance.

Main memory contains the programs that are currently being run and the data the programs are operating on.

24

Page 25: COMP 1020: Structured Programming (2)

Inboard memory

ALU

Registers

Cache memory

Main memory(RAM)

Memory bus

CPU

25

Page 26: COMP 1020: Structured Programming (2)

Registers -- a small amount of very fast memory used to speed the execution of programs by providing quick access to commonly used values—typically, the values being calculated at a given point in time. Most, but not all, modern computer architectures operate

on the principle of moving data from main memory into registers, operating on them, then moving the result back into main memory—a so-called load-store architecture.

All data must be represented in a register before it can be processed. For example, if two numbers are to be multiplied, both numbers must be in registers, and the result is also placed in a register.

The number of registers that a CPU has and the size of each (number of bits) help determine the power and speed of a CPU. For example a 32-bit CPU is one in which each register is 32 bits wide. Therefore, each CPU instruction can manipulate 32 bits of data. 26

Page 27: COMP 1020: Structured Programming (2)

Cache -- a block of memory for temporary storage of data likely to be used again

A simple definition of Cache : A temporary storage area where frequently accessed data can be stored for rapid access.

Once the data is stored in the cache, future use can be made by accessing the cached copy rather than re-fetching or re-computing the original data, so that the average access time is lower.

27

Page 28: COMP 1020: Structured Programming (2)

Inboard memory

ALU

Registers

L1 Cache

Main memory(RAM)

Memory bus

CPU

L2 Cache

Memory bus

28

Page 29: COMP 1020: Structured Programming (2)

Software Basics

• Computer = Hardware + Software

• Software– Application software

• Address users' specific needs in the real world• word processing, music software, image editing, games,

database programs, inventory control systems, etc.

– System software• Operating system

• Device drivers

• Utilities

• Programming software tools (compilers, debuggers, etc.) 29

Page 30: COMP 1020: Structured Programming (2)

The Computer Level Hierarchy

30

Page 31: COMP 1020: Structured Programming (2)

Operation System -- A set of programs to coordinate all activities of among computer hardware devices, to exploit resources provided by one or more processors.

It is a layer of software to hide all the details of machine complication from system users. All high level programming activities are performed on top of the OS.

Examples:

DOS, Window, Window XP

UNIX, Linux

Mac OS

31

Page 32: COMP 1020: Structured Programming (2)

Layered-view of a computing system

Computer hardware

Operating system

utilities

Application programs

End user

Programmer

Operating systemDesigner/programmer

32

Page 33: COMP 1020: Structured Programming (2)

33

Page 34: COMP 1020: Structured Programming (2)

Machine and Assembly Language

34

Page 35: COMP 1020: Structured Programming (2)

What an assembler does

35

Page 36: COMP 1020: Structured Programming (2)

High Level Language

AREA = HEIGHT * WIDTH

36

Page 37: COMP 1020: Structured Programming (2)

High Level Language

37

Page 38: COMP 1020: Structured Programming (2)

Construction of a Program

38

Page 39: COMP 1020: Structured Programming (2)

Computer programming

(or simply programming or coding) is the process of writing a set of commands or instructions that can later be compiled and/or interpreted and then transformed to an executable that an electronic machine can execute or "run".

Programming requires mainly logic, but has elements of science, mathematics, engineering, and — many would argue — art.

Programming requires the use of a programming language that the computer can understand.

In software engineering, programming (implementation) is regarded as one phase in a software development process.

39

Page 40: COMP 1020: Structured Programming (2)

Programming Language

The only programming language a computer can directly execute is machine language (sometimes called "machine code").

Originally all programmers worked out every detail of the machine code (machine dependent), but this is hardly ever done anymore.

Instead, programmers write high-level source code, and a computer (by running a compiler, an interpreter or occasionally an assembler) translates it through one or more translation steps to fill in all the details, before the final machine code is executed on the target computer.

40

Page 41: COMP 1020: Structured Programming (2)

Programming Language (2)

In some languages, an interpretable byte-code is generated, rather than machine language. Byte-code is used in the popular Java by Sun Microsystems, as well as Microsoft’s recent .NET family of languages and Visual Basic previous to the .NET version.

Different programming languages support different styles of programming. Part of the art of programming is selecting one of the programming languages best suited for the task at hand. Different programming languages require different levels of detail to be handled by the programmer when implementing algorithms, often in a compromise between ease of use and performance (a trade-off between "programmer time" and "computer time").

41

Page 42: COMP 1020: Structured Programming (2)

C &

History of C & C++

42

Page 43: COMP 1020: Structured Programming (2)

How C & C++ work?

43

Page 44: COMP 1020: Structured Programming (2)

Difference between C and C++

(including C)

44

Page 45: COMP 1020: Structured Programming (2)

Procedural Programming

• specifying the steps the program must take to reach the desired state

• based upon the concept of procedure calls

Simple sequential or unstructured programming in many situations which involve moderate complexity will lead to so-called “spaghetti code”.

45

Page 46: COMP 1020: Structured Programming (2)

Structured programming (using C)can be seen as a subset or sub-discipline of procedural programming, one of the major programming paradigms (other paradigm: object oriented programming with C++)

It is most famous for removing or reducing reliance on the GOTO (or "go to") action.

46

Page 47: COMP 1020: Structured Programming (2)

Benefits of structured programming

• The ability to re-use the same code at different places in the program without copying it.

• An easier way to keep track of program flow than a collection of "GOTO" or "JUMP" Actions (which can turn a large, complicated program into so-called “spaghetti code”).

• The ability to be strongly modular.

47

Page 48: COMP 1020: Structured Programming (2)

Low-level structure

At a low level, structured programs are composed of simple, hierarchical program flow structures. These are regarded as single Actions, and are the same time ways of combining simpler Actions, which may be one of these structures, or primitive Actions such as assignments or procedure calls. Three common types of structure were concatenation, selection, and repetition.

• "Concatenation" refers to a sequence of Actions executed in order.• In "selection", one of a number of Actions is executed depending on

the state of the program. This is usually Expressed with keywords such as if..then..else..endif, switch, or case.

• In "repetition" a Action is executed until the program reaches a certain state or applied to every element of a collection. This is usually Expressed with keywords such as while, repeat, for or do..until. Often it is recommended that each loop should only have one entry point (and in the original structural programming, also only one exit point), and a few languages enforce this.

48

Page 49: COMP 1020: Structured Programming (2)

High-level structure

Coders should break larger pieces of code into shorter subroutines (functions, procedures. methods, blocks, or otherwise) that are small enough to be understood easily.

In general, programs should use global variables sparingly; instead, subroutines should use local variables and take arguments by either value or reference. These techniques help to make isolated small pieces of code easier to understand without having to understand the whole program at once.

49

Page 50: COMP 1020: Structured Programming (2)

50

Page 51: COMP 1020: Structured Programming (2)

51

Page 52: COMP 1020: Structured Programming (2)

Programming Tools (1)

52

Page 53: COMP 1020: Structured Programming (2)

Programming Tools (2)

53

Page 54: COMP 1020: Structured Programming (2)

Programming Tools (3)

54

Page 55: COMP 1020: Structured Programming (2)

Wrappers or IDEs

55

Page 56: COMP 1020: Structured Programming (2)

Text editor

Programming with Wrappers or IDEs

56