Anda di halaman 1dari 9

CS1010E: Programming Methodology CS1010E: Programming Methodology

Introduction
In the beginning.. g g
[ CS1010E AY1112S1 Lec t ur e 1 ]
Lecture Overview
Computers and Computing Fundamentals
Programming Languages Programming Languages
Problem Solving
W i i Al i h i d d Writing Algorithms in pseudo code
2
[ CS1010E AY1112S1 Lec t ur e 1 ]
Computers as Information Processors Computers as Information Processors
(1/2)
Computer = Hardware + Software.
Hardware: physical components for p y p
computation/processing; should be simple, fast,
reliable.
S ft t f i t ti t f t k t Software: set of instructions to perform tasks to
specifications; should be flexible, user-friendly,
sophisticated.
Programs are thus software.
[ CS1010E AY1112S1 Lec t ur e 1 ]
3
Computers as Information Processors Computers as Information Processors
(2/2)
Computer are Information Processors
Computer
Raw
Processed Computer
system
Raw
data
Processed
information
Data units
Internal representation in machine
o 1 bit (binary digit): 0 or 1
o 1 byte = 8 bits
o Floating-point representation, etc.
Data types in programs
i t h fl t t o int, char, float, etc.
[ CS1010E AY1112S1 Lec t ur e 1 ]
4
Computer: Hardware Components
Main Components:
Processor (controls devices and processes data). ( p )
Memory: stores programs and intermediate data.
Input Devices: accept data from outside world.
O t t D i t d t t th t id ld Output Devices: present data to the outside world.
Computer Computer
Processor
Memory
Input Output
5
[ CS1010E AY1112S1 Lec t ur e 1 ]
Computer: Hardware Components
Monitor and
speaker (output)
Contains processor Contains processor,
memory, buses, etc.
Keyboard and
mouse (input) ( p )
6
[ CS1010E AY1112S1 Lec t ur e 1 ]
Software
Software is a written programthat directs the
operation of computer
Related terms:
operation of computer
Program: Sequence of instructions that tells a
computer what to do
Execution: Performing the instruction sequence
Programming language: Language for writing
instructions to a computer
7
[ CS1010E AY1112S1 Lec t ur e 1 ]
Software (1/4) w ( / )
Program
Sequence of instruction that tells a computer what to do q p
Execution
Performing the instruction sequence
Programming language
Language for writing instructions to a computer
Major flavors Major flavors
Machine language or object code
Assembly language
High-level
[ CS1010E AY1112S1 Lec t ur e 1 ]
8
Software (2/4) w ( / )
Program
Sequence of instruction that tells a computer what to do q p
Execution
Performing the instruction sequence
Programming language
Language for writing instructions to a computer
Major flavors Major flavors
Machine language or object code
Assembly language Program to which computer can
respond directly Each instruction
High-level
respond directly. Each instruction
is a binary code that corresponds
to a native instruction.
Example: 0001001101101110 Example: 0001001101101110
[ CS1010E AY1112S1 Lec t ur e 1 ]
9
Software (3/4) w ( / )
Program
Sequence of instruction that tells a computer what to do q p
Execution
Performing the instruction sequence
Programming language
Language for writing instructions to a computer
Major flavors Major flavors
Machine language or object code
Assembly language
Symbolic language
High-level
for coding machine
language instructions.
Example: ADD A, B, C
[ CS1010E AY1112S1 Lec t ur e 1 ]
10
Software (4/4) w ( / )
Program
Sequence of instruction that tells a computer what to do q p
Execution
Performing the instruction sequence
Programming language
Language for writing instructions to a computer
Major flavors Major flavors
Machine language or object code
Assembly language
Detailed knowledge of the machine
i t i d U b l
High-level
is not required. Uses a vocabulary
and structure closer to the problem
being solved.
Examples: Java C C++ Examples: Java, C, C++,
Prolog, Scheme.
[ CS1010E AY1112S1 Lec t ur e 1 ]
11
Programming Languages
At the lowest level, hardware components work
on electrical signals with two states:
On or Off (encoded as 1 or 0) On or Off (encoded as 1 or 0)
known as binary values
Hence, instructions and data must be expressed
in binary
known as Machine Language (Object Code) known as Machine Language (Object Code)
Multiply 3.14159
with 1 23!
????
with 1.23!
010111000111?
12
[ CS1010E AY1112S1 Lec t ur e 1 ]
Programming Languages
It is hard to express our ideas using machine
language directly
Several flavors of programming language help to
bridge the gap!
Machine Assembly High Level Natural
Closer to hardware
Language
Assembly
Language
$
High Level
Language
Natural
Language
0110111011010
1101010101
0110001..
load $r1, 3.141
load $r2, 1.23
mul $r3, $r1, $r2
res = 3.141*1.23; Multiply 3.141
with 1.23
Ease of expression
13
[ CS1010E AY1112S1 Lec t ur e 1 ]
Translating a program
High-level language programs (source
programs) must be translated into machine code
for execution for execution
Translator Translator
Accepts a program written in a source language and
translates it to a program in a target language
Compiler Compiler
Standard name for a translator whose source language is a
high-level language
Interpreter Interpreter
A translator that both translates and executes a source
program
More on the compilation process next lecture
14
[ CS1010E AY1112S1 Lec t ur e 1 ]
How to solve a problem?
Problem solving is a creative process
There is no fixed or universal approach pp
A great discovery solves a great problem but there is a grain
of discovery in the solution of any problem. y y p
Your problem may be modest; but if it challenges your
curiosity and brings into play your inventive faculties, and if
you solve it by your own means you may experience the you solve it by your own means, you may experience the
tension and enjoy the triumph of discovery.
Such experiences at a susceptible age may create a taste for p p g y
mental work and leave their imprint on mind and character
for a lifetime.
George Plya George Plya
15
[ CS1010E AY1112S1 Lec t ur e 1 ]
Problem Solving Process
Programming can be viewed as a problem
solving process g p
From the problem specification, derive a solution
expressed in a program
Problem Solving Process
1. Analysis
2. Design
3. Implementation
4. Testing
16
[ CS1010E AY1112S1 Lec t ur e 1 ]
Problem Solving Process: Analysis
Purpose:
Understand the problem Understand the problem
Establish the scope of the problem
What to do:
Determine the input known facts and desirable Determine the input, known facts and desirable
output
Write down a clear description of the problem so e do a c ea desc p o o e p ob e so
that solution can be attempted
17
[ CS1010E AY1112S1 Lec t ur e 1 ]
Problem Solving Process: Design
Purpose:
Reduce the problem into several components Reduce the problem into several components
Determine the interaction between components
What to do:
Describe the functionality of each of the Describe the functionality of each of the
components
Describe the interaction between components esc be e e ac o be ee co po e s
18
[ CS1010E AY1112S1 Lec t ur e 1 ]
Problem Solving Process: Implementation
Purpose:
Develop each of the components Develop each of the components
What to do:
Flesh out the design into actual usable component Flesh out the design into actual usable component
Use the components to produce an overall
solution so u o
19
[ CS1010E AY1112S1 Lec t ur e 1 ]
Problem Solving Process: Testing
Purpose:
Ensure the solution meets the requirement Ensure the solution meets the requirement
What to do: What to do:
Test each component individually to make sure
they function properly they function properly
Test the components as a whole
20
[ CS1010E AY1112S1 Lec t ur e 1 ]
Problem Solving Process
Analysis
Determine problem
features
Design
Write algorithm
Rethink as
appropriate
Implementation
Produce code
Testing
Check for correctness
and efficiency
The flow chart highlights the corresponding
and efficiency
g g p g
programming activities
21
[ CS1010E AY1112S1 Lec t ur e 1 ]
Algorithmic Problem Solving g b v g
An algorithm is a well-defined computational
procedure consisting of a set of instructions that takes procedure consisting of a set of instructions, that takes
some value or set of values, as input, and produces
some value or set of values, as output.
Algorithm Input Output
[ CS1010E AY1112S1 Lec t ur e 1 ]
22
Algorithms
Characteristics of an algorithm:
1. Each step of an algorithm must be exact
2. An algorithm must terminate
3. An algorithm must be effective
4. An algorithm must be general
Can be presented in pseudo code or
flowchart
23
[ CS1010E AY1112S1 Lec t ur e 1 ]
Euclidean algorithm d g
First documented algorithm by Greek mathematician
Euclid in 300 B C Euclid in 300 B.C.
To compute the GCD (greatest common divisor) of two integers.
1. Let A and B be integers with A > B 0.
2. If B = 0, then the GCD is A and algorithm ends.
3. Otherwise, find q and r such that
A = q.B + r where 0 r < B
4. Replace A by B, and B by r. Go to step 2.
[ CS1010E AY1112S1 Lec t ur e 1 ]
24
Algorithm: Observations
The Euclidean Algorithm highlights several
types of control structure (way to carry out
the steps):
Sequential:
executing the steps one after another
Repetition:
R ti th t ( ) lti l ti Repeating the step(s) multiple times
Selection:
Choose to take some of the steps Choose to take some of the steps
Together with the ability to store intermediate
results all problems can be expressed! results, all problems can be expressed!
25
[ CS1010E AY1112S1 Lec t ur e 1 ]
Find maximum and average of a list of numbers
(1/3)
Version 1
Declare variables sum, count and max.
First, you initialise sum, count and max to zero.
Then, you enter the input numbers, one by one. Then, you enter the input numbers, one by one.
For each number that you have entered, assign it to num and
add it to the sum.
Increase count by 1 Increase count by 1.
At the same time, you compare num with max. If num is larger
than max, let max be num instead.
Aft ll th b h b t d di id b th After all the numbers have been entered, you divide sum by the
numbers of items entered, and let ave be this result.
Print max and ave.
A better version in
End of algorithm.
A better version in
next slide
[ CS1010E AY1112S1 Lec t ur e 1 ]
26
Find maximum and average of a list of numbers
(2/3)
Version 2
sum count 0 // sum = sum of numbers
// count = how many numbers are entered?
max 0 // max to hold the largest value eventually
for each num entered,
count count + 1
sum sum + num
if num > max
then max num
/ t ave sum / count
print max, ave
Is there any error
in this algorithm?
[ CS1010E AY1112S1 Lec t ur e 1 ]
27
Find maximum and average of a list of numbers
(3/3)
Flowchart
start
Terminator box
Process box
sum count 0
max 0
d f
Decision box
end of
input?
increment count
Yes
No
sum sum + num
No
Y
ave sum/count
num > max?
Yes
No
max num
end
print max, ave
[ CS1010E AY1112S1 Lec t ur e 1 ]
28
Algorithm: Examples (1/2) g p ( / )
Example 1: Compute the average of three integers.
A possible algorithm:
enter values for num1, num2, num3
num1
Variables used:
num2 num3
ave ( num1 + num2 + num3 ) / 3
print ave
ave
Another possible algorithm:
enter values for num1, num2, num3
t t l ( 1 2 3 )
num1
Variables used:
num2 num3
total
total ( num1 + num2 + num3 )
ave total / 3
print ave
ave
total
[ CS1010E AY1112S1 Lec t ur e 1 ]
29
Algorithm: Examples (2/2) g p ( / )
Example 2: Find the sum of positive integers up to n
(assuming that n is a positive integer) (assuming that n is a positive integer).
Algorithm:
enter value for n
Variables used:
n
// Initialise a counter count to 1, and ans to 0
count 1
ans 0
while (count <= n) do the following
count
ans
while (count <= n) do the following
ans ans + count // add count to ans
count count + 1 // increase count by 1
// Display answer p y
print ans
[ CS1010E AY1112S1 Lec t ur e 1 ]
30
Step-wise Refinement (1/3) p w ( / )
From preceding examples, we can see that in general
an algorithm comprises three steps: an algorithm comprises three steps:
Input (read data (at the moment from user))
Compute (process the input data to generate some answers) Compute (process the input data to generate some answers)
Output (display the answers)
The compute step is in general the most complex. p p g p
Step-wise refinement breaking down a complex step
into smaller steps.
[ CS1010E AY1112S1 Lec t ur e 1 ]
31
Step-wise Refinement (2/3) p w ( / )
Example: Given a list A containing n integers, how
would you find the second largest value in the list? would you find the second largest value in the list?
Before we begin, remember Phase 1 of How To Solve
It: Understanding the problem. It : Understanding the problem.
Is the problem clear? If not, ask questions!
One possible algorithm: p g
read values into A // step 1: input
sort A in descending order // step 2: compute
print A // step 3: output print A
1
// step 3: output
[ CS1010E AY1112S1 Lec t ur e 1 ]
32
Step-wise Refinement (3/3) p w ( / )
We can actually stop here, if we are clear about how to
do each step do each step.
If not, we need to refine the step. For instance, step 2,
how do we sort? how do we sort?
We wont discuss this now as sorting will be covered later.
Can you solve this problem without using sorting? y p g g
[ CS1010E AY1112S1 Lec t ur e 1 ]
33
Exercise: Palindrome
A word is a palindrome if it reads the same
forward and backward forward and backward.
Examples: NOON, RADAR
How do you determine if a word Wis a
palindrome? palindrome?
You can use notation W to indicate the letter You can use notation W
i
to indicate the letter
at location i
The left most position is 0 W is the first letter The left most position is 0, W
0
is the first letter
34

Anda mungkin juga menyukai