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

Fundamentals of Software

Development
CT010-3-1
Problem Solving Using
Programmed Solutions

Prepared by: FSK First Prepared on:09 th August 2005 Last Modified on:08th September 2005
Quality checked by: GTK
Copyright 2005 Asia Pacific University College of Technology and Innovation

Topic & Structure of the lesson


Introduction to algorithms
Program design using pseudocodes
and simple notations

CT010-3-1 Fundamentals of Software Development

Problem Solving

Learning Outcomes
At the end of this topic, you should be able
to:
Define the steps in developing a
program
Define a problem
Outline a solution using algorithm

CT010-3-1 Fundamentals of Software Development

Problem Solving

Key Terms
If you have mastered this topic, you should
be able to use the following terms correctly in
your assignments and exams:
Program
Programming Language
Algorithm
Pseudocode
Flowchart
Structured Programming
Modular

CT010-3-1 Fundamentals of Software Development

Problem Solving

Problem Solving
Problem solving (within the context of
developing programs) refers to analysing
a problem with the intention of deriving a
solution for the problem.

CT010-3-1 Fundamentals of Software Development

Problem Solving

What is a Program?
A program is a collection of computer
instructions or operations which are
grouped together in a logical manner to
accomplish a given task.

CT010-3-1 Fundamentals of Software Development

Problem Solving

What is a Programming Language?


A vocabulary and set of grammatical
rules for instructing a computer to
perform specific tasks.
Example:
C Language
Visual Basic
Java
C++
CT010-3-1 Fundamentals of Software Development

Problem Solving

What is a Programming Language?


Each programming language has a
unique set of keywords (words that it
understands) and a special syntax for
organizing program instructions.

CT010-3-1 Fundamentals of Software Development

Problem Solving

Steps to Developing a Program


1.
2.
3.
4.
5.
6.
7.

Define the problem


Outline the solution
Develop the algorithm
Test the algorithm for correctness
Code the algorithm
Run the program
Document and maintain the program

CT010-3-1 Fundamentals of Software Development

Problem Solving

1. Statement of the Problem


Concentrate on the problem and identify
the following items:
inputs to the system
outputs of the system
general processing steps to produce
the output.
CT010-3-1 Fundamentals of Software Development

Problem Solving

1. Statement of the Problem


eg. Bill is shopping for some carpet for
his living room which is 12 x 18 feet. The
carpet he likes is RM20 per square yard.
He has RM500 to spend. After
purchasing the carpet, will he be able to
purchase a RM5 Happy Meal with the
money left over?

CT010-3-1 Fundamentals of Software Development

Problem Solving

1. Statement of the Problem


Some questions to ask:
What information has been given?
What do I want to find out?
What essential information is missing?
Is any of the given information
worthless?
What assumptions have been made?
CT010-3-1 Fundamentals of Software Development

Problem Solving

1. Statement of the Problem


$500 available
$20 per square yard
9 square feet per square yard

12 ft = 4 yds

18 ft = 6 yds
CT010-3-1 Fundamentals of Software Development

Problem Solving

2. Outline the solution


During this stage, certain details are
identified from the problem by analysing it
further, such as:
major processing tasks involved
major subtasks (if any)
major control structures
major variables or record structures used
(repetition or selection)
mainline logic

CT010-3-1 Fundamentals of Software Development

Problem Solving

2. Outline the solution


Find dimensions of room in yards.
Find area of room in square yards.
Multiply by cost of the carpet per square
yard to get total cost.
Subtract total cost from money available.
Is remainder enough for Happy Meal?

CT010-3-1 Fundamentals of Software Development

Problem Solving

3. Develop the algorithm


At this stage, a detailed step by step algorithm
is written out.
The algorithm is the design of the program.
When programmers plan the logic for a
solution to a programming problem, they often
use one of two tools:
Flowchart
Pseudocode (pronounced sue-doughcode)

CT010-3-1 Fundamentals of Software Development

Problem Solving

Flowchart
A flowchart is a pictorial representation of
the logical steps it takes to solve a
problem.
And because almost every program
involves the steps of input, processing,
and output; flowcharts need some way to
separate visually these three steps.
When you create a flowchart, you draw
geometric shapes around the individual
statements and connect them with arrows.
CT010-3-1 Fundamentals of Software Development

Problem Solving

Flowchart
The following are the commonly used
symbols for drawing program flowcharts.
off-page
connect
or

terminator

process

CT010-3-1 Fundamentals of Software Development

storage

Problem Solving

Flowchart
The following are the commonly used
symbols for drawing program flowcharts.
decision
making

document

input/output

connector
arrowheads

CT010-3-1 Fundamentals of Software Development

Problem Solving

Flowchart
Begin
Read Amount

YES

Amount>20.00?

Calculate
Actual=Amount * 0.80

Calculate
Actual=Amount

End
CT010-3-1 Fundamentals of Software Development

NO

Problem Solving

Pseudocode
Pseudocode is an English-like
representation of the logical steps it takes to
solve a problem.
Pseudo is a prefix that means false, and to
code a program means to put it in a
programming language; therefore
pseudocode simply means false code

CT010-3-1 Fundamentals of Software Development

Problem Solving

Pseudocode
The following three statements constitute a
pseudocode representation of number
doubling problem:
get NUMBER
compute ANSWER as NUMBER times 2
print ANSWER
CT010-3-1 Fundamentals of Software Development

Problem Solving

3. Develop the algorithm


Example:
Compute steps using a pencil and paper,
or a calculator.
1. len = 12ft/3ft/yd=4yd; wth= 18ft/3ft/yd=6yd
2. Area = 4yd*6yd = 24sq yd
3. Total cost = 24sq yd * RM20/sq yd =
RM480
4. Remainder = RM500-RM480 = RM20
5. Remainder >= RM5? Yes!
CT010-3-1 Fundamentals of Software Development

Problem Solving

4. Test algorithm for correctness


This step is one of the most important in the
development of a program, and yet it is the
step most often forgotten. The main
purpose of desk checking the algorithm is to
identify major logic errors early, so that they
may be easily corrected.
Test data needs to be walked through each
step in the algorithm to check that the
instructions described in the algorithm will
actually do what they are supposed to.
CT010-3-1 Fundamentals of Software Development

Problem Solving

4. Test algorithm for correctness


The programmer walks through the logic of
the algorithm, exactly as a computer would,
keeping track of all major variables on a
sheet of paper.

CT010-3-1 Fundamentals of Software Development

Problem Solving

4. Test algorithm for correctness


Rework problem in feet, rather than yards.
Estimate and compare:
12 x 18 is close to 10 x 20 = 200 sq ft
200 sq ft is close to 20 sq yds
20 sq yds times RM20 each is RM400
This is the same order of magnitude as
our RM480 answer.
Estimates are used simply to check
whether your answer makes sense.
CT010-3-1 Fundamentals of Software Development

Problem Solving

5. Code the algorithm


Code the algorithm into a specific
programming language.
Only after all design considerations have
been met in the previous four steps should
you actually start to code the program into
your chosen programming language.

CT010-3-1 Fundamentals of Software Development

Problem Solving

6. Run the program


This step uses a program compiler and
programmer-designed test data to
machine-test the code for both syntax and
logic errors.
This is usually the most rewarding step in
the program development process.

CT010-3-1 Fundamentals of Software Development

Problem Solving

6. Run the program


If the program has been well designed
then the usual time-wasting frustration
and despair often associated with
program testing are reduced to a
minimum.
This step may need to be performed
several times until you are satisfied that
the program is running as required.
CT010-3-1 Fundamentals of Software Development

Problem Solving

7. Document and maintain the


program
Program documentation should not be
listed as the last step in the program
development process, as it is really an
ongoing task from the initial definition of
the problem to the final test result.

CT010-3-1 Fundamentals of Software Development

Problem Solving

7. Document and maintain the


program
Documentation involves both external
documentation (such as hierarchy charts,
the solution algorithm, and test data
results) and internal documentation which
may have been coded in the program.
Program maintenance refers to changes
which may need to be made to a program
throughout its life.
CT010-3-1 Fundamentals of Software Development

Problem Solving

Quick Review Question


Think of a number. Add 3; multiply by 7; divide by 12.
Write down the answer, or save it in your calculators
memory.
Now take the answer you got and multiply it by 3;
divide by 7; now multiply by 4, and finally subtract
3.
What answer do you get? Why? Dream up a similar
example, using different numbers and operations.
Write in pseudocode the algorithm for doing this trick.
CT010-3-1 Fundamentals of Software Development

Problem Solving

Problem Solving
The first stage to problem solving is to obtain
a clear understanding of the problem.
Take a look at the following problem.
Problem
You are required to design a complete
system which will enable the sum of two
values to be calculated.
CT010-3-1 Fundamentals of Software Development

Problem Solving

Problem Solving
To grapple with this problem, we have to
understand the problem from the human
perspective.
A question to ask yourself is this:
How Would You Calculate the Sum of
Two Values?

CT010-3-1 Fundamentals of Software Development

Problem Solving

Problem Solving
As the computer is also a device similar to
the way in which the human brain functions,
the process of calculating the sum of two
values can also be easily performed by the
computer.

=
CT010-3-1 Fundamentals of Software Development

Problem Solving

Problem Solving
Processing
(Brains)

Input
Output

CT010-3-1 Fundamentals of Software Development

Problem Solving

Problem Solving
Output Device
CPU
(Brains)

Input Device
CT010-3-1 Fundamentals of Software Development

Problem Solving

Problem Solving
Let us assume we are interested in
calculating the sum of 5 and 10.
5

Processing

10
5 + 10 = 15

15
Input
Output
CT010-3-1 Fundamentals of Software Development

Problem Solving

Problem Solving
As shown previously, the example values
(5 and 10) have been specified explicitly.
As the brain is flexible enough in
calculating a wide range of numbers, the
two input values have to be generalised.

CT010-3-1 Fundamentals of Software Development

Problem Solving

Problem Solving
Notice that instead of using specific
numbers, variables are used to represent
these values.
Value2
Value1
Sum = Value1 + Value2

Sum

CT010-3-1 Fundamentals of Software Development

Problem Solving

What Are Variables?


Variables are memory locations within the
computer which allows pieces of data to be
stored.
The word variable comes from the word
vary, which means that whatever you place
within a variable can be changed.
A variable can be viewed as a container
used to store things.
Data (for example, name,
age, salary) can be stored in
these containers.
CT010-3-1 Fundamentals of Software Development

Problem Solving

What Are Variables?

CT010-3-1 Fundamentals of Software Development

Problem Solving

Algorithmic Problem Solving


Algorithmic problem:
Any problem whose solution can be
expressed as a set of executable
instructions.
Algorithm:
A well defined computational procedure
consisting of a set of instructions, that
takes some value or set of values, as
input, and produces some value or set of
values, as output.

CT010-3-1 Fundamentals of Software Development

Problem Solving

Algorithm Development
The basic mathematical operators used in
algorithms are as follows:
+
addition
-

subtraction

multiplication

division

assignment

()

brackets for grouping calculations

CT010-3-1 Fundamentals of Software Development

Problem Solving

Algorithm Development
Example of an algorithm (using pseudocodes)
which can be used to carry out the tasks
outlined is as follows:
1)

Sum = 0

2)

Read Value1, Value2

3)

Calculate
Sum = Value1 + Value2

4)

Display Sum

CT010-3-1 Fundamentals of Software Development

Problem Solving

Review Exercise
Problem
You are required to develop a program to
calculate the total and average of 3
numbers and display the total and average
on the monitor screen.

CT010-3-1 Fundamentals of Software Development

Problem Solving

Quick Review Question


Write an algorithm in pseudocode to:
1.
2.
3.

Enter 3 numbers.
Calculate the total and average of the
3 numbers.
Display the total and average of the 3
numbers.

CT010-3-1 Fundamentals of Software Development

Problem Solving

Summary of Main Teaching Points


Problem Solving
Pseudocodes
Flowcharts
Basic control structures
The sequence structure
The selection structure
The repetition structure

CT010-3-1 Fundamentals of Software Development

Problem Solving

Next Lesson
Introductory Programming Skills
Using the Integrated Development Environment
What is Java?
Why Java is popular?
Java and the Internet
The structure of a program
A simple Java program
How program code is executed by a computer
A look at JDK
Applets and Applications
API Documentation
CT010-3-1 Fundamentals of Software Development

Problem Solving

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