Beruflich Dokumente
Kultur Dokumente
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?
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
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
1-6
3.14159
H I
1-7
1-8
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
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
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
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
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
1-33