You are on page 1of 15

By the end of this section, the students should be able to:

 Understand basic techniques of problem solving


 Develop structure chart, flowchart and pseudocode
 Use Microsoft Visio 2003 for creating structure chart, flowchart and
pseudocode

1 Introduction

Programming is a problem-solving activity. The problem solving method is varying


according to the subject area. For example, engineering problems are solved through
scientific method, while business problems are solved using business system approach.
For programming problem, it is solved using software development method. A basic
software development method consists of four common steps as follows:
a. Step 1: Analysis
b. Step 2: Design the algorithm
c. Step 3: Code
d. Step 4: Test
In this chapter, you will learn what activities need to be done in Step 1 and Step 2.

1
2 Theoretical Background

3 Step 1: Analysis

In this step, you have to highlight the problem statement and eliminate unimportant
aspects in the problem. There are two main activities in this step:
a) Identify what are the input, processes and output of the problem.
b) Draw a structure chart of needed. The structure chart is developed if only you
want to use top-down design, in which you divided the problem into several sub
problems.

4 Step 2: Design the Algorithm

This step is to define the process outlined in Step 1 in details. The process consists of
using the input and executing a series of actions in a specific order to produce the desired
output. This process is referred as algorithm.
Algorithm is a procedure for solving a problem in terms of the actions to be
executed and the order in which these actions are to be executed. •Algorithm can be
written in pseudo codes or flowchart:
a) Pseudocode
 Pseudocode is informal language that helps programmers develop
algorithms.
 Its purpose is to describe in precise algorithm detail, what the program
being designed is to do.
 Pseudocode defined the steps to accomplish the tasks in sufficient
details so that they can be converted to computer program.
b) Flowchart
 A diagram that shows the step by step execution of a program.
 Table 1 shows common flowchart’s symbols.

2
Table 1: Common flowchart’s symbols
Symbol Name Description
Terminal/Terminator The beginning or end of a
program

Process Computation or data


manipulation

Data An input or output


operation

Decision A program branch point

On-page reference/ An entry to, or an exit from,


Connector another part of the flow
chart

Predefined process Call for a function

Flow lines Connecting the flowchart’s


symbols

 Each symbol indicates the type of operation to be performed.


 Flowchart graphically illustrates the sequence in which the operationsare to be
performed.

3
 Basic control structures:
i. Sequence: is a series of statements that execute one after another

Statement 1 Statement 2 Statement 3

ii. Selection (branch): is used to execute different statements depending on


certain conditions. Eg: IF condition THEN statement1 ELSE statement2

FALSE
Condition Statement 2

TRUE
Statement 1

ii. Repetition (loop): is used to repeat statements while certain conditions are
met.

CONDITION FALSE

TRUE

Statement

4
4.1 Step by step Examples

4.1.1 Drawing flowchart using Microsoft Visio

1. Start Visio 2000 : Click Start -> Program -> Microsoft Office > Microsoft
Visio
2. Choose the drawing type: On the Category section (on the left), click on the
Flowchart folder, and select Basic Flowchart on template column.

3. A drawing canvas and basic flowchart’s shapes will be shown.

5
4. To create a new symbol, (e.g. process symbol), , click on the process item and
drag it on to the grid paper.

5.

6
To save the drawing, click on File menu ->Save as -> click the Save button.

6. To add others drawing type. Click File ->New -> and select the drawing type you
want.

ware that can be used to draw a flowchart is Dia(https://wiki.gnome.org/Dia). Dia is an open source application, and work cross

7
4.2 Problem Solving (Step 1 and Step 2)

4.2.1 Example 1: Employee’s Salary (using top-down approach)

Given the employee’s working hour, a program will calculate salary for an employee.
The pay rate for an hour is RM 6.50. The program will display the total salary of the
employee.

Step 1: Analysis

i. Identify input, process, and output.


a. Input: Employee’s working hour, hour
b. Process: Calculate the salary based on the pay rate per hour, hour x 6.5
c. Output: The total salary, salary
ii. Structure chart

Employee’s salary
problem

Input the employee’s Display the total


Calculate the salary
working hour salary

Step 2: Design the Algorithm

i. Pseudocode
1. Start
2. Set salary=0
3. Call input() function
4. Call calculateSalary() function
5. Call display() function
6. End
Input() Function
1. Start
2. Read the employee’s working hour, hour
3. End

8
calculateSalary() Function
1. Start
2. Salary = 6.5*hour
3. End
Display() Function
1. Start
2. Display Salary
3. End

ii. Flowchart

START

input()
Read employee’s working
hour, HOUR

calculateSalary()
SALARY = HOUR*6.5

displaySalary()
Print SALARY

END

9
4.2.2 Example 2: Arithmetic Problem

A program will add two numbers and print a statement whether the sum is an even
number or an odd number. The program will repeat for five times.

Step 1: Analysis

i. Identify input, process, and output.


a. Input: Two numbers, A and B
b. Process:
i. Add the numbers, C = A + B
ii. Check C whether it is odd or even number
c. Output: Print the result

Step 2: Design the Algorithm

i. Pseudocode:
1. Start
2. Set COUNTER=0
3. If COUNTER < 5
4. Read two numbers: A, B
5. C = A + B
6. If C is an even number
Print “EVEN NUMBER”
7. Else
Print “ODD NUMBER”
8. Add 1 to COUNTER
9. Repeat step (3)
10. End

10
ii. Flowchart

START

COUNTER = 0

COUNTER < 5? False END

True

READ A,B

C=A+B

False PRINT
C IS EVEN
“ODD
NUMBER?
NUMBER”

True

PRINT “EVEN
NUMBER”

COUNTER = COUNTER + 1

11
4.3

12
Exercises

(Write pseudocode and flowchart for problems below)


1. A program will read four integers from the keyboard, calculates their average, and
then prints the numbers with their average.

2. The System will print “A” for exam grades greater than or equal to 90, Print “B” for
grades in the range 80 to 89, Print “C” for grades in the range 70 to 79, Print “D” for
grades in the range 60 to 69 and “F” for all other grades.

3. The system will calculate the perimeter of a circle. The radius is 5.With the perimeter
formula (2*radius*PI), print the output of the perimeter for the circle. Value of PI is
3.142.

4. A program will change a temperature reading from Farenheit to Celcius using the
following formula:
Celcius = (100/180) * (Farenheit – 32)

5. If a ladder is placed on the side of a building at 85 degree angle (as illustrated


below), the height at which the ladder touch the building can be calculate based on
the following formula. The height of the ladder is to be read from users, and you may
used C++ library function for sin().
Building’s Height = ladder_height * sin 85.

Building’s height

85 degree

13
4.4 Self-Review Questions

1. Answer the questions below.


(a) What is an algorithm?

(b) What is the difference between pseudocode and flowchart?

(c) Which of the following are algorithms?


i. Directions on a box for preparing one bowl of oatmeal.
ii. List of nutrients in the oatmeal.
iii. List of health recommendations for a long life.
iv. Directions for filling out a registration form.
v. Rules for dividing one number by another.

2. Draw a flowchart for pseudo code below:


1. Start
2. Read 2 integers, A, B
3. If A > B
Repeat step 2
Else
Print B
4. Stop

14
3. The flowchart below represent a program that will calculate the time takes for a ball to
hit the ground after it has been dropped from a tower. The tower’s height is to be read
from the user. Write a pseudocode for the flowchart. (You may used C++ sqrt() library
function)
START

INPUT
HEIGHT,
h
TIME = sqrt(2 * height / 32.2)

PRIN
T
TIME

STOP

4. Develop a pseudocode to interchange the content of two cups of liquid. Assume that

the third cup is available to hold the content of either cup temporarily. Each cup

should be rinsed before any cup is poured into it.

5. Write a flowchart for the process of making a telephone call.

You might also like