C Programming (1/7) - Computers and Programming

34
1-1 FE1008 Computing Chapter 1 Computers & Programming A/Prof Fan Weijun, Email: [email protected]

description

Hardware, Types of programming languages

Transcript of C Programming (1/7) - Computers and Programming

Page 1: C Programming (1/7) - Computers and Programming

1-1

FE1008 Computing

Chapter 1 Computers & Programming

A/Prof Fan Weijun, Email: [email protected]

Page 2: C Programming (1/7) - Computers and Programming

1-2

Outline

Computers

Overview of C

Fundamental Data Types

C Operators

Standard Library Functions

Decision Making

Loops

Text book: Harry H. Cheng “C for Engineers and Scientists”, McGraw-Hill, 2010

CA 30%; Final Examination 70%

Part I by W J FAN Part II by Prof C Q SUN

Functions Modular Programming Arrays Pointers File I/O

Page 3: C Programming (1/7) - Computers and Programming

1-3

What is a Computer?

Keyword: programmable

Input Processing Output

Instructions & Data Storage

Page 4: C Programming (1/7) - Computers and Programming

1-4

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

Page 5: C Programming (1/7) - Computers and Programming

1-5

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

Page 6: C Programming (1/7) - Computers and Programming

1-6

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.

Page 7: C Programming (1/7) - Computers and Programming

1-7

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

3.14159

H

I

100000 100001 100002 100003

100004

100005

Page 8: C Programming (1/7) - Computers and Programming

1-8

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

Page 9: C Programming (1/7) - Computers and Programming

1-9

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.

Page 10: C Programming (1/7) - Computers and Programming

1-10

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.

Page 11: C Programming (1/7) - Computers and Programming

1-11

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.

Page 12: C Programming (1/7) - Computers and Programming

1-12

CPU Components

Registers

Control Unit

ALU

M

e

m

o

r

y

Data, Address, Control Buses

Page 13: C Programming (1/7) - Computers and Programming

1-13

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.)

Page 14: C Programming (1/7) - Computers and Programming

1-14

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.

Page 15: C Programming (1/7) - Computers and Programming

1-15

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

Page 16: C Programming (1/7) - Computers and Programming

1-16

System 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.

Two major types of software: (i) System Software; (ii) Application Software.

Page 17: C Programming (1/7) - Computers and Programming

1-17

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

Page 18: C Programming (1/7) - Computers and Programming

1-18

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)

Page 19: C Programming (1/7) - Computers and Programming

1-19

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.

Page 20: C Programming (1/7) - Computers and Programming

1-20

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

Page 21: C Programming (1/7) - Computers and Programming

1-21

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.

Page 22: C Programming (1/7) - Computers and Programming

1-22

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.

Page 23: C Programming (1/7) - Computers and Programming

1-23

High-Level Languages (Examples)

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

Page 24: C Programming (1/7) - Computers and Programming

1-24

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.

Page 25: C Programming (1/7) - Computers and Programming

1-25

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

Page 26: C Programming (1/7) - Computers and Programming

1-26

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).

Page 27: C Programming (1/7) - Computers and Programming

1-27

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.

Page 28: C Programming (1/7) - Computers and Programming

1-28

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

Page 29: C Programming (1/7) - Computers and Programming

1-29

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

Page 30: C Programming (1/7) - Computers and Programming

1-30

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 . . . . . . .

Page 31: C Programming (1/7) - Computers and Programming

1-31

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

Page 32: C Programming (1/7) - Computers and Programming

1-32

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).

Page 33: C Programming (1/7) - Computers and Programming

1-33

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.

Page 34: C Programming (1/7) - Computers and Programming

1-34

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.