Sie sind auf Seite 1von 34

FE1008 Computing

Chapter 1 Computers & Programming


A/Prof Fan Weijun, Email: ewjfan@ntu.edu.sg

1-1

Outline
Part I by W J FAN Computers Overview of C Fundamental Data Types C Operators Standard Library Functions Decision Making Loops Functions Modular Programming Arrays Pointers File I/O Part II by Prof C Q SUN

Text book: Harry H. Cheng C for Engineers and Scientists, McGraw-Hill, 2010 CA 30%; Final Examination 70% 1-2

What is a Computer?

Instructions & Data Storage

Input

Processing

Output

Keyword: programmable
1-3

Computer Systems
Two main components: 1. Hardware: monitor, keyboard, etc. 2. Software: Collections of instructions for the computer to execute (programs).
The Apple 1 which was sold as a do-it-yourself kit

The modern Apple PC 1-4

Hardware Components
Main Memory (Primary Storage) The Central Processing Unit (CPU) Secondary Storage Input Devices Output Devices

http://www.youtube.com/watch?v=S6-5FCv7-_g

1-5

Main Memory (1)


To store instructions to be executed, data to be manipulated, processing results. Information is stored in bits--short for binary digits (0 or 1, off or on). These bits are organized into groups of 8 bits (1 byte) called memory cells.

1-6

Main Memory (2)


Think of main memory as a collection of boxes (cells). Each cell can hold 1 byte of data and has a unique address (an integer value). Address Contents
100000 100001 100002 100003 100004 100005

3.14159

H I
1-7

Main Memory (3)


As each memory cell can be accessed directly, main memory is also known as Random Access Memory (RAM). The size is measured in kilobytes (KB) megabytes (MB), or gigabytes (GB): 1 KB = 210 bytes = 1024 bytes 1 MB = 220 bytes = 1024 KB 1 GB = 230 bytes = 1024 MB

1-8

Main Memory (4)


RAM is very fast memory for storing instructions and data temporarily. However, the most common type of RAM has two disadvantages: 1. It is volatile (i.e. it loses all data when power is cut off). 2. It is expensive. We need cheap and permanent storage: disks, tapes, etc.
1-9

Read-Only Memory
ROM (Read-Only Memory) stores data permanently. Usually, they can only be read from but not written to. The data are physically encoded in the circuit, so they cannot be modified easily, if at all. Usually used to store boot-up (start up) instructions, i.e., the initial instructions that run when the computer is powered on. It is also used for storing other critical system instructions.
1-10

Flash Memory
Flash memory is a type of non-volatile memory that can be erased and reprogrammed. It is used in memory cards (e.g. for digital cameras and cell phones) and USB flash drives (thumb drive, pen drive, USB stick, ) for storage and transfer of data between digital devices.

1-11

CPU Components

Registers Control Unit ALU Data, Address, Control Buses

M e m o r y

1-12

ALU
The ALU (Arithmetic Logic Unit) is a fundamental component of the CPU, and it can perform: Arithmetic operations (+, -, x, ) Logical operations (COMPARE, AND, OR, NOT, etc.)

1-13

Registers & Control Unit


Control Unit: It controls flow of instructions and data from and to memory, and inside the CPU. It tells the ALU what operation to perform on the data. Registers: These are memory cells inside the CPU to allow very rapid access of instructions and data by the ALU and the Control Unit.
1-14

Secondary Storage & I/O Devices


For cheap and permanent storage of data. Examples: Hard disks Flash drives CD-R, CD-RW, DVD, DVD-RW Magnetic tapes Input Devices: keyboard, mouse, tablets Output Devices: monitor, printers, plotters

1-15

System Software
Two major types of software: (i) System Software; (ii) Application Software. System Software sub-categories: 1. Operating Systems (OS) Manage resources of a computer. Most important software. 2. Utility Programs e.g. programs to format disks, compress data, etc. 3. Software Development Tools e.g. compilers, linkers, etc.

1-16

Operating System
Loaded into RAM when computer is started (a process known as booting the computer). Controls access to computer Enforces security and privacy of files Allocates/manages memory, disk space, etc. An OS shields the user from the complexity of computer hardware. Examples: Windows 2000/2003/XP/Vista/7, UNIX (Linux), Mac OSX

1-17

Application Software
Software that are very useful to most users. Examples are: Word Processors (Microsoft Word) Spreadsheets (Excel) Graphics (Photoshop) World Wide Web (WWW) Browsers (Internet Explorer, Firefox, Opera, Safari) Computer-Aided Design (CAD) (AutoCAD)

1-18

What is Programming?
The computer needs detailed and exact instructions to carry out the steps needed to solve a problem. These instructions must be coded (i.e. written) using a programming language and they form a computer program.

1-19

Programming Languages
A programming language is an artificial language with a set of special words (keywords) and a set of rules (syntax or grammar). Two main categories: (1) Low-level languages Machine language, Assembly language (2) High-level languages

1-20

Machine Language
Each CPU has a set of instructions designed and manufactured into it. These machine instructions are different for CPUs from different chip design companies. They consist of sequences of 0s and 1s. The only language "understood" by the computer. Machine language statements look like: 100100 0000 010001 100110 0000 010010 100010 0000 010011 Difficult to use.
1-21

High-Level Languages
Most popular because They use English words (such as PRINT, READ, WRITE, etc.) Need a translator (compiler) to convert the statements into machine language. The programs are portable. High-level languages are not tied to any particular CPU. Programs are much easier to read, understand and modify.

1-22

High-Level Languages (Examples)


There are numerous high-level languages. Common ones are: FORTRAN (Formula Translation) C, C++ Java C# (C Sharp) Perl Python

1-23

Language Standard
Each major high-level language has a language standard that describes its syntax (grammar). The syntax rules are very strict. Deviations will result in syntax errors.

1-24

The Programming Process


1. Problem Solving: (a) Problem Specification (b) Problem Analysis (c) Algorithmic Design (Solution Procedures) 2. Implementation (a) Coding (b) Testing (c) Maintenance

1-25

Algorithm
An algorithm is a set of precisely stated, finite sequence of executable steps for solving a problem. Finding a suitable algorithm is frequently the most difficult part of the problem solving process. An algorithm is usually written using informal English-like statements known as pseudocode. Another representation is using a graphical flowchart (not covered here).
1-26

Pseudocode Example 1
To calculate the area of a circle, given its radius. INPUT radius circle_area=3.14159*radius*radius OUTPUT circle_area Note: We can use words like GET or READ, etc. instead of INPUT; similarly, we can also use WRITE, PRINT, etc. for OUTPUT.

1-27

Pseudocode Example 2a
To find the larger of two user-input numbers. INPUT number1, number2 IF number1 > number2 THEN SET max = number1 ELSE SET max = number2 END IF OUTPUT max

1-28

Pseudocode Example 2b
The following is an alternative (and better) form for finding the larger of two user-input numbers. INPUT number1, number2 SET max = number1 IF number2 > max THEN SET max = number2 END IF OUTPUT max
1-29

Pseudocode Example 3
To calculate and display the distances travelled by a particle falling under gravity from an initial time 0 to a given maximum time. Assume zero initial velocity. Formula is s=1/2*a*t*t

The results are to be displayed in a table form: Time Elapsed Distance Travelled ---------------------------------0.00 0.00 0.50 1.23 1.00 4.90 1.50 11.03 2.00 19.60 .... ...

1-30

Pseudocode Example 3
INPUT max_time, interval SET accel = 9.8, time = 0 PRINT Table Title WHILE time <= max_time distance = 0.5*accel*time*time SET time = time + interval PRINT time, distance END WHILE

1-31

Edit, Compile & Link Cycle


1. Create the source program (simple.c) by typing program statements into a program editor. 2. Use a compiler to translate the source program into machine language. This gives an object file (simple.obj) if there are no syntax errors. If there are errors, we need to go back to the editor to fix these errors. 3. Use a linker to combine this object file with other necessary components to get a stand-alone executable file (simple.exe).
1-32

Edit, Compile & Link Cycle


These steps are usually carried out within an IDE (Integrated Development Environment). An IDE typically contains an editor, a compiler, a linker, a debugger (for helping to find and correct programming errors), and other components. We use the Microsoft Visual C++ in this course.

1-33

Errors (Bugs) in Programs


A bug is an error in software/hardware. Correcting a bug is known as debugging. The following types of errors may appear in our programs: Syntax Errors: errors in grammar Logical Errors: errors due to the use of wrong algorithm, wrong formula, etc. Runtime Errors: errors that occur when a program is being executed. An example is division by a variable value which turns out to be zero.
1-34

Das könnte Ihnen auch gefallen