Вы находитесь на странице: 1из 33

SME 1013

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

 Experienced programmers spend up to 90% of their time working


on the logic of their program, not on the coding
What are these?

 Building blocks of the game of TETRIS


Building Blocks of a Program
 Sequential Executions
- instructions are performed one after
the other
 Branching Operations
- a decision is made to perform one
block of instructions or another.
 Looping Operations
- a block of instructions is repeated

Only these three basic building blocks are


needed to develop a solution to a problem.
Some Definitions
 Algorithm
- a step-by-step procedure for finding the solution
to a problem.
- the idea of how a problem can be solved.
 Pseudocode
- a ‘free’ way or style of how an algorithm is written.
- a hybrid mixture of a particular programming
language and English.
 Flowchart
- a way to describe algorithms graphically.
- requires correct use of symbols.
Common Flowchart Symbols

start or stop direction of


flow
computation connection
Input or
output branching

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 ρ.

Allow for input of the values of


diameter d, wall thickness t, and
density ρ. Also, allow for output of the
values of volume v and weight w.
Solution 1.2 (Algorithm)
1. Input diameter, thickness, and
density
2. Compute the inner and outer radii
3. Compute the volume
4. Compute the weight
5. Print volume and weight
Solution 1.2 start

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.

If the point lies within the circle, print


out a message and the distance, z, of
that point from the center of the circle.
Solution 1.5 (Algorithm)
1. Input the radius r and the (x, y)
coordinate
2. Compute the distance z = (x2 + y2)1/2 of
the point (x, y) from the origin.
3. Is z < r ?
If yes, then print ‘inside’ and print z
If no, then print ‘outside’
End of branch
Solution 1.5 start

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.

Stop reading in the numbers when one of


them has a value of zero (sentinel value).

Then compute the average of all the


numbers and report it.
Solution 1.10 start

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

Construct a flowchart to read an integer


and evaluate its factorial. Use the
subprocess to calculate the factorial.

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

Вам также может понравиться