A Typical Computer System
A Typical Computer System
A Typical Computer System
Ch 1-C Programming 2
Basic PC Hardware
• Central Processing Unit (CPU)
Executing instructions e.g. data movement,
arithmetic calculation, data comparison etc.
• Primary Storage
For temporary storage of programs & data
during processing (only when power is ON).
• Auxiliary Storage (Secondary)
For permanent storage of programs and data
(even when power is OFF, e.g. hard disk).
Ch 1-C Programming 3
Types of Computer Software
Ch 1-C Programming 6
Application Software
• General-Purpose
word processor, spreadsheet (Excel) &
database management programs (Access).
• Application-Specific
Can only be used for its intended purpose.
Cannot be used for other generalised tasks,
e.g. a custom-made payroll system for a
particular firm.
Ch 1-C Programming 7
Relationship between Various
Software and Hardware
Users normally work with
application software.
Application s/w in turn
interacts with system s/w.
System software interacts
with hardware
Users interacts
directly with
system software
Ch 1-C Programming 8
Computer Language Evolution
Time-Line
Ch 1-C Programming 10
What is a programming language?
• High-level languages: programming
languages that are relatively easy to be
understood by human.
• High-level languages are portable to many
different computers.
• Today the most popular high-level language
is C – the language to be learnt in this subject.
• Symbolic languages and high-level languages
must be translated into machine languages
before they can be executed by the computer.
Ch 1-C Programming 11
Machine Languages (Codes)
• In the earliest days of computers, it is the
only programming languages available.
• Each computer (i.e. CPU inside) has its own
machine language - streams of 0s and 1s.
• An example of multiplying two numbers is
given in next slide.
Ch 1-C Programming 12
Multiplication Program in Machine Code
00000000 00000100 0000000000000000
01011110 00001100 11000010 0000000000000010
11101111 00010110 0000000000000101
11101111 10011110 0000000000001011
11111000 10101101 11011111 0000000000010010
01100010 11011111 0000000000010101
11101111 00000010 11111011 0000000000010111
11111100 10101101 11011111 0000000000011110
00000011 10100010 11011111 0000000000100001
11101111 00000010 11111011 0000000000100100
01111110 11110100 10101101
11111000 10101110 11000101 0000000000101011
Ch 1-C Programming 13
Symbolic Languages
• Machine Languages were not convenient to use.
• These early programming languages simply
mirrored the machine languages using symbols,
or mnemonics, to represent the various machine
language instructions.
• Mnemonics are more comprehensible to human
than machine codes.
• A symbolic language is not understood by a
computer, it must be translated to the machine
codes.
Ch 1-C Programming 14
Symbolic Languages
• A special program called an assembler is used to
translate symbolic code into machine language.
• These symbolic languages are also known as assembly
languages.(symbolic languages = assembly languages)
• Different CPUs have different assembly languages.
• Assembly languages are still used in microprocessor-
or micro controller-based application developments.
• Advantage of assembly programs : can run faster than
high-level language programs.
• See the multiplication program in next slide.
Ch 1-C Programming 15
Multiplication Program in Assembly
entry main,^m<r2>
sub12 #12, sp
jsb C$MAIN_ARGS
movab $CHAR_STRING_CON
push -8 (fp)
pushal (r2)
calls #2, SCANF
pushal -12 (fp)
pushal 3 (r2)
calls #2, SCANF
mull3 -8 (fp), -12 (fp), -
pusha 6 (r2)
calls #2, PRINTF
clrl r0
ret Ch 1-C Programming 16
High-level Languages
• Symbolic Languages require programmers to
concentrate on hardware.
• each machine instruction has to be individually
coded.
• High-level languages are written in plain English text
which is more readable and understandable.
• They are also portable to many different computers.
• Examples of high-level programming languages :
FORTRAN, COBOL, Pascal, C, C++, Visual Basic,
Ada, LISP & Prolog etc.
Ch 1-C Programming 17
High-level Languages
• This allows the programmer to concentrate on the
application problem rather than the details of
manipulating computer hardware.
• They are designed to relieve the programmer from the
details of the assembly language.
• Similar to symbolic languages, high-level languages
must be converted to machine language (codes).
• The process of conversion is compilation. A special
program called compiler is used for such conversion.
Ch 1-C Programming 18
Writing and Editing
• Visual Basic comes with an integrated development
environment (IDE) which is used to create and edit
programs source file.
• It helps to enter, change and store program information.
• Differences between text processing and program
writing : programs are oriented around lines of code
rather than characters and lines.
• It is usually provided together with the compiler.
• The program text is stored as a source file
• The IDE reads the source code , scans for Syntax errors
in the source program and reported to the programmer.
Ch 1-C Programming 19
Program Execution
• Once the program is made to .EXE, it is ready for
execution.
• Getting the program into memory is the function of
an operating system program known as the loader.
• It locates the executable program and reads it into
memory.
• When everything is loaded, the program takes
control and it begins execution.
• When the program finishes its job, it informs the
operating system to remove it from memory, and
control is return to the operating system.
Ch 1-C Programming 20
Executing Programs
Ch 1-C Programming 21
System Development
• System Development is a critical process that
determines the overall quality and success of your
program.
• If you carefully design your program using good
structured development techniques, your programs
will be efficient, error free, and easy to maintain.
• Large-scale, modern programming projects are
built using a series of interrelated phases: system
development life cycle.
• One example is the waterfall model.
Ch 1-C Programming 22
Waterfall Model for System Development
Forward
Development Path
Ch 1-C Programming 24
Program Development
• An example for Program Design is given below.
Example: To calculate the square footage of your house
1st step: Understand the Problem:
• What is the definition of square footage ?
• How is the square footage going to be used ?
– For insurance purposes ?
– To paint the inside or outside of the house ?
– To carpet the whole house ?
• Is the garage included ?
• Are closets and hallways included ?
Ch 1-C Programming 25
Program Development
2nd step: Develop the solution :
• Three tools will help
– 1. Structured Charts
– 2. Pseudocode
– 3. Flowcharts
• A structure (or hierarchy) chart shows the functional
flow through the program.
• It shows how you are going to break the program
into logical steps or modules.
• It also shows the interaction between all parts of the
program.
Ch 1-C Programming 26
1. Structure/Hierarchy Chart
Ch 1-C Programming 27
2. Pseudocode
• Pseudocode is part English, part program logic, the
purpose is to describe what the program being designed
is to do.
• Pseudocode requires defining the steps to accomplish
the task in sufficient detail so that they can be converted
into a computer program.
• Most of the statements in the pseudocode are easily
understood plain-English text and simple mathematical
symbols.
Ch 1-C Programming 28
Pseudocode: a basic design tool
• Pseudocode is a more practical way to show how your
program will work.
• Pseudocode is a mixture of:
– English (or Cantonese!) and
– structured C programming statements, such as if,
while, do.
• Pseudocode should be simpler and easier to
understand than the final program source code
• Pseudocode should be written before you type in your
new program!
Ch 1-C Programming 29
Flowchart
Flowchart symbols
Terminal box
Ch 1-C Programming 30
Flowchart
Flowchart symbols
Process box
Ch 1-C Programming 31
Flowchart
Flowchart symbols
Decision box
Ch 1-C Programming 32
Flowchart
Flowchart symbols
Input/output box
Ch 1-C Programming 33
Flowchart
Flowchart symbols
represent a sequence of
processes given in
another separate
flowchart
Ch 1-C Programming 34
Flowchart
Flowchart symbols
Connector circle
Ch 1-C Programming 35
Flowchart
Flowchart symbols
Flow line
Ch 1-C Programming 36
Flowchart Example :
Start 1
Read R
1
Ch 1-C Programming 37
Problem 1
Determine Water Status
• Write a program to determine whether you
get water, steam or ice with a certain input
º C.
• Sample Input/Output:
– This program will evaluate a temperature
– and determine if water, ice or steam will return.
– ----------------------------------------------------------
– Please enter a temperature in degree Celsius : 120
– You will get STEAM.
Ch 1-C Programming 38
Problem 1
Pseudocode
• 1. Write a program description message
• 2. Write a user prompt to enter the
Temperature
• 3. Read (Temperature)
• 4. Determine the status
• 5. Write the result
Which step(s) may need further refinement ?
Ch 1-C Programming 39
Problem 1
Refinement
• 4. Determine the status
– If Temperature > 0 then
– If Temperature >= 100 then
– Status is “Steam”
– Else
– Status is “Water”
– Else
– Status is “Ice”
Ch 1-C Programming 40
Problem 2
Determine Water Status 2
• Refine the question of problem 1 if we are at the
mountain top, 95 degree Celsius can turn water
into steam. Therefore the sample input/output
will be
– This program will evaluate a temperature
– and determine if water, ice or steam will return.
– ----------------------------------------------------------
– Please enter a temperature in degree Celsius : 95
– Are you at the mountain top (Y/N) : Y
– You will get STEAM.
Ch 1-C Programming 41
Problem 2
Pseudocode
• Step 4 pseudocode will be refined as
– If Temperature > 0 then
– If ((Temperature >= 95) and At Mountain Top) or
– Temperature >= 100 then
– Status is “Steam”
– Else
– Status is “Water”
– Else
– Status is “Ice”
Ch 1-C Programming 42
Problem 3
Number Guessing Game
• We need to design a Guessing Game the
most easy one is number guessing.
– the game will ask the player to guess a hidden
number from a pre-defined range
– if the guess is wrong the program will indicate
whether the guess is higher or lower than the
target value
– the game will repeatedly ask the player to enter
guess until a correct one is entered
Ch 1-C Programming 43
Problem 3
Example Input/Output
• Number Guessing Game
• ------------------------------
• The range of number to guess : 1 - 100
• Please enter first guess : 50
• -> higher than that next guess : 75
• -> higher than that next guess : 87
• -> lower than that next guess : 81
• -> Bingo ! Total guess 4 times !
Ch 1-C Programming 44
Problem 3
Pseudocode
• 1. Generate a random number within 1 to 100
• 2. Write the program description message
• 3. Enter the first guess
• 4. Repeatedly ask for guess until a correct one is
entered
• 5. Write a message to inform the player about the
right guess
Ch 1-C Programming 46
Problem 4
Numerical Data Processing
• Write a program to calculate the sum
average largest and smallest values of a
series of number.
• To further control the input process a
interim running subtotal will be printed
whenever the user have inputted 3 values
• The program will stop whenever a zero is
inputted, and a summary will be printed
Ch 1-C Programming 47
Problem 4
Pseudocode
• 1. Write a program description message
• 2. Repeatedly prompt for input values until a
zero is entered
– 2.1 Try to read 3 values from user
– 2.2 Write the interim subtotal line
• 3. Write the Summary line
Ch 1-C Programming 49