introduction to computer programming nai-wei lin department of computer science and information...

36
Introduction to Introduction to Computer Programming Computer Programming Nai-Wei Lin Department of Computer Sc ience and Information Eng ineering National Chung Cheng Univ ersity

Upload: katelyn-bellus

Post on 15-Dec-2015

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

Introduction to Introduction to Computer Computer

ProgrammingProgrammingNai-Wei Lin

Department of Computer Science and Information Engineering

National Chung Cheng University

Page 2: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

2

ContentsContents

• Introduction to computers and

computer science

• Basic programming skills in

programming language C

• Basic problem solving techniques

Page 3: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

3

Chapter OneChapter One

Introduction to Introduction to ComputersComputers

Page 4: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

4

ComputersComputers

• Computers are programmable machines capable of performing calculations

• Examples of special-purpose computers are calculators and game-playing machines

• Examples of general-purpose computers are personal computers and notebooks

Page 5: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

5

History of ComputingHistory of Computing

• Abacus – 2000 B. C.• First mechanical calculator – Wilhelm Schickard, 16

23• Mechanical machine (addition) – Blaise Pascal, 1640• Mechanical machine (multiplication/division) – Gott

fried Leibniz, 1673• Difference engine and analytical engine (program) –

Charles Babbage, 1871; first programmer – Augusta Ada Byron

Page 6: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

6

History of ComputingHistory of Computing

• First vacuum tube electronic computer – John Atanasoff & Clifford Barry, 1939

• Von Neumann Architecture – John von Neumann, 1946

• First transistor electronic computer – IBM 7090, 1958

• First integrated circuit electronic computer – IBM 360, 1964

• First microprocessor electronic computer – Altair 8800, 1975

Page 7: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

7

Hardware/SoftwareHardware/Software

• A computer consists of hardware and software

• The hardware consists of various physical devices that performs wired and basic operations

• The software consists of various programs that coordinates basic operations to accomplish flexible and complex tasks

Page 8: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

8

ProgramsPrograms

• A Program is a sequence of instructions (basic operations) to control the operation of the computer

• Programming is the task of designing programs

• Programming languages are notations used to represent the instructions of computers

Page 9: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

9

HardwareHardware

storage unit

input unit

primary storage (memory) unit

output unit

secondary storage unit

control unit

arithmetic and logic unit

(ALU)

central processin

g unit (CPU)

I/O unit

control bus

data bus

Page 10: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

10

Control UnitControl Unit

• Control unit repeatedly fetches instructions from memory unit to control unit via data bus

• Control unit then interprets instructions, and coordinates the operations of other units via control bus

Page 11: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

11

Arithmetic and Logic UnitArithmetic and Logic Unit

• ALU is responsible for performing calculations based on arithmetic operations such as addition, subtraction, multiplication, and division

• ALU is also responsible for making decisions based on logical operations such as equality (=, ≠) and relation (<, ≦, >, ≧)

Page 12: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

12

Arithmetic and Logic Arithmetic and Logic UnitUnit

• Arithmetic or logical operations performed by ALU are controlled by control unit via control bus

• Data on which operations are performed are transferred from memory unit via data bus

• Data resulted from operations are transferred to memory unit via data bus

Page 13: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

13

Primary Storage UnitPrimary Storage Unit

• Memory unit stores both instructions and data

• It retains information that is actively being used by the computer

• It is the short-term, rapid-access, low-capacity warehouse of the compute

Page 14: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

14

Secondary Storage UnitSecondary Storage Unit

• Secondary storage unit retains information that is not actively being used by the computer

• It is the long-term, slow-access, high-capacity warehouse of the computer

• Common secondary storage devices are disks and tapes

Page 15: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

15

Input UnitInput Unit

• Input unit transfers information from various input devices to memory unit

• It also transforms information in human-readable form to information in machine-readable form

• Common input devices are keyboards and mouse devices

Page 16: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

16

Output UnitOutput Unit• Output unit transfers information

from memory unit to various output devices

• It also transforms information in machine-readable form to information in human-readable form

• Common output devices are screens and printers

Page 17: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

17

Users

SoftwareSoftware

Application Programs

Operating System

Hardware

Page 18: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

18

Operating SystemOperating System

• The operating system provides efficient management of the hardware so that application programmers can easily use the computer without knowing the detailed operations of the hardware

• Common operating systems are DOS, Windows 2000, Windows NT, Unix, Linux

Page 19: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

19

Functions of Operating Functions of Operating SystemsSystems

• System administration

• Job scheduling

• Memory management

• File management

• Input and output device

management

Page 20: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

20

Evolution of Operating Evolution of Operating SystemsSystems

• Simple batch systems

• Multiprogrammed batch systems

• Time-sharing systems

• Parallel systems

• Distributed systems

Page 21: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

21

Application ProgramsApplication Programs

• An application program allows users to use a computer to solve problems in a specific domain without knowing the details of the computer

• Common application programs are MS Word, MS Excel, MS Access, MS Internet Explorer, Netscape Communicator

Page 22: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

22

Programming Programming LanguagesLanguagesHigh-level language

Compiler

Assembly language

Assembler

Machine language

Page 23: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

23

Machine LanguagesMachine Languages

• A computer can directly understand only

its own machine language

• A program denoted by a machine

language consists of strings of numbers

(1's and 0's)

• Machine languages are machine-

dependent

Page 24: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

24

An ExampleAn Example

21 0000010121 0000020111 0000010131 0000020112 0000030122 00000301

Input

Output

:

:

:

:92

95

187

ALU

00000101

00000201

00000301

21

22

11

12

31

21

Page 25: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

25

Assembly LanguagesAssembly Languages

• The assembly language of a computer is a mnemonic representation of its machine language and is also machine-dependent

• An assembler converts assembly language programs into machine language programs

• Each assembly instruction is usually converted into one machine instruction

Page 26: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

26

An ExampleAn Example

INPUT EnglishINPUT ChineseLOAD EnglishADD ChineseSTORE TotalOUTPUT Total

Input

Output

:

:

:

:92

95

187

ALU

English

Chinese

Total

Page 27: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

27

An ExampleAn Example

INPUT English 21 00000101 INPUT Chinese

21 00000201 LOAD English 11 00000101 ADD Chinese 31 00000201STORE Total 12 00000301 OUTPUT Total22 00000301

Page 28: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

28

High-Level LanguagesHigh-Level Languages

• A high-level language uses English-

like notations and commonly used

mathematical notations

• A standardized high-level language

is machine-independent or

portable

Page 29: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

29

CompilersCompilers

• A compiler translates high-level language

programs into assembly language

programs or machine language programs

• Each high-level instruction is usually

translated into several assembly

instructions

Page 30: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

30

An ExampleAn Example

Input(English);Input(Chinese);Total = English + Chinese;Output(Total);

Input

Output

:

:

:

:92

95

187

ALU

English

Chinese

Total

Page 31: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

31

An ExampleAn Example

Input(English); INPUT EnglishInput(Chinese); INPUT ChineseTotal = English + Chinese; LOAD English

ADD ChineseSTORE Total

Output(Total); OUTPUT Total

Page 32: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

32

Computer ScienceComputer Science

• Computer science is more concerned with the software or the science of problem solving

• Computer engineering is more concerned with the hardware or the engineering of computing machines

• Hardware costs have been declining dramatically; software costs have been rising steadily

Page 33: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

33

AlgorithmsAlgorithms

• An algorithm is an abstract strategy for

solving a problem

• Solving a problem by computer consists of

designing an algorithm and expressing the

algorithm as a program

Page 34: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

34

An Example:Finding the An Example:Finding the GCDGCD

M N R

369 27369 = 27 x 13 + 18 27 = 18 x 1 + 9 18 = 9 x 2 + 0 9

1

2~3

4

Page 35: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

35

An Example:Finding the An Example:Finding the GCDGCD

• Store M and N the value of the larger and

smaller of the two input values, respectively

• Divide M by N, and store the remainder R

• If R is not 0, then store M the value of N,

store N the value of R, and return to step 2

• Otherwise, the GCD is the current value of N

Page 36: Introduction to Computer Programming Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University

36

An Example:Finding the An Example:Finding the GCDGCD

Input(M);Input(N);Do { Q = M / N; R = M % N; M = N; N = R;} While (R != 0);Output(N);