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

Chapter 1

Designing and Structuring


Programs

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

Objectives

learn the software development method.


learn and understand the concept of algorithms
learn to design and represent algorithms using
pseudocode and flowcharts

Learning Outcomes

able to develop a software based on a systematic


procedure.
able to formulate algorithms in solving a given problem.
able to implement algorithms using flowchart and
pseudocode.

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

1.1 Software Development Method


Software development method is a framework imposed on
the development of a software product
Each software development process models involves a
series of activities in order to ensure the success in the
software creation

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

The Waterfall Model (1/6)


It is a systematic and effective method of developing a
program
Each phase is seen to occur sequentially

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

The Waterfall Model (2/6)


Requirements
All possible requirements of the system to be developed (i.e., the
expected functionalities and constraints) are clearly defined
The validity of these requirements and the feasibility of
implementing them in the system are also investigated

Analysis
It involves identifying the inputs to the problem, the expected
outputs and any special conditions or constraints imposed
Then, a relationship between the input and output data is
established
For computing problems, appropriate formulae might be used
to relate the input and output data

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

The Waterfall Model (3/6)


Analysis (cont.)
Example: Convert a temperature in degrees Fahrenheit to degrees
Celsius
Input to the problem:
Temperature in degrees Fahrenheit
Expected output:
Temperature in degrees Celsius
Formula for the conversion:
Temperature in degrees Celsius
= 5/9 (Temperature in degrees Fahrenheit 32)

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

The Waterfall Model (4/6)


Design
The specifications of the software are translated into the software
representation
It involves identifying suitable data structures and algorithms to be
used, identifying modules and interfaces between modules
Design tools: pseudocode, flowchart
Top-down design
Divides the problem into several simpler sub-problems, solves
each one of them separately, and then combines them
Many programs could be divided logically into three main parts,
i.e.,
Initialization: Initialize the variables used in the program
Processing: Input data, perform the calculation
Termination: Display the result
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

The Waterfall Model (5/6)


Coding
The implementation of the designed algorithm in a programming
language
If the detailed design is good, the transition from the design to real
code should be smooth
Each line of pseudocode or each block of a flowchart should be
translated into one or few lines of real code

Testing
To discover bugs or defects of the software, and do the necessary
corrections to eliminate these errors
Different sets of test data should be provided for testing

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

The Waterfall Model (6/6)


Maintenance
It is a continuous and systematic process of updating the software
that is already in use, in order to prevent system failures and to
enhance the efficiency of the software
The software is updated to
rectify errors and oversights undetected in the testing phase
accommodate the changing needs of the end-user
Proper documentations should be kept during each phase of
software development process
requirements documents, software design documents, internal
documentation, etc

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

1.2 Algorithms (1/3)


An algorithm is a procedure to accomplish a specific task
A set of well-defined instructions is specified in the order in which
these instructions should be performed

Example: Convert a temperature in degrees Fahrenheit to


degrees Celsius
Initialization: No variables need to be initialized in this case
Processing:
Get the temperature in degrees Fahrenheit
Convert the temperature to degrees Celsius using the formula,
Temperature in Celsius = 5/9 (Temperature in Fahrenheit
32)
Termination: Display the temperature in degrees Celsius
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

10

Algorithms (2/3)
Sequential execution
Instructions executed one after another in the sequence in which
they are written

Transfer the control


When the next instruction executed is not the next one in sequence

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

11

Algorithms (3/3)
Structured programming: All program can be written in
terms of three control structures
Sequence structure: Instructions are executed sequentially by
default
Selection structure: A condition is tested; the next instruction to be
executed is selected from two or more options based on the truth
value of the condition
Repetition structure: A condition is tested; a block of instructions is
repeated as long as the condition remains true

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

12

Pseudocode
It is one of the most commonly used design tools for
developing algorithms
It is an imitation of the actual computer instructions
It is not actually executed on computers
It consists of actions statements
input or output operations, assignment statements, calculations
and data manipulations, function calls, etc

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

13

Flowcharts (1/2)
A flowchart is a graphical representation of an algorithm
Each steps of an algorithm are illustrated as boxes of
various special-purpose symbols connected by arrows
The order in which actions are to be performed is indicated by the
arrows

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

14

Flowcharts (2/2)
Commonly used symbols:
Symbol

Description
Oval or start and end symbol
It indicates the beginning or end of a
program, or a section of code
Rectangle or action symbol
It indicates any types of action
Diamond or decision symbol
It indicates a decision is to be made
Connector or entry and exit symbol
It connects one flowchart to another
Arrow or flowline
It shows the flow of the program

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

15

Examples of Algorithm Designs (1/2)


Design an algorithm that inputs 2 numbers and displays
the numbers in ascending order
Flowchart

Pseudocode

Prompt user for two numbers


Input a and b from user
If a is smaller than b
Print a and b
Otherwise
Print b and a
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

16

Examples of Algorithm Designs (2/2)

Design an algorithm that prints the first 3 integers


Flowchart

Pseudocode

Set count to 1
While count is less than or equal to 3
Print count

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

17

1.3 Applying Software Development Method

(1/5)

Problem statement
Design an algorithm that inputs an arbitrary number of non-zero
positive integers and determine the sum of all odd integers read

Analysis
The problem states that an unknown number of non-zero positive
integers are expected to be received from the user. Upon receiving
these numbers, the algorithm should be able to identify all integers
having an odd value and determine the sum of all odd integers
read
Input to the problem:
integer /* a number with no fractional part */
Expected output:
sum
/* sum of all odd integers read */
Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

18

Applying Software Development Method

(2/5)

Design (Top-down design)


First refinement: Divide problem into smaller tasks and list them in
order
Initialization: Initialize variables
Processing: Input data from user, determine and sum all odd
integers
Termination: Display the result
Second refinement: Commit to specific variables
Refine the initialization phase from Initialize variables to
Initialize sum to 0

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

19

Applying Software Development Method

(3/5)

Design (Top-down design) (cont.)


Second refinement: Commit to specific variables (cont.)
Refine the processing phase from Input data from user,
determine and sum all odd integers to
Input integer
While integer is not 1

If the remainder of dividing integer by 2 is 1


sum = sum + integer
Input integer
Refine the termination phase from Display the result to
Display sum

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

20

Applying Software Development Method

(4/5)

The complete pseudocode:


Initialize sum to 0
Input integer
While integer is not 1
If the remainder of dividing integer by 2 is 1
sum = sum + integer
Input integer
Display sum

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

21

Applying Software Development Method

(5/5)

Flowchart:

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

22

1.4 Summary
The Waterfall model is a commonly used software
development method
An algorithm is a sequence of instructions for carrying out
a procedure or solving a problem
An algorithm can be represented in the form of
pseudocode and flowchart

Powerpoint slides to be used with C Programming for Beginners by HC Ling, SN Cheong, YK Teh,
LC Kwek, Emerson, Vishnu Monn B and Badrolhisham (Pearson Education, 2010)

23

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