Академический Документы
Профессиональный Документы
Культура Документы
Programming for
Engineers
Programming Concepts
by:
Assoc. Prof. Dr. Mohd Shafiek Yaacob
Stages of Program Development
Logic errors
Syntax errors
Step 1 Devide by
Input?
Fortran? Zero?
Formula? Step 2
C/C++? Formula?
Special Branching?
BASIC? Special
Cases? Looping? Cases?
Java?
Output? Stop? Output?
Program Algorithm Program
Concept Development Coding Debuging
subprocess looping
Sequential Execution of Instructions
Sequential instructions are
executed one after the other.
The computer begins with the
first instruction.
The result from current and
previous calculations may be
used in the subsequent
calculation.
The instructions must be
executed in correct order.
Example 1.1
Construct an algorithm and a
flowchart to compute the weight w
of a hollow sphere of outside
diameter d, wall thickness t, and
density ρ.
Solution 1.1 (Algorithm)
1. Compute the inner and outer radii
ro = d/2 and ri = d/2 – t
2. Compute the volume
v = 4π(ro3 – ri3)/3
3. Compute the weight
w = ρv
Solution 1.1 start
(Flowchart) ro = d/2
ri = d/2 – t
v = 4π(ro3 – ri3)/3
w = ρv
stop
Example 1.2
Construct an algorithm and a
flowchart to compute the weight w of a
hollow sphere of outside diameter d,
wall thickness t, and density ρ.
Input
d, t, ρ
Flowchart
ro = d/2
ri = d/2 – t
v = 4π(ro3 – ri3)/3
w = ρv
Print
v, w
stop
Branching Operations
A branch is a point in the program
where the computer will make a
decision about which set of
instructions to be executed next,
A decision is made depending on
the answer of a posed question. ?
The question must be formulated
such that it has a simple answer
and has only one possible
outcome (e.g. yes or no).
Example 1.5
Construct an algorithm and a
flowchart to determine if a point (x, y)
lies within a circle of radius r centered
at the origin.
Input
(Flowchart) r, x, y
z = (x2 + y2)1/2
yes no
z<r?
Print Print
‘inside’, ‘outside’
z
Stop
Nesting Branching Operations
A nested branch is used when the
computer needs to choose between
more than two alternatives. ?
Example 1.6
Construct a flowchart to see if an integer
n is negative, positive, or zero.
Solution 1.6 start
Input
(Flowchart) n
no yes
n<0?
no yes Print
n=0?
‘Negative’
Print Print
‘Positive’ ‘Zero’
Stop
Loops
A counted loop
repeats the
executions for a Instructions
predetermine
number of times.
A conditional loop
repeats the Count?
executions until a Condition?
no
condition is satisfied.
yes
Counted Loops
Most widely used in
computer
finished start value loop back
programming. stop value
Loop control variable step value
LCV is used to control
the loop. LCV name
The loop stops when
the LCV exceeds stop
body of
value
loop
Number of iterations is
predetermine
Example 1.8
Construct a flowchart to print out the
numbers from 1 to 100 and their
squares.
Solution 1.8 start
(Flowchart) start=1
stop=100
step=1
LCV
Print
LCV, LCV2
Stop
Example 1.9
Construct a flowchart to print out the
even numbers from 1 to 100 and their
squares.
Solution 1.9 start
(Flowchart) start=2
stop=100
step=2
LCV
Print
LCV, LCV2
Stop
Conditional Loops
The most common type is termed the while loop.
A set of instructions is repeated while some
condition is true.
yes no
Finished ?
body of
loop
Example 1.10
Construct a flowchart to read in a series of
numbers and keep track of the running total
and the number of data items.
SUM=0
(Flowchart) COUNT=0
Input X
no yes
X≠0?
AVG=SUM/COUNT SUM=SUM+X
COUNT=COUNT+1
Print
AVG, COUNT
stop
Nested Loops
A counted loop is placed within another
counted loop or a combination of different
loops within each other.
Example 1.11
Construct an algorithm and flowchart to
create a 10 by 10 multiplication table
such as 1 x 1 = 1, 1 x 2 = 2, and so forth.
Solution 1.11 start
start=1
(Flowchart) stop=10
step=1
LCV1
start=1
stop=10
Stop step=1
LCV2
Product =
LCV1 x LCV2
Print
LCV1, LCV2,
Product
Procedures
A large program is often
broken into several
smaller components
known as procedures or
Subprocess
modules.
Modules or procedures
may be reuse again in the
same program or in
another program.
Example 1/13
n! = 1 x 2 x …x (n-1) x n
Solution 1/13 (Flowchart)
Factorial
start
Y =1
Input n
start=1
stop=n
step=1
Y=Factorial(n)
LCV
Y = Y x LCV
Print
n, Y
Stop Return