01 - Programming Problem-Solving
01 - Programming Problem-Solving
SOLVING
Programming Technique I
(SCSJ1013)
Problem-Solving Process
The Programming Process
Programming Process
This week
Input, Process and Output
Input, Process and Output
Gather input
from keyboard
from files on disk drives
Input data
Length
Width
Process the input
Area = Length * Width
Output Data
Area
In-Class Exercise 1
Develop Algorithms
Flowchart
Pseudo code
Top-down design
Structured Chart
Algorithms
Algorithm - a sequence of a finite number of steps arranged
in a specific logical order to produce the solution for a
problem.
Algorithms requirements:
Must have input
Must produce output
Unambiguous
Generality
Correctness
Finiteness
Efficiency (pg. 32)
Pseudo codes
Pseudo Code
Calculate Result
C=A*B
Display the
Result C
Stop
Flowchart Symbol
Flowchart Symbol
Terminal: Used to indicates the start and end of a flowchart. Single flowline. Only one “Start”
and “Stop” terminal for each program. The end terminal for function/subroutine must use
“Return” instead of “Stop”.
Process: Used whenever data is being manipulated. One flowline enters and one flowline exits.
Input/Output: Used whenever data is entered (input) or displayed (output). One flowline enters
and one flowline exits.
Decision: Used to represent operations in which there are two possible selections. One flowline
enters and two flowlines (labelled as “Yes” and “No”) exit.
On-page Connector: Used to connect remote flowchart portion on the same page. One flowline
enters and one flowline exits.
Off-page Connector: Used to connect remote flowchart portion on different pages. One flowline
enters and one flowline exits.
Get A
Input.
Get B Enter values for A and B
Calculate Result
Process.
C=A*B
Display the
Output
Result C
Stop Terminal
Stop Program end here
Example: Use of comments/
description
Start
Yes
No
Stop
Example: Use of connectors on the
same page
Start 2
1
No
2
Example: Use of connectors on the
different page
Page 1 Page 2
Start
2
1
Yes Stop
1
No
2
In-Class Exercise
Start
Display the
Name and Tax
Your program will display the name
of the employee and amount of tax
Stop
on the screen.
Flowchart Structures
Control Structure
1. Start
2. Statement_1
3. Statement_2
4. Statement_3
: :
n. Statement_n
n+1. End
Sequential Structure: Flowchart
statement
statement
Flowchart Tracing
Start
Read Input:
Length, Length = 5
Width Width = 3
Calculate Area
Process:
Area = Length * Width
Area = 5 * 3 = 15
Print Output
Area, Area = 15
Perimeter Perimeter = 16
Stop
Trace Table
Trace tables allow developers to test their algorithms in order
to make sure there are no logic errors.
Example:
d. 3 N is odd
number
End
Flowchart Structures: Selection
Selection Structure
Selection allows you to choose between two or more
alternatives; that is it allows you to make decision.
TRUE
condition
condition
FALSE
statement
statement
1. Start
2. Read n
3. If n modulus 2 == 0
1. Print “This is an even number”
4. End if
5. End
Flowchart
Start
Read n
n%2=
0
True
False
Print “this
is an even
number”
End
Selection Structure
(cont..)
• Pseudo code – requires the use of the keywords if and
else.
TRUE FALSE
condition
Read n
n%2=0
False
True
End
Selection Structure –
Problem Examples
Used to compare numbers to determine relative order
Operators:
Examples:
12 > 5 is true
7 <= 5 is false
if x is 10, then
x == 10 is true,
x != 8 is true, and
x == 8 is false
Logical Operators
Used to create relational expressions from other relational
expressions
(x <= z) || (y == z) false
(x <= z) || (y != z) true
Start
Enter a Number >> 10
Category A
Input:
Read Num
Num <- 10
Num = 10
10 > 0 ? => YES
Num>0? No
Print
"Category B"
Yes
Print
Output:
"Category A"
“Category A”
Stop
Example:
What is the output of the following flowchart when the input is Num= 0
Start
Enter a Number >> 0
Input:
Num <- 0
Category B
Read Num Category A
Num = 0 Output:
0 > 0 ? => NO “Category B”
Num>0? No
Print
"Category B"
Yes
Print
Output:
"Category A"
“Category A”
Stop
Example:
What is the output of the following flowchart when the input Num= 4
Start Variables
Variables
Variables (in
(in
(inmemory):
memory):
memory):
Num
Num
Num [[[ 444 ]]]
Read Num Input: Result
Result
Result [[[ 4
0710]
9 ]]] 0497 +++ 4312
Num <- 4 Count
Count [[[ 3
Count 420
1 ]]] 4312 --- 111
Initialize
Result=0
Count=Num
Print
Result
Stop
In-Class Exercise
Start
Print
itemName,
nettprice
End
In-Class Exercise 2
• Write a pseudo code for a program that will
accept 2 numbers. If the first number is greater
than the second number, find the difference
between the numbers and print the numbers and
difference value. If the second number is greater
than the first number, find the sum of the two
values and print the numbers and the sum.
• Draw the flowchart for the pseudo code.
• Trace the algorithm with the following input.
Write the output: 40 50
70 30
In-Class Exercise 3
Algorithm: nested if
:
n. if condition
:
n.m if condition
n.m.1 statement
:
n+1. end_if
:
Selection Structure (cont..)
Nested if
(if within if)
FALSE
test1 FALSE
test1
TRUE
TRUE
FALSE
TRUE
test2
statement
Considered as
one statement
it is an “one-choice” if
Selection Structure (cont..)
• Pseudo code – nested if using if-else if.
Algorithm: if-else if
:
n. if condition
n.m if condition
n.m.1 statement
:
n+1. else
n+1.m.1 statement
:
n+2. end_if
:
Selection Structure (cont..)
FALSE
condition
TRUE
statement
statement TRUE
condition
FALSE
statement
T
condition
statement
T
condition statement
T
statement condition
statement
In-Class Exercise 1
• Suppose we want to associate noise loudness
measured in decibels with the effect of the
noise. The following table shows the
relationship between noise levels and human
perception of noises. Draw a flow chart.
false
“Very
acidic”
In-Class Exercise 3
• Write a pseudo code and draw a flow chart for
a program that will implement the following
decision table. The program will print the
transcript message based on the input grade
point.
Grade Point Average Transcript Message
0.0 – 0.99 Failed
1.0 – 1.99 On probation
2.0 – 2.99 Average
3.0 – 3.49 Dean’s List
3.5 – 4.00 Highest Honors
Flowchart Structures: Repetition
Repetition Structure
FALSE
condition
condition
TRUE statement
body of loop
x
Start
initialization
cnt = 0
°
FALSE
FALSE
condition
cnt < 5 End
TRUE
TRUE
body of loop
Print “Programming”
increment
cnt = cnt + 1
y
Pre-test loop steps summary
Counter-controlled loop
Initialization of counter: counter = 0
Testing of counter value: counter > n
Updating of counter value (increase by 1) during each iteration:
counter = counter + 1
Example
Steps:
How many repetition?
• Initialization
• Condition to check for the counter?
• Update of counter
Pseudo code
1. Start
2. Set sum=0, counter = 0
3. While (counter < 10)
3.1 sum = sum + counter
3.2 counter = counter + 1
4. End_While
5. Display sum
6. End
Flow Chart
Start
sum = 0, counter = 1
false
counter print sum
< 10
true
counter = counter + 1
Trace the following
Start
sum = 0, counter = 0
What is the output for the
following input:
false
counter print 20 30 40 50 10
<5 sum
true
read n
End
sum = sum + n
counter = counter+1
Repetition Structure:
Post-Test
• Pseudo code – requires the use of the keywords repeat..until for
post-test loop.
statement
statement
condition
TRUE
FALSE
1. Start
2. Set sum = 0, counter = 0
3. do
3.1 sum = sum + counter
3.2 counter = counter + 1
4. while (counter < 10)
5. Display sum
6. End
Flow Chart
Start
sum = 0, counter = 0
true
counter = counter + 1
End
In-Class Exercise 1
• Develop an algorithm (pseudo code) and flow
chart for a program to calculate an average of
15 numbers input by the user. Use pre-test
loop
• Modify your solution above by using the post-
test loop.
In-Class Exercise 2
• Develop an algorithm and flow chart to print
even numbers between 1 to 50. Use pre-test
loop.
• Modify your solution by using post-test loop.
Repetition Structure -
Letting the User Control a Loop
• Program can be written so that user input determines loop
repetition.
Start
cnt = 0
Get limit
FALSE
cnt < limit End
TRUE
Print “Radziah”
cnt = cnt + 1
Repetition Structure -
Sentinels
• Used to terminate input when user may not know how many
values will be entered
Repetition Structure -
Sentinels
Page 1 Page 2
Start terminal for a
Start Function is different.
Read
n1, n2 , n3
sum = n1+
n2+n3
result = AVRG (n1, Body of a function is
the same with
n2, n3) main flowchart
At this point,
we only focus on what
result = sum/3
to do. How to do it,
it comes later. Print
result
This part is known as
Function-Call Return result
Stop
End terminal
must be a “Return”.
“result” is the output
Start
AVRGSUM (n1,
n2,n3)
Read
X, Y , Z
sum = n1+
n2+n3
(S,A)= AVRGSUM
(X,Y,Z )
Read
n1, n2 , n3
sum = a+b+c
result = AVRG
(n1,n2,n3)
Each formal parameter represents
an actual parameter according
to its order: R = sum/3
a represents n1,
Print b represents n2,
Parameters used in a function-call result c represents n3
are called actual parameters
The name of an actual parameter
Return Rmay be
result, n1, n2, n3 are actual parameters different from its formal parameter
Stop
Start
AVRG ( a,b,c)
Read
Wrong!
n1, n2 , n3
n1, n2, n3
can only be sum = n1 + n2
referred to + n3
in the main
result = AVRG (n1, flowchart.
n2,n3)
R = sum/3
Wrong! Print
R can only be R
referred to
in flowchart Return R
AVRG.
Stop
A function may be called several times
Page 1 Page 2
Start
AVRG (a, b,c)
Read
n1, n2 , n3
result = (average1 +
average2) / 2 Now:
Stop
Function definition:
Start EXCHG( x, y)
Read hold = x
p, q
x=y
EXCHG (p, q)
y = hold
Print Return
p, q
Function call:
Start
N=6
AVRG ( n1, n2, n3)
Read
N Sum = 10 + 5 + 6
result = sum/3
Print
average Output:
Average: 7
Return Result
Stop
What you have learnt so far ….
•Analysis:
-Understanding the problem
•Design:
- Developing algorithm
- Understanding how a flowchart works
- Constructing flowcharts
The next step is implementation (coding)
Problem
Flowchart
Intermediate code
-prepare a rough code-
Complete code
- add details -
The conversion process is straight forward
Example: multiplying two numbers
Intermediate C++ code
Read A
cin >> A;
Read B
cin >> B;
C = A * B;
Calculate Result
C=A*B cout << C;
}
Display the
Result C
Start
Average(a, b, c)
Read
n1, n2 , n3 sum = a + b + c
Stop
Intermediate code of the main flowchart
Preparing the rough code
Print
avrg
Stop
Intermediate code of the function flowchart
Preparing the rough code
Average(a, b, c)
Average(a, b, c) {
sum = a + b + c;
sum = a + b + c
result = sum / 3.0;
return result;
result = sum / 3 }
Return result
The complete code
Adding details to the rough code. The details are shown by
bold texts
#include <iostream>
Using namespace std;
sum = a + b + c;
result = sum/3.0;
return result;
}
int main ()
{
int n1;
int n2;
int n3;
float avrg;
avrg = Average(n1,n2,n3);