Post on 02-Jan-2016
1
COMP 1020:
Structured Programming (2)
Instructor: Prof. Ken Tsang Room E409-R11
Email: kentsang@uic.edu.hk
2
TA information
Mr Fu Song-feng付嵩峰Room E409 Tel: 3620630
songfengfu@uic.edu.hk
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
4
Tutorials
• One hour each week
• Time & place to be announced later (we need your input)
• More explanations
• More examples
• More exercises
5
How is my final grade determined?
• Lab works 15% • Project 20%• Quizzes 15%• Final Examination 50%
6
UIC Score System
7
Grade Distribution Guidelines
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
• 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
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
11
The McKinsey Global Institute:
12
How Companies Learn Your Secrets
By CHARLES DUHIGGPublished: February 16, 2012
Lecture 1
Introduction: basic knowledge of computer hardware and software
Structured Programming Instructor: Prof. K. T. Tsang
13
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
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
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
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
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
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
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
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
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
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
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
Inboard memory
ALU
Registers
Cache memory
Main memory(RAM)
Memory bus
CPU
25
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
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
Inboard memory
ALU
Registers
L1 Cache
Main memory(RAM)
Memory bus
CPU
L2 Cache
Memory bus
28
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
The Computer Level Hierarchy
30
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
Layered-view of a computing system
Computer hardware
Operating system
utilities
Application programs
End user
Programmer
Operating systemDesigner/programmer
32
33
Machine and Assembly Language
34
What an assembler does
35
High Level Language
AREA = HEIGHT * WIDTH
36
High Level Language
37
Construction of a Program
38
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
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
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
C &
History of C & C++
42
How C & C++ work?
43
Difference between C and C++
(including C)
44
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
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
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
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
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
50
51
Programming Tools (1)
52
Programming Tools (2)
53
Programming Tools (3)
54
Wrappers or IDEs
55
Text editor
Programming with Wrappers or IDEs
56