Vous êtes sur la page 1sur 36

Part 1 Slide 1

References
H. M. Deitel & P. J. Deitel.
C How to Program,
5th Edition.
Prentice Hall, 2004.

B. W. Kernighan & D. M. Ritchie.


The C Programming Language,
2nd Edition.
Prentice-Hall, 1988.

LYS, SAM, IB, HK


Part 1 Slide 2

Collaboration
The goal of assignments (homework) is to give you
practice in mastering the course material.
You must write up each problem solution or
program by yourself without assistance, even if
you collaborate with others to solve the problem.
You are asked on your assignment hand-in to
identify your collaborators. If you did not work
with anyone, you should write "Collaborators:
none". If you obtain a solution through research
(e.g., on the world wide web), acknowledge your
source, but write up the solution in your own
words.
Plagiarism and other anti-intellectual behavior
cannot be tolerated in any academic environment.
LYS, SAM, IB, HK
Part 1 Slide 3

What is a computer?
A computer is a machine that stores data (numbers,
words, pictures), interacts with devices (the
monitor screen, the sound system, the printer), and
executes programs.
A computer program is a sequence of instructions
and decisions that the computer carries out to
achieve a task.
Programs describe specific actions.
A computer executes very simple instructions.
A computer executes instructions very rapidly.
A computer is a general purpose machine.

LYS, SAM, IB, HK


Part 1 Slide 4

What is a computer?
A computer must be programmed to perform
tasks. Different tasks require different programs.
A computer program consists of a finite sequence
of very basic operations.
A typical operation may be one of the following:
put a red dot onto a certain screen position
send the letter B to the printer
get a number from a certain location in memory
add up two numbers
if this value is negative, stop the program
repeat this instruction ten times

LYS, SAM, IB, HK


Part 1 Slide 5

What is a computer?
Hardware
Physical devices of a computer system
Software
Programs that run on computers

LYS, SAM, IB, HK


Part 1 Slide 6

Common Hardware Components

Standard Hardware Processor (CPU)


Organization Central Processing Unit
Interprets and executes the
instructions
Memory
main & secondary (auxiliary)
Memory holds data and programs
Input device(s)
mouse, keyboard, etc.

Input Output Output device(s)


Processor video display, printer, etc.
Devices (CPU) Devices
CPU and memory are
physically housed together

LYS, SAM, IB, HK


Part 1 Slide 7

What is programming?
A computer program tells a computer, in very
detail, the sequence of steps that are needed to
fulfill a task.
The act of designing and implementing these
programs is called computer programming.
Programmers develop computer programs to make
computers perform new tasks.
A professional computer scientist or software
engineer does a great deal of programming.
The activity of programming is an important part
of computer science.

LYS, SAM, IB, HK


Part 1 Slide 8

Anatomy of a computer
At the heart of the computer lies the central
processing unit (CPU).
The CPU locates and executes the program
instructions; it carries out arithmetic operations
such as addition, subtraction, multiplication, and
division; it fetches data from external memory or
devices or stores data back.

LYS, SAM, IB, HK


Part 1 Slide 9

Anatomy of a computer
The computer keeps data and programs in storage.
There are two kinds of storage:
primary storage, also called random-access memory
(RAM) or simply memory.
secondary storage, usually a hard disk.
Primary storage loses all its data when the power
is turned off. Secondary storage persists without
electricity.
Most computers have removable storage devices:
floppy disks, tapes, compact discs (CDs),
flashdisks.

LYS, SAM, IB, HK


Part 1 Slide 10

Anatomy of computer
To interact with a human user, a computer
requires peripheral devices.
input devices: keyboard, mouse.
output devices: display screen, printer.

The CPU, the RAM, and the electronics controlling


the hard disk and other devices are interconnec-
ted through a set of electrical lines called a bus.

LYS, SAM, IB, HK


Part 1 Slide 11

Computer System
Five logical units of a computer system
Input unit: Mouse, keyboard
Output unit: Printer, monitor, audio speakers
Primary Storage unit (Memory unit): RAM
Central processing unit (CPU)
supervises operation of other devices
contains Arithmetic and Logic Unit (ALU)
ALU performs calculations
Secondary storage unit
Hard-disk drives, floppy-disk drives, CD drives

LYS, SAM, IB, HK


Part 1 Slide 12

Central Processing Unit

LYS, SAM, IB, HK


Part 1 Slide 13

RAM Chips

LYS, SAM, IB, HK


Part 1 Slide 14

A Hard Disk

LYS, SAM, IB, HK


Part 1 Slide 15

Floppy Disk and


Its Drive

LYS, SAM, IB, HK


Part 1 Slide 16

A CD-ROM Drive

LYS, SAM, IB, HK


Part 1 Slide 17

Tape Backup Drives and Data Tape

LYS, SAM, IB, HK


Part 1 Slide 18

A Personal Computer
LYS, SAM, IB, HK
Part 1 Slide 19

A Motherboard

LYS, SAM, IB, HK


Part 1 Slide 20

Schematic Diagram of a
Personal Computer
LYS, SAM, IB, HK
Part 1 Slide 21

Main Memory Organization

Bit = one binary digit Address Data Byte


Binary digit can have only 3021 1111 0000 Item 1: 2 bytes
one of two values, 0 or 1 stored
Byte = 8 bits 3022 1100 1100

“Byte Addressable” 3023 1010 1010 Item 2: 1 byte


Main memory is a list of
stored
3024 1100 1110 Item 3: 3 bytes
numbered locations that stored
contain one byte of data in 3025 0011 0001
each location 3026 1110 0001
Number of bytes per data
item may vary 3027 0110 0011 Item 4: 2 bytes
stored
3028 1010 0010

3029 … Next Item, etc.

LYS, SAM, IB, HK


Part 1 Slide 22

Running a Program
Program—a sequence of instructions for a computer to follow

Program

Data
Computer Output
(input for the program)

LYS, SAM, IB, HK


Part 1 Slide 23

A very simple C program:

/* A first program in C */
#include <stdio.h>
/* function main begins program execution */
int main()
{
printf( “Selamat belajar programming" );
return 0; /*indicate that program ended successfully */

} /* end function main */

output: Selamat Belajar Programming!


LYS, SAM, IB, HK
Part 1 Slide 24

Compiling and Running


Type program with a text editor (e.g. vi, notepad)
Save into a file with name myprogram.c
Compile into machine codes
gcc myprogram.c –o myprogram
Execute the program
myprogram

LYS, SAM, IB, HK


Part 1 Slide 25

Edit–Compile–Debug Loop

LYS, SAM, IB, HK


Part 1 Slide 26

Problem Solving
Given a problem, find an appropriate algorithm.
Algorithms are implemented as executable
computer programs.
Algorithm is an unambiguous, executable, and
terminating specification of a way to solve a
problem.

LYS, SAM, IB, HK


Part 1 Slide 27

Algorithms
Unambiguous
Executable
Terminating
If you can't find an algorithm, the computer can't
solve your problem.

LYS, SAM, IB, HK


Part 1 Slide 28

Programming Languages
Machine language
“Natural language” of computer component
Machine dependent
Assembly language
English-like abbreviations represent computer operations
Assembler converts it to machine language
High-level language
Allows for writing more “English-like” instructions
Contains commonly used mathematical operations
Compiler converts it to machine language
Interpreter executes high-level language programs
without compilation

LYS, SAM, IB, HK


Part 1 Slide 29

 High-Level Language (HLL)  Machine Language


 closest to natural language  least natural language for
 words, numbers, and math humans, most natural
symbols language for hardware
 not directly understood by  just 0s and 1s
hardware  directly understood by
 “portable” source code hardware
(hardware independent)  not portable (hardware
dependent)

LYS, SAM, IB, HK


Part 1 Slide 30

Assembly Language
(middle level)
a more or less human .model small
.stack
readable version of machine .data
language message db "Hello world, I'm learning
Assembly !!!", "$"
words, abbreviations, letters
and numbers replace 0s and .code
1s main proc
easily translated from human mov ax,seg message
readable to machine
mov ds,ax

executable code mov ah,09


lea dx,message
like machine code, not int 21h
portable (hardware
dependent) mov ax,4c00h
int 21h
main endp
end main

LYS, SAM, IB, HK


Part 1 Slide 31

Some High Level Languages


C, C++
Haskell
Fortran
FORmula TRANslator
COBOL
COmmon Business Oriented Language
Pascal, Modula
BASIC
Ada
C#, Java
Prolog
LYS, SAM, IB, HK
Part 1 Slide 32

Getting from Source-code


to Machine-code
“Compiling a program”
translating from a high-level language source code to machine (object, or
executable) code.
“Compiler”
a program that translates HLL source code to machine (object, or executable)
code.
Compilers need to know the specific target hardware

Source Machine
Compiler
Code Code

LYS, SAM, IB, HK


Part 1 Slide 33

Types of Errors
Syntax

Run-Time

Logic

LYS, SAM, IB, HK


Part 1 Slide 34

Syntax Errors

a “grammatical” error
caught by compiler (“compiler-time error”)
automatically found, usually the easiest to fix
cannot run code until all syntax errors are fixed
error message may be misleading

Example:
Misspelling a command, for example “rturn” instead of “return”

LYS, SAM, IB, HK


Part 1 Slide 35

Run-Time Errors
An execution error (during run-time)
Not always so easy to fix
Error message may or may not be helpful

Example:
Division by zero - if your program attempts to divide an integer by
zero it automatically terminates and prints an error message.

LYS, SAM, IB, HK


Part 1 Slide 36

Logic Errors

Just because it compiles and runs without getting an error message


does not mean the program is correct!

An error in the design (the algorithm) or its implementation


code compiles without errors
no run-time error messages
but incorrect action or data occurs during execution
Generally the most difficult to find and fix
Need to be alert and test thoroughly
think about test cases and predict results before executing the code
Formal Method: use mathematics to develop software and prove its
correctness

LYS, SAM, IB, HK

Vous aimerez peut-être aussi