c programming (1/7) - computers and programming

Download C Programming (1/7) - Computers and Programming

Post on 13-Nov-2014




4 download

Embed Size (px)


Hardware, Types of programming languages


FE1008 ComputingChapter 1 Computers & ProgrammingA/Prof Fan Weijun, Email: ewjfan@ntu.edu.sg


OutlinePart 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




Keyword: programmable1-3

Computer SystemsTwo 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



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.


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 Contents100000 100001 100002 100003 100004 100005


H I1-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


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 MemoryROM (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 MemoryFlash 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.


CPU Components

Registers Control Unit ALU Data, Address, Control Buses

M e m o r y


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


Registers & Control UnitControl 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 DevicesFor 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


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


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


Application SoftwareSoftware 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)


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.


Programming LanguagesA 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


Machine LanguageEach 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 LanguagesMost 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.


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


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


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


AlgorithmAn 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 1To 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.


Pseudocode Example 2aTo 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


Pseudocode Example 2bThe 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 max1-29

Pseudocode Example 3To 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 .... ...


Pseudocode Example 3INPUT max_time, interval SET accel = 9.8, time = 0 PRINT Table Title WHILE time


View more >