Lecture5 Week 3
Lecture5 Week 3
11/05/23 1
Programming Fundamentals
2
Programming Fundamentals
3
Programming Fundamentals
4
Programming Fundamentals
1. Requirement Specification
Specifying the problem requirements requires
you to state the problem clearly and to gain the
understanding of what to be solved and what
would be the solution.
When specifying problem requirement, we ask
ourselves the following questions:
What the problem is.
What the solution should provide.
What is needed to solve it.
If there are constraints and special conditions.
5
Programming Fundamentals
Yummy Cupcake
Problem: You are required to calculate the amount to
be paid by a customer buying cupcakes.
What the problem is.
6
Programming Fundamentals
Problem Analysis
Analyzing the problem require us to identify the
following:
Input(s) to the problem, their form and the input
media to be used
Output(s) expected from the problem, their form and
the output media to be used
Special constraints or conditions (if any)
Any formulas or equations to be used
7
Programming Fundamentals
Yummy Cupcake
Input?
Quantity of the cupcake purchased (integer)
Price per cupcake (RM, float)
Output?
Total amount to be paid by the customer (float)
Constraint/condition?
Quantity purchased must be more than zero
Price per cupcake must be more than zero (it is not free)
We assume that the price given is the standard price to all
cupcakes
Formula/equation?
Amount to pay = quantity of cupcake x price per cupcake
8
Programming Fundamentals
Designing algorithm
Designing algorithm to solve the problem requires
you to develop a list of steps, arranged in a
specific logical order which, when executed,
produces the solution for a problem.
Using top-down design (also called divide and
conquer):
You first list down the major tasks
For each major task, you further divide it into sub-
tasks (refinement step)
When you write algorithm, write it from the
computer’s point of view.
9
Programming Fundamentals
10
Programming Fundamentals
Yummy Cupcake
Major Task:
1. Read the quantity of cupcake purchased
2. Read the price per cupcake
3. Calculate total amount to pay
4. Display the total amount to pay
After refinement:
1. Read the quantity of cupcake purchased
2. Read the price per cupcake
3. Total amount to pay = quantity of cupcake x price per cupcake
4. Display the total amount to pay
11
Programming Fundamentals
12
Programming Fundamentals
Control Structure
13
Programming Fundamentals
Control Structure
Sequence: A series of steps or statements that are
executed in the order they are written in an
algorithm.
Selection: Defines two courses of action
depending on the outcome of a condition. A
condition is an expression that is, when
computed, evaluated to either true or false.
Repetition: Specifies a block of one or more
statements that are repeatedly executed until a
condition is satisfied.
E.g
14
Programming Fundamentals
15
Programming Fundamentals
Pseudocodes
A pseudocode is a semiformal, English-like
language with limited vocabulary that can be
used to design and describe algorithms.
16
Programming Fundamentals
Flowcharts
Flowcharts is a graph used to depict or show a
step by step solution using symbols which
represent a task.
The symbols used consist of geometrical shapes
that are connected by flow lines.
It is an alternative to pseudocoding; whereas a
pseudocode description is verbal, a flowchart is
graphical in nature.
17
Programming Fundamentals
Flowchart Symbols
18
Programming Fundamentals
19
Programming Fundamentals
if statement
if-else statement
While
switch statement
conditional operator statement
20
Programming Fundamentals
begin begin
statement 1.
statement 1
statement 2.
… statement 2
…
statement n. …
end statement n
end
21
Programming Fundamentals
Begin begin
read birth year
age = current year – birth year
display age read birth year
End
Age = current year –
birth year
Display age
end
22
Programming Fundamentals
23
Programming Fundamentals
Example: If
#include< iostream.h>
int main( )
{
int x,y; x=15; y=13;
if (x > y )
{
cout << "x is greater than y";
getch();
}
24
Programming Fundamentals
Example: If-else
void main( )
{
int x,y; x=15; y=18;
if (x > y )
{
cout << "x is greater than y";
}
else { cout << "y is greater than x";
}
}
25
Programming Fundamentals
Begin
read age
if (age is greater than 60)
print “Senior Citizen” Begin
else
print “Junior Citizen” Read age
end_if
End
YES NO
age > 60?
Begin
read age print “Senior Citizen” print “Junior Citizen”
if (age > 60)
print “Senior
Citizen”
else End
print “Junior
Citizen”
end_if
End
26
Programming Fundamentals
27
Programming Fundamentals
Exercise
Draw the flowchart diagram for
Example 1 and Example 2
28